@@ -257,7 +257,7 @@ def _type_repr(obj):
257257 return repr (obj )
258258
259259
260- def _collect_parameters (args ):
260+ def _collect_parameters (args , * , enforce_default_ordering : bool = True ):
261261 """Collect all type variables and parameter specifications in args
262262 in order of first appearance (lexicographic order).
263263
@@ -286,15 +286,16 @@ def _collect_parameters(args):
286286 parameters .append (collected )
287287 elif hasattr (t , '__typing_subst__' ):
288288 if t not in parameters :
289- if type_var_tuple_encountered and t .has_default ():
290- raise TypeError ('Type parameter with a default'
291- ' follows TypeVarTuple' )
289+ if enforce_default_ordering :
290+ if type_var_tuple_encountered and t .has_default ():
291+ raise TypeError ('Type parameter with a default'
292+ ' follows TypeVarTuple' )
292293
293- if t .has_default ():
294- default_encountered = True
295- elif default_encountered :
296- raise TypeError (f'Type parameter { t !r} without a default'
297- ' follows type parameter with a default' )
294+ if t .has_default ():
295+ default_encountered = True
296+ elif default_encountered :
297+ raise TypeError (f'Type parameter { t !r} without a default'
298+ ' follows type parameter with a default' )
298299
299300 parameters .append (t )
300301 else :
@@ -1416,7 +1417,11 @@ def __init__(self, origin, args, *, inst=True, name=None):
14161417 args = (args ,)
14171418 self .__args__ = tuple (... if a is _TypingEllipsis else
14181419 a for a in args )
1419- self .__parameters__ = _collect_parameters (args )
1420+ enforce_default_ordering = origin in (Generic , Protocol )
1421+ self .__parameters__ = _collect_parameters (
1422+ args ,
1423+ enforce_default_ordering = enforce_default_ordering ,
1424+ )
14201425 if not name :
14211426 self .__module__ = origin .__module__
14221427
0 commit comments