Vu Nguyen Vu Nguyen - 6 months ago 24
Python Question

How to determine type from type aliasing?

After I declare an alias

t1 = typing.List[int]
or
t2=typing.List[double]
, how do I determine from
t1
that it is a
List[int]
and from
t2
that it is a
List[double]
? Are there some hidden attributes or functions of
t1
and
t2
that can help?

Answer

Interactively, your most useful option is probably

>>> t1
typing.List[int]

If you want to inspect it in a program, you can look at things like __name__, __parameters__, or whether issubclass reports it as a subclass of typing.List:

>>> t1.__name__
'List'
>>> t1.__parameters__
(<class 'int'>,)
>>> issubclass(t1, typing.List)
True