Skip to content

Instantly share code, notes, and snippets.

@gdevanla
Last active January 20, 2021 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdevanla/ee9f0ff13b66a7d2edfa79fede3c7a2d to your computer and use it in GitHub Desktop.
Save gdevanla/ee9f0ff13b66a7d2edfa79fede3c7a2d to your computer and use it in GitHub Desktop.
import typing as tp
class Abstract:
def func1(self, *, x: int, y: str):
raise NotImplementedError()
class Foo(Abstract):
@classmethod
def func1(cls, *, x, y):
print(x)
print(y)
class Bar(Abstract):
@classmethod
def func1(cls, *, x, y): # pylint: disable=unused-argument
print(x)
def bar1(x: tp.Type[Abstract]):
x.func1(10, 20)
# alternate implementation
class Foo1(:
@classmethod
def func1(cls, *, x, y):
print(x)
print(y)
class Bar1:
@classmethod
def func1(cls, *, x):
print(x)
Abstract1 = tp.Union[tp.Type[Foo1], tp.Type[Bar1]]
def bar2(x: Abstract1):
if x is Foo:
x.func1(10, 20)
elif x is Bar:
x.func1(10)
else:
raise ValueError(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment