Open AlexWaygood opened 5 months ago
Actually it looks like this is pretty doable
https://github.com/python/typing_extensions/pull/397 implements the correct behaviour on Python >=3.11.1. I don't know if it's feasible to backport it on earlier versions of Python, or if we should just document that it's a known limitation that __args__
may be incorrect on lower versions of Python if you're using typevarlikes with defaults.
I think we can leave this open in case someone wants to add support. We have some similar open issues around edge cases with ParamSpec.
We've fixed a number of issues with our PEP-696 backport in recent days, and we're now a lot closer to CPython's implementation on Python 3.13. We're still missing 5 specialisation-related tests from CPython's test suite, however, and they all fail using the
typing_extensions
implementation on Python 3.12:Missing tests
```diff diff --git a/src/test_typing_extensions.py b/src/test_typing_extensions.py index fa04e59..0ab1e7f 100644 --- a/src/test_typing_extensions.py +++ b/src/test_typing_extensions.py @@ -6402,6 +6402,26 @@ class TypeVarLikeDefaultsTests(BaseTestCase): class A(Generic[Unpack[Ts]]): ... Alias = Optional[Unpack[Ts]] + def test_typevartuple_specialization(self): + T = TypeVar("T") + Ts = TypeVarTuple('Ts', default=Unpack[Tuple[str, int]]) + self.assertEqual(Ts.__default__, Unpack[Tuple[str, int]]) + class A(Generic[T, Unpack[Ts]]): ... + self.assertEqual(A[float].__args__, (float, str, int)) + self.assertEqual(A[float, range].__args__, (float, range)) + self.assertEqual(A[float, *tuple[int, ...]].__args__, (float, *tuple[int, ...])) + + def test_typevar_and_typevartuple_specialization(self): + T = TypeVar("T") + U = TypeVar("U", default=float) + Ts = TypeVarTuple('Ts', default=Unpack[Tuple[str, int]]) + self.assertEqual(Ts.__default__, Unpack[Tuple[str, int]]) + class A(Generic[T, U, Unpack[Ts]]): ... + self.assertEqual(A[int].__args__, (int, float, str, int)) + self.assertEqual(A[int, str].__args__, (int, str, str, int)) + self.assertEqual(A[int, str, range].__args__, (int, str, range)) + self.assertEqual(A[int, str, *tuple[int, ...]].__args__, (int, str, *tuple[int, ...])) + def test_no_default_after_typevar_tuple(self): T = TypeVar("T", default=int) Ts = TypeVarTuple("Ts") @@ -6487,6 +6507,34 @@ class TypeVarLikeDefaultsTests(BaseTestCase): a4 = Callable[[Unpack[Ts]], T] self.assertEqual(a4.__args__, (Unpack[Ts], T)) + def test_paramspec_specialization(self): + T = TypeVar("T") + P = ParamSpec('P', default=[str, int]) + self.assertEqual(P.__default__, [str, int]) + class A(Generic[T, P]): ... + self.assertEqual(A[float].__args__, (float, (str, int))) + self.assertEqual(A[float, [range]].__args__, (float, (range,))) + + def test_typevar_and_paramspec_specialization(self): + T = TypeVar("T") + U = TypeVar("U", default=float) + P = ParamSpec('P', default=[str, int]) + self.assertEqual(P.__default__, [str, int]) + class A(Generic[T, U, P]): ... + self.assertEqual(A[float].__args__, (float, float, (str, int))) + self.assertEqual(A[float, int].__args__, (float, int, (str, int))) + self.assertEqual(A[float, int, [range]].__args__, (float, int, (range,))) + + def test_paramspec_and_typevar_specialization(self): + T = TypeVar("T") + P = ParamSpec('P', default=[str, int]) + U = TypeVar("U", default=float) + self.assertEqual(P.__default__, [str, int]) + class A(Generic[T, P, U]): ... + self.assertEqual(A[float].__args__, (float, (str, int), float)) + self.assertEqual(A[float, [range]].__args__, (float, (range,), float)) + self.assertEqual(A[float, [range], int].__args__, (float, (range,), int)) ```Here are the test failures if I add those tests to our suite:
I don't know if it's going to be possible to fix this easily in
typing_extensions
, or if we should try to do so before the next release.