@@ -881,6 +881,9 @@ class P(Protocol): pass
881881 class C (P ): pass
882882
883883 self .assertIsInstance (C (), C )
884+ with self .assertRaises (TypeError ):
885+ C (42 )
886+
884887 T = TypeVar ('T' )
885888
886889 class PG (Protocol [T ]): pass
@@ -895,6 +898,8 @@ class PG(Protocol[T]): pass
895898 class CG (PG [T ]): pass
896899
897900 self .assertIsInstance (CG [int ](), CG )
901+ with self .assertRaises (TypeError ):
902+ CG [int ](42 )
898903
899904 def test_cannot_instantiate_abstract (self ):
900905 @runtime_checkable
@@ -1322,6 +1327,37 @@ def __init__(self):
13221327
13231328 self .assertEqual (C [int ]().test , 'OK' )
13241329
1330+ class B :
1331+ def __init__ (self ):
1332+ self .test = 'OK'
1333+
1334+ class D1 (B , P [T ]):
1335+ pass
1336+
1337+ self .assertEqual (D1 [int ]().test , 'OK' )
1338+
1339+ class D2 (P [T ], B ):
1340+ pass
1341+
1342+ self .assertEqual (D2 [int ]().test , 'OK' )
1343+
1344+ def test_new_called (self ):
1345+ T = TypeVar ('T' )
1346+
1347+ class P (Protocol [T ]): pass
1348+
1349+ class C (P [T ]):
1350+ def __new__ (cls , * args ):
1351+ self = super ().__new__ (cls , * args )
1352+ self .test = 'OK'
1353+ return self
1354+
1355+ self .assertEqual (C [int ]().test , 'OK' )
1356+ with self .assertRaises (TypeError ):
1357+ C [int ](42 )
1358+ with self .assertRaises (TypeError ):
1359+ C [int ](a = 42 )
1360+
13251361 def test_protocols_bad_subscripts (self ):
13261362 T = TypeVar ('T' )
13271363 S = TypeVar ('S' )
0 commit comments