Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: how to display nested type information?

Tags:

python

Does Python inclue a built-in type variant that will display nested type information, something like this?

>>> extended_type([()])
<class 'list' containg <class 'tuple'>>
like image 273
Mark Harrison Avatar asked Jul 30 '26 06:07

Mark Harrison


1 Answers

No. Type hints and the typing module and PEP 585 provide a notation for this (namely list[tuple] or List[tuple] before Python 3.9), but these are only meant to be checked by an external type-checker like MyPy; there's no capability for checking it at runtime.

PEP 585 on Making isinstance(obj, list[str]) perform a runtime type check:

This functionality requires iterating over the collection which is a destructive operation in some of them. This functionality would have been useful, however implementing the type checker within Python that would deal with complex types, nested type checking, type variables, string forward references, and so on is out of scope for this PEP.

like image 159
Dennis Avatar answered Aug 01 '26 20:08

Dennis