[3.14] Address invalid inputs of TypeAliasType#477
Conversation
Also draft for others
- compatibility tests have their own test - skip tests for appropriate python versions
| for case, msg in invalid_cases: | ||
| with self.subTest(type_params=case): | ||
| if TYPING_3_12_0 and sys.version_info < (3, 14): | ||
| self.skipTest("No backport for 3.12 and 3.13 requires cpython PR #124795") |
There was a problem hiding this comment.
We decided not to backport in CPython. This test should be changed so that typing_extensions does provide the backport.
There was a problem hiding this comment.
As TypeAliasType cannot be subclassed, I tried to do a minimal invasive workaround with __new__ to return the typing variant but this fails the pickling check.
Do you know a workaround or is this __new__ method not something to pursue further?
At the moment the <3.12 backport would pass the test and would work as a full backport for 3.12 & 3.13.
There was a problem hiding this comment.
I think we can just use the pre-3.12 variant on 3.12 and 3.13.
There was a problem hiding this comment.
Guess with annotationlib (likely?) comming it needs a backport anyway.
| default_value_encountered = False | ||
| parameters = [] | ||
| for type_param in type_params: | ||
| if not isinstance(type_param, (TypeVar, TypeVarTuple, ParamSpec)): |
There was a problem hiding this comment.
This needs to accept both the typing and typing_extensions versions of these classes, which are different on some versions. Test this.
There was a problem hiding this comment.
Yes, I saw this too, but realized that _TypeVarLikeMeta handles isinstance check. test_type_params_compatibilitytest_type_var_compatibility and the other methods test this.
…' into TypeAliasType/invalid_param_spec
There was a problem hiding this comment.
What "some cases" exactly? Ideally this should be accepted.
There was a problem hiding this comment.
Updated the comment for more clarity. As isinstance(Unpack[Ts], TypeVar) is True in Python < 3.12 Unpack would not raise here without the additional check and would be accepted as a type parameter.
for type_param in type_params:
if (not isinstance(type_param, (TypeVar, TypeVarTuple, ParamSpec))
or _is_unpack(type_param)
):
raise TypeError(f"Expected a type param, got {type_param!r}")
I've prepared this draft which contains two changes:
typing_extensions.TypeAliasTypenot raising TypeError if a non-tuple is passed compared to thetypingvariant.backport of
gh-124787: Fix
TypeAliasTypeand incorrecttype_paramscpython#124795 for <=3.11 (needs to be accepted first)Await merge and change if necessary
Afterwards fix correct python versions. I currently assume the backport is valid in 3.14.
Should backport for 3.12-13 be made? Yes.
It should maybe noted that for 3.12.0-3.12.7 the
typing.TypeAliasTypevariant does not contain this stricter backport.I do not think that there is a necessity to do a backport for 3.12 and it will likely do more harm (I did not test it).