Created
June 6, 2020 00:28
-
-
Save davips/354773001f99f3290eda0f2e01f4abe8 to your computer and use it in GitHub Desktop.
MyPy doesn't accept subclass as type in derived methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from abc import abstractmethod | |
from typing import get_type_hints | |
class DataOrColl: | |
pass | |
class Collection(DataOrColl): | |
pass | |
class Data(DataOrColl): | |
pass | |
class Component: | |
@abstractmethod | |
def func(self, datacoll: DataOrColl) -> DataOrColl: | |
pass | |
def monta_transformer(self): | |
args = get_type_hints(self.func) | |
if 'data' in args: | |
print('montando transf for data') | |
elif 'collection' in args: | |
print('montando transf for collection') | |
else: | |
raise Exception("Missing a valid parameter! Should be 'data' or 'collection':", args) | |
print(args) | |
class MyComponent(Component): | |
def func(self, data: DataOrColl) -> DataOrColl: # mypy exige Union[Data, Collection], não aceita Data | |
return Data() | |
my = MyComponent() | |
my.monta_transformer() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment