I'm trying out using Python's type annotations
with abstract class.
My __init__
function looks like this:
from abc import ABCMeta
class SomeClass(object, metaclass=ABCMeta):
def __init__(self, *args, **kwargs):
print("Initiating %s object.", self.__class__.__name__)
self.username = kwargs['data']
assert isinstance(self.username, str)
is_premioum = kwargs.get('premioum', False)
self.money_investmant = kwargs.get('investmant')
if isinstance(self.money_investmant, str):
self.money_investmant = float(self.money_investmant)
As you can see, kwargs
could contain arguments from a several number of types- float
, bool
and str
.
Now, I am trying to write the type annotation for the function, that looks like this:
def __init__(self, *args, **kwargs: Union[bool, str, float]) -> None:
But my PyCharm
IDE alerts me:
Except type 'Integral', got 'str' instead
And:
Cannot find referance 'get' in bool | str | float'
Am I doing something wrong?
How should I write the type annotation for kwargs if it contains arguments from multiple types?