You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
typing: tested TypeVar instance subclass TypeError is incidental #90800
The reason this raises a TypeError is incidental and subject to behavior change, not because doing so is prohibited per se; what's happening is the class creation does the equivalent of type(TypeVar('T')(name, bases, namespace), but this calls TypeVar's __init__ function with these items as the TypeVar constraints. TypeVar runs typing._type_check on the type constraints passed to it, and the literals for the namespace/name do not pass the callable() check in typing._type_check, causing it to raise a TypeError. I find it dubious this is the behavior the testcase is intending to test and the error it gives is confusing
I propose we add __mro_entries__ to the TypeVar class that only contains a raise of TypeError to properly handle this case
The reason this test passed before is a bit confusing. Run the following code standalone to see where the type(TypeVar('T'))(name, bases, namespace) check is coming from.
class TypeVar:
def __init__(self, name, *constraints):
# in actual TypeVar, each constraint is run through
# typing._type_check, casuing TypeError via not callable()
print(repr(constraints))
class V(TypeVar("T")):
pass
Note that instances of most other types are non-subclassable "by accident".
>>> classA(42): pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: int() takes at most 2 arguments (3 given)
>>> classB:
... def__init__(self, *args): pass
...
>>> classC(B()): pass
...
>>> C
<__main__.B object at 0x7fdcfb49aae0>
It is okay until we decide that there is a problem, and it that case it would require more general solution.
Are there any issues with this in real code?
GBeauregard commented on Feb 6, 2022
GBeauregardmannequin
MannequinAuthor
More actions
The issue in real code I had in mind was internal to cpython: if we remove the callable() check from _type_check naively, this test starts to fail. Both of our proposed changes happen to not fail this check. Given your second example, would you prefer if we remove this testcase if the issue comes up in the final proposed patches? On the other hand, there is precedent for giving this a nice error message other places in typing.py.
This still behaves similarly after the bpo-46642 fix:
>>> classV(TypeVar("T")): pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/jelle/py/cpython/Lib/typing.py", line 906, in __init__
self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jelle/py/cpython/Lib/typing.py", line 906, in <genexpr>
self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
^^^^^^^^^^^^^^^^^^^
File "/Users/jelle/py/cpython/Lib/typing.py", line 189, in _type_check
raise TypeError(f"{msg} Got {arg!r:.100}.")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: TypeVar(name, constraint, ...): constraints must be types. Got (~T,).
Activity
GBeauregard commentedon Feb 4, 2022
cpython/Lib/test/test_typing.py
Lines 227 to 230 in bf95ff9
typing's testcases contain the following test to ensure instances of TypeVar cannot be subclassed:
The reason this raises a TypeError is incidental and subject to behavior change, not because doing so is prohibited per se; what's happening is the class creation does the equivalent of type(TypeVar('T')(name, bases, namespace), but this calls TypeVar's __init__ function with these items as the TypeVar constraints. TypeVar runs typing._type_check on the type constraints passed to it, and the literals for the namespace/name do not pass the callable() check in typing._type_check, causing it to raise a TypeError. I find it dubious this is the behavior the testcase is intending to test and the error it gives is confusing
I propose we add __mro_entries__ to the TypeVar class that only contains a raise of TypeError to properly handle this case
I can write this patch
GBeauregard commentedon Feb 5, 2022
The reason this test passed before is a bit confusing. Run the following code standalone to see where the type(TypeVar('T'))(name, bases, namespace) check is coming from.
GBeauregard commentedon Feb 5, 2022
Fixing this test unblocks bpo-46644
serhiy-storchaka commentedon Feb 6, 2022
Note that instances of most other types are non-subclassable "by accident".
It is okay until we decide that there is a problem, and it that case it would require more general solution.
Are there any issues with this in real code?
GBeauregard commentedon Feb 6, 2022
The issue in real code I had in mind was internal to cpython: if we remove the callable() check from _type_check naively, this test starts to fail. Both of our proposed changes happen to not fail this check. Given your second example, would you prefer if we remove this testcase if the issue comes up in the final proposed patches? On the other hand, there is precedent for giving this a nice error message other places in typing.py.
serhiy-storchaka commentedon Feb 6, 2022
The test is good. If we accidentally make a TypeVar instance subclassable, it will catch such error.
JelleZijlstra commentedon Mar 12, 2022
This still behaves similarly after the bpo-46642 fix: