Does Python inclue a built-in type variant that will display nested type information, something like this?
>>> extended_type([()])
<class 'list' containg <class 'tuple'>>
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.
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