Skip to content

Commit edf7d19

Browse files
committed
Merge remote-tracking branch 'upstream/main' into ParamSpec/Generic-simple
2 parents a280a44 + 67c16e1 commit edf7d19

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
@@ -6294,6 +6294,10 @@ def test_typing_extensions_defers_when_possible(self):
62946294
'AsyncGenerator', 'ContextManager', 'AsyncContextManager',
62956295
'ParamSpec', 'TypeVar', 'TypeVarTuple', 'get_type_hints',
62966296
}
6297+
if sys.version_info < (3, 14):
6298+
exclude |= {
6299+
'TypeAliasType'
6300+
}
62976301
if not typing_extensions._PEP_728_IMPLEMENTED:
62986302
exclude |= {'TypedDict', 'is_typeddict'}
62996303
for item in typing_extensions.__all__:
@@ -7504,6 +7508,80 @@ def test_no_instance_subclassing(self):
75047508
class MyAlias(TypeAliasType):
75057509
pass
75067510

7511+
def test_type_var_compatibility(self):
7512+
# Regression test to assure compatibility with typing variants
7513+
typingT = typing.TypeVar('typingT')
7514+
T1 = TypeAliasType("TypingTypeVar", ..., type_params=(typingT,))
7515+
self.assertEqual(T1.__type_params__, (typingT,))
7516+
7517+
# Test typing_extensions backports
7518+
textT = TypeVar('textT')
7519+
T2 = TypeAliasType("TypingExtTypeVar", ..., type_params=(textT,))
7520+
self.assertEqual(T2.__type_params__, (textT,))
7521+
7522+
textP = ParamSpec("textP")
7523+
T3 = TypeAliasType("TypingExtParamSpec", ..., type_params=(textP,))
7524+
self.assertEqual(T3.__type_params__, (textP,))
7525+
7526+
textTs = TypeVarTuple("textTs")
7527+
T4 = TypeAliasType("TypingExtTypeVarTuple", ..., type_params=(textTs,))
7528+
self.assertEqual(T4.__type_params__, (textTs,))
7529+
7530+
@skipUnless(TYPING_3_10_0, "typing.ParamSpec is not available before 3.10")
7531+
def test_param_spec_compatibility(self):
7532+
# Regression test to assure compatibility with typing variant
7533+
typingP = typing.ParamSpec("typingP")
7534+
T5 = TypeAliasType("TypingParamSpec", ..., type_params=(typingP,))
7535+
self.assertEqual(T5.__type_params__, (typingP,))
7536+
7537+
@skipUnless(TYPING_3_12_0, "typing.TypeVarTuple is not available before 3.12")
7538+
def test_type_var_tuple_compatibility(self):
7539+
# Regression test to assure compatibility with typing variant
7540+
typingTs = typing.TypeVarTuple("typingTs")
7541+
T6 = TypeAliasType("TypingTypeVarTuple", ..., type_params=(typingTs,))
7542+
self.assertEqual(T6.__type_params__, (typingTs,))
7543+
7544+
def test_type_params_possibilities(self):
7545+
T = TypeVar('T')
7546+
# Test not a tuple
7547+
with self.assertRaisesRegex(TypeError, "type_params must be a tuple"):
7548+
TypeAliasType("InvalidTypeParams", List[T], type_params=[T])
7549+
7550+
# Test default order and other invalid inputs
7551+
T_default = TypeVar('T_default', default=int)
7552+
Ts = TypeVarTuple('Ts')
7553+
Ts_default = TypeVarTuple('Ts_default', default=Unpack[Tuple[str, int]])
7554+
P = ParamSpec('P')
7555+
P_default = ParamSpec('P_default', default=[str, int])
7556+
7557+
# NOTE: PEP 696 states: "TypeVars with defaults cannot immediately follow TypeVarTuples"
7558+
# this is currently not enforced for the type statement and is not tested.
7559+
# PEP 695: Double usage of the same name is also not enforced and not tested.
7560+
valid_cases = [
7561+
(T, P, Ts),
7562+
(T, Ts_default),
7563+
(P_default, T_default),
7564+
(P, T_default, Ts_default),
7565+
(T_default, P_default, Ts_default),
7566+
]
7567+
invalid_cases = [
7568+
((T_default, T), f"non-default type parameter '{T!r}' follows default"),
7569+
((P_default, P), f"non-default type parameter '{P!r}' follows default"),
7570+
((Ts_default, T), f"non-default type parameter '{T!r}' follows default"),
7571+
# Only type params are accepted
7572+
((1,), "Expected a type param, got 1"),
7573+
((str,), f"Expected a type param, got {str!r}"),
7574+
# Unpack is not a TypeVar but isinstance(Unpack[Ts], TypeVar) is True in Python < 3.12
7575+
((Unpack[Ts],), f"Expected a type param, got {re.escape(repr(Unpack[Ts]))}"),
7576+
]
7577+
7578+
for case in valid_cases:
7579+
with self.subTest(type_params=case):
7580+
TypeAliasType("OkCase", List[T], type_params=case)
7581+
for case, msg in invalid_cases:
7582+
with self.subTest(type_params=case):
7583+
with self.assertRaisesRegex(TypeError, msg):
7584+
TypeAliasType("InvalidCase", List[T], type_params=case)
75077585

75087586
class DocTests(BaseTestCase):
75097587
def test_annotation(self):

src/typing_extensions.py

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

35453545

3546-
if hasattr(typing, "TypeAliasType"):
3546+
if sys.version_info >= (3, 14):
35473547
TypeAliasType = typing.TypeAliasType
3548+
# 3.8-3.13
35483549
else:
35493550
def _is_unionable(obj):
35503551
"""Corresponds to is_unionable() in unionobject.c in CPython."""
@@ -3617,11 +3618,29 @@ class TypeAliasType:
36173618
def __init__(self, name: str, value, *, type_params=()):
36183619
if not isinstance(name, str):
36193620
raise TypeError("TypeAliasType name must be a string")
3621+
if not isinstance(type_params, tuple):
3622+
raise TypeError("type_params must be a tuple")
36203623
self.__value__ = value
36213624
self.__type_params__ = type_params
36223625

3626+
default_value_encountered = False
36233627
parameters = []
36243628
for type_param in type_params:
3629+
if (
3630+
not isinstance(type_param, (TypeVar, TypeVarTuple, ParamSpec))
3631+
# 3.8-3.11
3632+
# Unpack Backport passes isinstance(type_param, TypeVar)
3633+
or _is_unpack(type_param)
3634+
):
3635+
raise TypeError(f"Expected a type param, got {type_param!r}")
3636+
has_default = (
3637+
getattr(type_param, '__default__', NoDefault) is not NoDefault
3638+
)
3639+
if default_value_encountered and not has_default:
3640+
raise TypeError(f"non-default type parameter '{type_param!r}'"
3641+
" follows default type parameter")
3642+
if has_default:
3643+
default_value_encountered = True
36253644
if isinstance(type_param, TypeVarTuple):
36263645
parameters.extend(type_param)
36273646
else:

0 commit comments

Comments
 (0)