How do I define a `NewType` that's subscriptable, like `List[int]`

788 views Asked by At

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?

1

There are 1 answers

2
emmeowzing On BEST ANSWER

The answer is to use Generic, as defined in the standard library's typing documentation.