@@ -3958,6 +3958,7 @@ class C: pass
39583958
39593959 def test_defining_generic_protocols (self ):
39603960 T = TypeVar ('T' )
3961+ T2 = TypeVar ('T2' )
39613962 S = TypeVar ('S' )
39623963
39633964 @runtime_checkable
@@ -3967,17 +3968,26 @@ def meth(self): pass
39673968 class P (PR [int , T ], Protocol [T ]):
39683969 y = 1
39693970
3971+ self .assertEqual (P .__parameters__ , (T ,))
3972+
39703973 with self .assertRaises (TypeError ):
39713974 PR [int ]
39723975 with self .assertRaises (TypeError ):
39733976 P [int , str ]
3977+ with self .assertRaisesRegex (
3978+ TypeError ,
3979+ re .escape ('Some type variables (~S) are not listed in Protocol[~T, ~T2]' ),
3980+ ):
3981+ class ExtraTypeVars (P [S ], Protocol [T , T2 ]): ...
39743982
39753983 class C (PR [int , T ]): pass
39763984
3985+ self .assertEqual (C .__parameters__ , (T ,))
39773986 self .assertIsInstance (C [str ](), C )
39783987
39793988 def test_defining_generic_protocols_old_style (self ):
39803989 T = TypeVar ('T' )
3990+ T2 = TypeVar ('T2' )
39813991 S = TypeVar ('S' )
39823992
39833993 @runtime_checkable
@@ -3996,9 +4006,19 @@ class P(PR[int, str], Protocol):
39964006 class P1 (Protocol , Generic [T ]):
39974007 def bar (self , x : T ) -> str : ...
39984008
4009+ self .assertEqual (P1 .__parameters__ , (T ,))
4010+
39994011 class P2 (Generic [T ], Protocol ):
40004012 def bar (self , x : T ) -> str : ...
40014013
4014+ self .assertEqual (P2 .__parameters__ , (T ,))
4015+
4016+ msg = re .escape ('Some type variables (~S) are not listed in Protocol[~T, ~T2]' )
4017+ with self .assertRaisesRegex (TypeError , msg ):
4018+ class ExtraTypeVars (P1 [S ], Protocol [T , T2 ]): ...
4019+ with self .assertRaisesRegex (TypeError , msg ):
4020+ class ExtraTypeVars (P2 [S ], Protocol [T , T2 ]): ...
4021+
40024022 @runtime_checkable
40034023 class PSub (P1 [str ], Protocol ):
40044024 x = 1
@@ -4011,6 +4031,28 @@ def bar(self, x: str) -> str:
40114031
40124032 self .assertIsInstance (Test (), PSub )
40134033
4034+ def test_protocol_parameter_order (self ):
4035+ # https://github.com/python/cpython/issues/137191
4036+ T1 = TypeVar ("T1" )
4037+ T2 = TypeVar ("T2" , default = object )
4038+
4039+ class A (Protocol [T1 ]): ...
4040+
4041+ class B0 (A [T2 ], Generic [T1 , T2 ]): ...
4042+ self .assertEqual (B0 .__parameters__ , (T1 , T2 ))
4043+
4044+ class B1 (A [T2 ], Protocol , Generic [T1 , T2 ]): ...
4045+ self .assertEqual (B1 .__parameters__ , (T1 , T2 ))
4046+
4047+ class B2 (A [T2 ], Protocol [T1 , T2 ]): ...
4048+ self .assertEqual (B2 .__parameters__ , (T1 , T2 ))
4049+
4050+ class B3 [T1 , T2 ](A [T2 ], Protocol ):
4051+ @staticmethod
4052+ def get_typeparams ():
4053+ return (T1 , T2 )
4054+ self .assertEqual (B3 .__parameters__ , B3 .get_typeparams ())
4055+
40144056 def test_pep695_generic_protocol_callable_members (self ):
40154057 @runtime_checkable
40164058 class Foo [T ](Protocol ):
0 commit comments