Skip to content

Commit b2412b0

Browse files
committed
Adjusted error messages to match cpython
1 parent 4e2f041 commit b2412b0

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

src/test_typing_extensions.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7318,63 +7318,58 @@ def test_type_params_possibilities(self):
73187318
with self.assertRaisesRegex(TypeError, "type_params must be a tuple"):
73197319
TypeAliasType("InvalidTypeParams", List[T], type_params=[T])
73207320

7321-
# Regression test assure compatibility with typing.TypeVar
7321+
# Regression test to assure compatibility with typing.TypeVar
73227322
typing_T = typing.TypeVar('T')
73237323
with self.subTest(type_params="typing.TypeVar"):
73247324
TypeAliasType("TypingTypeParams", List[typing_T], type_params=(typing_T,))
73257325

7326-
# Test default order
7326+
# Test default order and other invalid inputs
73277327
T_default = TypeVar('T_default', default=int)
73287328
Ts = TypeVarTuple('Ts')
73297329
Ts_default = TypeVarTuple('Ts_default', default=Unpack[Tuple[str, int]])
73307330
P = ParamSpec('P')
73317331
P_default = ParamSpec('P_default', default=[str, int])
7332-
P_default2 = ParamSpec('P_default2', default=...)
7332+
P_default2 = ParamSpec('P_default2', default=P_default)
73337333

73347334
ok_cases = [
73357335
(T, T_default),
73367336
(T, Ts_default),
7337-
(T, T_default, Ts_default),
73387337
(T, P, Ts),
7339-
(T, P, Ts_default),
7338+
(P, T_default, Ts_default),
7339+
(Ts, P, Ts_default),
73407340
(T, P_default),
73417341
(T, P_default, T_default),
73427342
(T, P_default, Ts_default),
73437343
(T, Ts_default, P_default),
73447344
(T, P_default, P_default2),
73457345
]
73467346
invalid_cases = [
7347-
(T_default, T),
7348-
(Ts_default, T),
7349-
7350-
# TypeVar after TypeVarTuple
7347+
((T_default, T), f"non-default type parameter {T!r} follows default"),
7348+
((P_default, P), f"non-default type parameter {P!r} follows default"),
7349+
((Ts_default, Ts), f"non-default type parameter {Ts!r} follows default"),
7350+
7351+
# Potentially add invalid inputs, e.g. literals or classes
7352+
# depends on upstream
7353+
((1,), "Expected a type param, got 1"),
7354+
((str,), f"Expected a type param, got {str!r}"),
7355+
73517356
# "TypeVars with defaults cannot immediately follow TypeVarTuples"
7357+
# is currently not enfored for the type statement, only for Generics
73527358
# (T, Ts, T_default),
73537359
# (T, Ts_default, T_default),
7354-
7355-
# Two TypeVarTuples in a row, should the reverse be also invalid?
7356-
(T, Ts_default, Ts),
7357-
7358-
(P_default, T),
7359-
(P_default, Ts),
7360-
7361-
# Double defintion
7360+
7361+
# Double use; however T2 = T; (T, T2) would likely be fine for a type checker
73627362
# (T, T)
73637363
# (Ts, *Ts)
73647364
# (P, **P)
7365-
7366-
# Potentially add invalid inputs, e.g. literals or classes
7367-
# depends on upstream
7368-
# (1,)
7369-
# (str,)
73707365
]
73717366

73727367
for case in ok_cases:
73737368
with self.subTest(type_params=case):
73747369
TypeAliasType("OkCase", List[T], type_params=case)
7375-
for case in invalid_cases:
7370+
for case, msg in invalid_cases:
73767371
with self.subTest(type_params=case):
7377-
with self.assertRaises(TypeError):
7372+
with self.assertRaisesRegex(TypeError, msg):
73787373
TypeAliasType("InvalidCase", List[T], type_params=case)
73797374

73807375
class DocTests(BaseTestCase):

src/typing_extensions.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3541,10 +3541,12 @@ def __init__(self, name: str, value, *, type_params=()):
35413541
for type_param in type_params:
35423542
if not isinstance(type_param, (TypeVar, TypeVarTuple, ParamSpec)):
35433543
raise TypeError(f"Expected a type param, got {type_param!r}")
3544-
has_default = getattr(type_param, '__default__', NoDefault) is not NoDefault
3544+
has_default = (
3545+
getattr(type_param, '__default__', NoDefault) is not NoDefault
3546+
)
35453547
if default_value_encountered and not has_default:
3546-
raise TypeError(f'Type parameter {type_param!r} without a default'
3547-
' follows type parameter with a default')
3548+
raise TypeError(f'non-default type parameter {type_param!r}'
3549+
' follows default type parameter')
35483550
if has_default:
35493551
default_value_encountered = True
35503552
if isinstance(type_param, TypeVarTuple):

0 commit comments

Comments
 (0)