I've tried things like NewType['SomeType', (SomeClass, typing.Container)]
, and it doesn't work.
Suppose I have a class that's really simple, as follows.
class A:
def __init__(self, a):
self.a = a
Now I want to annotate a function that returns this type as NewA[int]
, i.e. using __getitem__
, which I obviously haven't defined in A
above, but it adds an extra layer of description. Is this possible? And if not, is there a reason I shouldn't try to do something like this?
The answer is to use Generic
, as defined in the standard library's typing
documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With