|
11 | 11 |
|
12 | 12 | from typing import Any, NoReturn, Never, assert_never |
13 | 13 | from typing import TypeVar, TypeVarTuple, Unpack, AnyStr |
14 | | -from typing import _determine_typevar_substitution |
15 | 14 | from typing import T, KT, VT # Not in __all__. |
16 | 15 | from typing import Union, Optional, Literal |
17 | 16 | from typing import Tuple, List, Dict, MutableMapping |
@@ -479,45 +478,48 @@ class A(Generic[Unpack[Ts]]): pass |
479 | 478 |
|
480 | 479 | B = A[Unpack[Ts]] |
481 | 480 | 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] |
485 | 487 |
|
486 | 488 | C = A[Unpack[Ts], int] |
487 | 489 | 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] |
491 | 496 |
|
492 | 497 | D = A[int, Unpack[Ts]] |
493 | 498 | 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] |
497 | 505 |
|
498 | 506 | E = A[int, Unpack[Ts], str] |
499 | 507 | 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] |
503 | 514 |
|
504 | 515 | F = A[Unpack[Ts], Unpack[tuple[str, ...]]] |
505 | 516 | self.assertTrue(repr(F).endswith('A[*Ts, *tuple[str, ...]]')) |
506 | | - self.assertTrue(repr( |
| 517 | + with self.assertRaises(NotImplementedError): |
507 | 518 | F[()] |
508 | | - ).endswith( |
509 | | - 'A[*tuple[str, ...]]') |
510 | | - ) |
511 | | - self.assertTrue(repr( |
| 519 | + with self.assertRaises(NotImplementedError): |
512 | 520 | F[float] |
513 | | - ).endswith( |
514 | | - 'A[float, *tuple[str, ...]]' |
515 | | - )) |
516 | | - self.assertTrue(repr( |
| 521 | + with self.assertRaises(NotImplementedError): |
517 | 522 | F[float, int] |
518 | | - ).endswith( |
519 | | - 'A[float, int, *tuple[str, ...]]' |
520 | | - )) |
521 | 523 |
|
522 | 524 | def test_cannot_subclass_class(self): |
523 | 525 | with self.assertRaises(TypeError): |
@@ -791,132 +793,6 @@ class C(Generic[Unpack[Ts]]): pass |
791 | 793 | Ts2 = TypeVarTuple('Ts2') |
792 | 794 | self.assertNotEqual(C[Unpack[Ts1]], C[Unpack[Ts2]]) |
793 | 795 |
|
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 | | - |
920 | 796 |
|
921 | 797 | class UnionTests(BaseTestCase): |
922 | 798 |
|
|
0 commit comments