Skip to content

Commit f65e885

Browse files
committed
Merge remote-tracking branch 'upstream/main' into 3.14/evaluate_forward_ref
2 parents 4521d21 + 67c16e1 commit f65e885

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

.github/workflows/third_party.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,5 +412,5 @@ jobs:
412412
owner: "python",
413413
repo: "typing_extensions",
414414
title: `Third-party tests failed on ${new Date().toDateString()}`,
415-
body: "Runs listed here: https://github.com/python/typing_extensions/actions/workflows/third_party.yml",
415+
body: "Full history of runs listed here: https://github.com/python/typing_extensions/actions/workflows/third_party.yml",
416416
})

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ aliases that have a `Concatenate` special form as their argument.
2323
`Ellipsis` as an argument. Patch by [Daraan](https://github.com/Daraan).
2424
- Fix error in subscription of `Unpack` aliases causing nested Unpacks
2525
to not be resolved correctly. Patch by [Daraan](https://github.com/Daraan).
26+
- Backport CPython PR [#124795](https://github.com/python/cpython/pull/124795):
27+
fix `TypeAliasType` not raising an error on non-tuple inputs for `type_params`.
28+
Patch by [Daraan](https://github.com/Daraan).
2629

2730
# Release 4.12.2 (June 7, 2024)
2831

src/test_typing_extensions.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6194,6 +6194,10 @@ def test_typing_extensions_defers_when_possible(self):
61946194
'AsyncGenerator', 'ContextManager', 'AsyncContextManager',
61956195
'ParamSpec', 'TypeVar', 'TypeVarTuple', 'get_type_hints',
61966196
}
6197+
if sys.version_info < (3, 14):
6198+
exclude |= {
6199+
'TypeAliasType'
6200+
}
61976201
if not typing_extensions._PEP_728_IMPLEMENTED:
61986202
exclude |= {'TypedDict', 'is_typeddict'}
61996203
for item in typing_extensions.__all__:
@@ -7404,6 +7408,80 @@ def test_no_instance_subclassing(self):
74047408
class MyAlias(TypeAliasType):
74057409
pass
74067410

7411+
def test_type_var_compatibility(self):
7412+
# Regression test to assure compatibility with typing variants
7413+
typingT = typing.TypeVar('typingT')
7414+
T1 = TypeAliasType("TypingTypeVar", ..., type_params=(typingT,))
7415+
self.assertEqual(T1.__type_params__, (typingT,))
7416+
7417+
# Test typing_extensions backports
7418+
textT = TypeVar('textT')
7419+
T2 = TypeAliasType("TypingExtTypeVar", ..., type_params=(textT,))
7420+
self.assertEqual(T2.__type_params__, (textT,))
7421+
7422+
textP = ParamSpec("textP")
7423+
T3 = TypeAliasType("TypingExtParamSpec", ..., type_params=(textP,))
7424+
self.assertEqual(T3.__type_params__, (textP,))
7425+
7426+
textTs = TypeVarTuple("textTs")
7427+
T4 = TypeAliasType("TypingExtTypeVarTuple", ..., type_params=(textTs,))
7428+
self.assertEqual(T4.__type_params__, (textTs,))
7429+
7430+
@skipUnless(TYPING_3_10_0, "typing.ParamSpec is not available before 3.10")
7431+
def test_param_spec_compatibility(self):
7432+
# Regression test to assure compatibility with typing variant
7433+
typingP = typing.ParamSpec("typingP")
7434+
T5 = TypeAliasType("TypingParamSpec", ..., type_params=(typingP,))
7435+
self.assertEqual(T5.__type_params__, (typingP,))
7436+
7437+
@skipUnless(TYPING_3_12_0, "typing.TypeVarTuple is not available before 3.12")
7438+
def test_type_var_tuple_compatibility(self):
7439+
# Regression test to assure compatibility with typing variant
7440+
typingTs = typing.TypeVarTuple("typingTs")
7441+
T6 = TypeAliasType("TypingTypeVarTuple", ..., type_params=(typingTs,))
7442+
self.assertEqual(T6.__type_params__, (typingTs,))
7443+
7444+
def test_type_params_possibilities(self):
7445+
T = TypeVar('T')
7446+
# Test not a tuple
7447+
with self.assertRaisesRegex(TypeError, "type_params must be a tuple"):
7448+
TypeAliasType("InvalidTypeParams", List[T], type_params=[T])
7449+
7450+
# Test default order and other invalid inputs
7451+
T_default = TypeVar('T_default', default=int)
7452+
Ts = TypeVarTuple('Ts')
7453+
Ts_default = TypeVarTuple('Ts_default', default=Unpack[Tuple[str, int]])
7454+
P = ParamSpec('P')
7455+
P_default = ParamSpec('P_default', default=[str, int])
7456+
7457+
# NOTE: PEP 696 states: "TypeVars with defaults cannot immediately follow TypeVarTuples"
7458+
# this is currently not enforced for the type statement and is not tested.
7459+
# PEP 695: Double usage of the same name is also not enforced and not tested.
7460+
valid_cases = [
7461+
(T, P, Ts),
7462+
(T, Ts_default),
7463+
(P_default, T_default),
7464+
(P, T_default, Ts_default),
7465+
(T_default, P_default, Ts_default),
7466+
]
7467+
invalid_cases = [
7468+
((T_default, T), f"non-default type parameter '{T!r}' follows default"),
7469+
((P_default, P), f"non-default type parameter '{P!r}' follows default"),
7470+
((Ts_default, T), f"non-default type parameter '{T!r}' follows default"),
7471+
# Only type params are accepted
7472+
((1,), "Expected a type param, got 1"),
7473+
((str,), f"Expected a type param, got {str!r}"),
7474+
# Unpack is not a TypeVar but isinstance(Unpack[Ts], TypeVar) is True in Python < 3.12
7475+
((Unpack[Ts],), f"Expected a type param, got {re.escape(repr(Unpack[Ts]))}"),
7476+
]
7477+
7478+
for case in valid_cases:
7479+
with self.subTest(type_params=case):
7480+
TypeAliasType("OkCase", List[T], type_params=case)
7481+
for case, msg in invalid_cases:
7482+
with self.subTest(type_params=case):
7483+
with self.assertRaisesRegex(TypeError, msg):
7484+
TypeAliasType("InvalidCase", List[T], type_params=case)
74077485

74087486
class DocTests(BaseTestCase):
74097487
def test_annotation(self):

src/typing_extensions.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3534,8 +3534,9 @@ def __ror__(self, other):
35343534
return typing.Union[other, self]
35353535

35363536

3537-
if hasattr(typing, "TypeAliasType"):
3537+
if sys.version_info >= (3, 14):
35383538
TypeAliasType = typing.TypeAliasType
3539+
# 3.8-3.13
35393540
else:
35403541
def _is_unionable(obj):
35413542
"""Corresponds to is_unionable() in unionobject.c in CPython."""
@@ -3608,11 +3609,29 @@ class TypeAliasType:
36083609
def __init__(self, name: str, value, *, type_params=()):
36093610
if not isinstance(name, str):
36103611
raise TypeError("TypeAliasType name must be a string")
3612+
if not isinstance(type_params, tuple):
3613+
raise TypeError("type_params must be a tuple")
36113614
self.__value__ = value
36123615
self.__type_params__ = type_params
36133616

3617+
default_value_encountered = False
36143618
parameters = []
36153619
for type_param in type_params:
3620+
if (
3621+
not isinstance(type_param, (TypeVar, TypeVarTuple, ParamSpec))
3622+
# 3.8-3.11
3623+
# Unpack Backport passes isinstance(type_param, TypeVar)
3624+
or _is_unpack(type_param)
3625+
):
3626+
raise TypeError(f"Expected a type param, got {type_param!r}")
3627+
has_default = (
3628+
getattr(type_param, '__default__', NoDefault) is not NoDefault
3629+
)
3630+
if default_value_encountered and not has_default:
3631+
raise TypeError(f"non-default type parameter '{type_param!r}'"
3632+
" follows default type parameter")
3633+
if has_default:
3634+
default_value_encountered = True
36163635
if isinstance(type_param, TypeVarTuple):
36173636
parameters.extend(type_param)
36183637
else:

0 commit comments

Comments
 (0)