Skip to content

Commit b9b1c80

Browse files
committed
Cut out variadic type substitution logic
1 parent f094905 commit b9b1c80

File tree

2 files changed

+32
-366
lines changed

2 files changed

+32
-366
lines changed

Lib/test/test_typing.py

+27-151
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
from typing import Any, NoReturn, Never, assert_never
1313
from typing import TypeVar, TypeVarTuple, Unpack, AnyStr
14-
from typing import _determine_typevar_substitution
1514
from typing import T, KT, VT # Not in __all__.
1615
from typing import Union, Optional, Literal
1716
from typing import Tuple, List, Dict, MutableMapping
@@ -479,45 +478,48 @@ class A(Generic[Unpack[Ts]]): pass
479478

480479
B = A[Unpack[Ts]]
481480
self.assertTrue(repr(B).endswith('A[*Ts]'))
482-
self.assertTrue(repr(B[()]).endswith('A[()]'))
483-
self.assertTrue(repr(B[float]).endswith('A[float]'))
484-
self.assertTrue(repr(B[float, str]).endswith('A[float, str]'))
481+
with self.assertRaises(NotImplementedError):
482+
B[()]
483+
with self.assertRaises(NotImplementedError):
484+
B[float]
485+
with self.assertRaises(NotImplementedError):
486+
B[float, str]
485487

486488
C = A[Unpack[Ts], int]
487489
self.assertTrue(repr(C).endswith('A[*Ts, int]'))
488-
self.assertTrue(repr(C[()]).endswith('A[int]'))
489-
self.assertTrue(repr(C[float]).endswith('A[float, int]'))
490-
self.assertTrue(repr(C[float, str]).endswith('A[float, str, int]'))
490+
with self.assertRaises(NotImplementedError):
491+
C[()]
492+
with self.assertRaises(NotImplementedError):
493+
C[float]
494+
with self.assertRaises(NotImplementedError):
495+
C[float, str]
491496

492497
D = A[int, Unpack[Ts]]
493498
self.assertTrue(repr(D).endswith('A[int, *Ts]'))
494-
self.assertTrue(repr(D[()]).endswith('A[int]'))
495-
self.assertTrue(repr(D[float]).endswith('A[int, float]'))
496-
self.assertTrue(repr(D[float, str]).endswith('A[int, float, str]'))
499+
with self.assertRaises(NotImplementedError):
500+
D[()]
501+
with self.assertRaises(NotImplementedError):
502+
D[float]
503+
with self.assertRaises(NotImplementedError):
504+
D[float, str]
497505

498506
E = A[int, Unpack[Ts], str]
499507
self.assertTrue(repr(E).endswith('A[int, *Ts, str]'))
500-
self.assertTrue(repr(E[()]).endswith('A[int, str]'))
501-
self.assertTrue(repr(E[float]).endswith('A[int, float, str]'))
502-
self.assertTrue(repr(E[float, bool]).endswith('A[int, float, bool, str]'))
508+
with self.assertRaises(NotImplementedError):
509+
E[()]
510+
with self.assertRaises(NotImplementedError):
511+
E[float]
512+
with self.assertRaises(NotImplementedError):
513+
E[float, bool]
503514

504515
F = A[Unpack[Ts], Unpack[tuple[str, ...]]]
505516
self.assertTrue(repr(F).endswith('A[*Ts, *tuple[str, ...]]'))
506-
self.assertTrue(repr(
517+
with self.assertRaises(NotImplementedError):
507518
F[()]
508-
).endswith(
509-
'A[*tuple[str, ...]]')
510-
)
511-
self.assertTrue(repr(
519+
with self.assertRaises(NotImplementedError):
512520
F[float]
513-
).endswith(
514-
'A[float, *tuple[str, ...]]'
515-
))
516-
self.assertTrue(repr(
521+
with self.assertRaises(NotImplementedError):
517522
F[float, int]
518-
).endswith(
519-
'A[float, int, *tuple[str, ...]]'
520-
))
521523

522524
def test_cannot_subclass_class(self):
523525
with self.assertRaises(TypeError):
@@ -791,132 +793,6 @@ class C(Generic[Unpack[Ts]]): pass
791793
Ts2 = TypeVarTuple('Ts2')
792794
self.assertNotEqual(C[Unpack[Ts1]], C[Unpack[Ts2]])
793795

