Python: recursive state check

How to check the full signature of a nested abstract class type? In this example

In [4]: from typing import Sequence

In [5]: IntSeq = Sequence[int]

In [6]: isinstance([1], IntSeq)
Out[6]: True

In [7]: isinstance([1.0], IntSeq)
Out[7]: True

I want the last call to isinstanceactually return False, whereas it only checks that argument Sequence. I was thinking about recursive type checking, but IntSeqdon't have any public attributes that store the nested type (s):

In [8]: dir(IntSeq)
Out[8]: 
['__abstractmethods__',
 '__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__extra__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__len__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__origin__',
 '__parameters__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__slots__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 '_abc_cache',
 '_abc_negative_cache',
 '_abc_negative_cache_version',
 '_abc_registry']

Thus, entering nested types is not straightforward. I can not find the relevant information in the documents.

PS I need this for multiple implementations of sending.

Update

Alexander Huszagh Blender , Python 3.5 () , : __parameters__ __args__. Linux (Ubuntu) Darwin (OS X), Linux . Linux , __parameters__, OS X. .

+4
1

, -, , ; , .

, __parameters__ ; , 3.5.1. git Python (3.6.0a4+) __parameters__ , __args__ , __origin__ - __bases__:

>>> intSeq = typing.Sequence[int]
>>> intSeq.__args__
(<class 'int'>,)
>>> intSeq.__parameters__
()
>>> intSeq.__origin__
typing.Sequence<+T_co>

3.6 , , , PEP 411, , , , .

+2

Source: https://habr.com/ru/post/1652111/


All Articles