Skip to content

Commit 91075ff

Browse files
authored
Merge branch 'main' into get_type_hints
2 parents 5612f6f + 67c16e1 commit 91075ff

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
@@ -27,6 +27,9 @@ aliases that have a `Concatenate` special form as their argument.
2727
Patch by [Daraan](https://github.com/Daraan).
2828
- Fix error in subscription of `Unpack` aliases causing nested Unpacks
2929
to not be resolved correctly. Patch by [Daraan](https://github.com/Daraan).
30+
- Backport CPython PR [#124795](https://github.com/python/cpython/pull/124795):
31+
fix `TypeAliasType` not raising an error on non-tuple inputs for `type_params`.
32+
Patch by [Daraan](https://github.com/Daraan).
3033

3134
# Release 4.12.2 (June 7, 2024)
3235

src/test_typing_extensions.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6281,6 +6281,10 @@ def test_typing_extensions_defers_when_possible(self):
62816281
'AsyncGenerator', 'ContextManager', 'AsyncContextManager',
62826282
'ParamSpec', 'TypeVar', 'TypeVarTuple', 'get_type_hints',
62836283
}
6284+
if sys.version_info < (3, 14):
6285+
exclude |= {
6286+
'TypeAliasType'
6287+
}
62846288
if not typing_extensions._PEP_728_IMPLEMENTED:
62856289
exclude |= {'TypedDict', 'is_typeddict'}
62866290
for item in typing_extensions.__all__:
@@ -7491,6 +7495,80 @@ def test_no_instance_subclassing(self):
74917495
class MyAlias(TypeAliasType):
74927496
pass
74937497

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

74957573
class DocTests(BaseTestCase):
74967574
def test_annotation(self):

src/typing_extensions.py

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

36103610

3611-
if hasattr(typing, "TypeAliasType"):
3611+
if sys.version_info >= (3, 14):
36123612
TypeAliasType = typing.TypeAliasType
3613+
# 3.8-3.13
36133614
else:
36143615
def _is_unionable(obj):
36153616
"""Corresponds to is_unionable() in unionobject.c in CPython."""
@@ -3682,11 +3683,29 @@ class TypeAliasType:
36823683
def __init__(self, name: str, value, *, type_params=()):
36833684
if not isinstance(name, str):
36843685
raise TypeError("TypeAliasType name must be a string")
3686+
if not isinstance(type_params, tuple):
3687+
raise TypeError("type_params must be a tuple")
36853688
self.__value__ = value
36863689
self.__type_params__ = type_params
36873690

3691+
default_value_encountered = False
36883692
parameters = []
36893693
for type_param in type_params:
3694+
if (
3695+
not isinstance(type_param, (TypeVar, TypeVarTuple, ParamSpec))
3696+
# 3.8-3.11
3697+
# Unpack Backport passes isinstance(type_param, TypeVar)
3698+
or _is_unpack(type_param)
3699+
):
3700+
raise TypeError(f"Expected a type param, got {type_param!r}")
3701+
has_default = (
3702+
getattr(type_param, '__default__', NoDefault) is not NoDefault
3703+
)
3704+
if default_value_encountered and not has_default:
3705+
raise TypeError(f"non-default type parameter '{type_param!r}'"
3706+
" follows default type parameter")
3707+
if has_default:
3708+
default_value_encountered = True
36903709
if isinstance(type_param, TypeVarTuple):
36913710
parameters.extend(type_param)
36923711
else:

0 commit comments

Comments
 (0)