794-
def test_typevar_substitution(self):
795-
T1 = TypeVar('T1')
796-
T2 = TypeVar('T2')
797-
Ts = TypeVarTuple('Ts')
798-
799-
# Cases which should generate a TypeError.
800-
# These are tuples of (typevars, args) arguments to
801-
# _determine_typevar_substitution..
802-
test_cases = [
803-
# Too few args
804-
805-
# One TypeVar: if (potentially) 0 args
806-
((T1,), ()),
807-
((T1,), (Unpack[tuple[()]],)),
808-
((T1,), (Unpack[tuple[int, ...]],)),
809-
((T1,), (Unpack[tuple[int, ...]], Unpack[tuple[int, ...]])),
810-
# Two TypeVars: if (potentially) <= 1 args
811-
((T1, T2), (int,)),
812-
((T1, T2), (Unpack[tuple[int]],)),
813-
((T1, T2), (Unpack[tuple[int, ...]],)),
814-
((T1, T2), (Unpack[tuple[int, ...]], Unpack[tuple[int, ...]])),
815-
((T1, T2), (Unpack[tuple[int, ...]], Unpack[tuple[int, ...]], Unpack[tuple[int, ...]])),
816-
# One TypeVarTuple and one TypeVar: if (potentially) 0 args
817-
# TypeVarTuple first
818-
((Ts, T1), ()),
819-
((Ts, T1), (Unpack[tuple[()]],)),
820-
((Ts, T1), (Unpack[tuple[int, ...]],)),
821-
((Ts, T1), (Unpack[tuple[int, ...]], Unpack[tuple[int, ...]])),
822-
((Ts, T1), (Unpack[tuple[int, ...]], Unpack[tuple[int, ...]], Unpack[tuple[int, ...]])),
823-
# TypeVarTuple last
824-
((T1, Ts), ()),
825-
((T1, Ts), (Unpack[tuple[()]],)),
826-
((T1, Ts), (Unpack[tuple[int, ...]],)),
827-
((T1, Ts), (Unpack[tuple[int, ...]], Unpack[tuple[int, ...]])),
828-
((T1, Ts), (Unpack[tuple[int, ...]], Unpack[tuple[int, ...]], Unpack[tuple[int, ...]])),
829-
# OneTypeVarTuple and two TypeVars: if (potentially) <= 1 args
830-
# TypeVarTuple first
831-
((Ts, T1, T2), ()),
832-
((Ts, T1, T2), (int,)),
833-
((Ts, T1, T2), (Unpack[tuple[int]],)),
834-
((Ts, T1, T2), (Unpack[tuple[int, ...]],)),
835-
((Ts, T1, T2), (Unpack[tuple[int, ...]], Unpack[tuple[int, ...]])),
836-
((Ts, T1, T2), (Unpack[tuple[int, ...]], Unpack[tuple[int, ...]], Unpack[tuple[int, ...]])),
837-
((Ts, T1, T2), (Unpack[tuple[int, ...]], Unpack[tuple[int, ...]], Unpack[tuple[int, ...]], Unpack[tuple[int, ...]])),
838-
# TypeVarTuple in middle
839-
((T1, Ts, T2), ()),
840-
((T1, Ts, T2), (int,)),
841-
((T1, Ts, T2), (Unpack[tuple[int]],)),
842-
((T1, Ts, T2), (Unpack[tuple[int, ...]],)),
843-
((T1, Ts, T2), (Unpack[tuple[int, ...]], Unpack[tuple[int, ...]])),
844-
((T1, Ts, T2), (Unpack[tuple[int, ...]], Unpack[tuple[int, ...]], Unpack[tuple[int, ...]])),
845-
((T1, Ts, T2), (Unpack[tuple[int, ...]], Unpack[tuple[int, ...]], Unpack[tuple[int, ...]], Unpack[tuple[int, ...]])),
846-
# TypeVarTuple last
847-
((T1, T2, Ts), ()),
848-
((T1, T2, Ts), (int,)),
849-
((T1, T2, Ts), (Unpack[tuple[int]],)),
850-
((T1, T2, Ts), (Unpack[tuple[int, ...]],)),
851-
((T1, T2, Ts), (Unpack[tuple[int, ...]], Unpack[tuple[int, ...]])),
852-
((T1, T2, Ts), (Unpack[tuple[int, ...]], Unpack[tuple[int, ...]], Unpack[tuple[int, ...]])),
853-
((T1, T2, Ts), (Unpack[tuple[int, ...]], Unpack[tuple[int, ...]], Unpack[tuple[int, ...]], Unpack[tuple[int, ...]])),
854-
855-
# Too many args
856-
857-
# One TypeVar: if (potentially) >= 2 args
858-
((T1,), (int, int)),
859-
((T1,), (Unpack[tuple[int, int]],)),
860-
((T1,), (Unpack[tuple[int]], Unpack[tuple[int]])),
861-
((T1,), (int, Unpack[tuple[int, ...]])),
862-
# Two TypeVars: if (potentially) >= 3 args
863-
((T1, T2), (int, int, int)),
864-
((T1, T2), (Unpack[tuple[int, int, int]],)),
865-
((T1, T2), (Unpack[tuple[int]], Unpack[tuple[int]], Unpack[tuple[int]])),
866-
((T1, T2), (int, int, Unpack[tuple[int, ...]],)),
867-
868-
# Too many TypeVarTuples
869-
870-
((Ts, Ts), ()),
871-
((Ts, Ts), (int,)),
872-
((Ts, Ts), (int, str)),
873-
]
874-
for typevars, args in test_cases:
875-
with self.subTest(f'typevars={typevars}, args={args}'):
876-
with self.assertRaises(TypeError):
877-
_determine_typevar_substitution(typevars, args)
878-
879-
# Cases which should succeed.
880-
# These are tuples of (typevars, args, expected_result).
881-
test_cases = [
882-
# Correct number of args, TypeVars only
883-
((T1,), (int,), {T1: int}),
884-
((T1,), (Unpack[tuple[int]],), {T1: int}),
885-
((T1, T2), (int, str), {T1: int, T2: str}),
886-
((T1, T2), (Unpack[tuple[int, str]],), {T1: int, T2: str}),
887-
# Correct number of args, TypeVarTuple only
888-
((Ts,), (), {Ts: ()}),
889-
((Ts,), (int,), {Ts: (int,)}),
890-
((Ts,), (Unpack[tuple[int]],), {Ts: (int,)}),
891-
((Ts,), (int, str), {Ts: (int, str)}),
892-
((Ts,), (Unpack[tuple[int, ...]],), {Ts: (Unpack[tuple[int, ...]],)}),
893-
# Correct number of args, TypeVarTuple at the beginning
894-
((Ts, T1), (int,), {Ts: (), T1: int}),
895-
((Ts, T1), (int, str), {Ts: (int,), T1: str}),
896-
((Ts, T1), (int, str, float), {Ts: (int, str), T1: float}),
897-
((Ts, T1), (Unpack[tuple[int, ...]], str), {Ts: (Unpack[tuple[int, ...]],), T1: str}),
898-
((Ts, T1), (Unpack[tuple[int, ...]], str, bool), {Ts: (Unpack[tuple[int, ...]], str), T1: bool}),
899-
# Correct number of args, TypeVarTuple at the end
900-
((T1, Ts), (int,), {T1: int, Ts: ()}),
901-
((T1, Ts), (int, str), {T1: int, Ts: (str,)}),
902-
((T1, Ts), (int, str, float), {T1: int, Ts: (str, float)}),
903-
((T1, Ts), (int, Unpack[tuple[str, ...]]), {T1: int, Ts: (Unpack[tuple[str, ...]],)}),
904-
((T1, Ts), (int, str, Unpack[tuple[float, ...]]), {T1: int, Ts: (str, Unpack[tuple[float, ...]],)}),
905-
# Correct number of args, TypeVarTuple in the middle
906-
((T1, Ts, T2), (int, str), {T1: int, Ts: (), T2: str}),
907-
((T1, Ts, T2), (int, float, str), {T1: int, Ts: (float,), T2: str}),
908-
((T1, Ts, T2), (int, Unpack[tuple[int, ...]], str), {T1: int, Ts: (Unpack[tuple[int, ...]],), T2: str}),
909-
((T1, Ts, T2), (int, float, Unpack[tuple[bool, ...]], str), {T1: int, Ts: (float, Unpack[tuple[bool, ...]],), T2: str}),
910-
((T1, Ts, T2), (int, Unpack[tuple[bool, ...]], float, str), {T1: int, Ts: (Unpack[tuple[bool, ...]], float), T2: str}),
911-
((T1, Ts, T2), (int, complex, Unpack[tuple[bool, ...]], float, str), {T1: int, Ts: (complex, Unpack[tuple[bool, ...]], float), T2: str}),
912-
]
913-
for typevars, args, result_or_exception in test_cases:
914-
with self.subTest(f'typevars={typevars}, args={args}'):
915-
self.assertEqual(
916-
_determine_typevar_substitution(typevars, args),
917-
result_or_exception
918-
)
919-
920796

921797
class UnionTests(BaseTestCase):
922798

0 commit comments

Comments
 (0)