@@ -142,6 +142,7 @@ def analyze_type_alias(
142142 global_scope : bool = True ,
143143 allowed_alias_tvars : list [TypeVarLikeType ] | None = None ,
144144 alias_type_params_names : list [str ] | None = None ,
145+ python_3_12_type_alias : bool = False ,
145146) -> tuple [Type , set [str ]]:
146147 """Analyze r.h.s. of a (potential) type alias definition.
147148
@@ -160,6 +161,7 @@ def analyze_type_alias(
160161 prohibit_self_type = "type alias target" ,
161162 allowed_alias_tvars = allowed_alias_tvars ,
162163 alias_type_params_names = alias_type_params_names ,
164+ python_3_12_type_alias = python_3_12_type_alias ,
163165 )
164166 analyzer .in_dynamic_func = in_dynamic_func
165167 analyzer .global_scope = global_scope
@@ -202,6 +204,7 @@ def __init__(
202204 is_typeshed_stub : bool ,
203205 * ,
204206 defining_alias : bool = False ,
207+ python_3_12_type_alias : bool = False ,
205208 allow_tuple_literal : bool = False ,
206209 allow_unbound_tvars : bool = False ,
207210 allow_placeholder : bool = False ,
@@ -220,6 +223,7 @@ def __init__(
220223 self .tvar_scope = tvar_scope
221224 # Are we analysing a type alias definition rvalue?
222225 self .defining_alias = defining_alias
226+ self .python_3_12_type_alias = python_3_12_type_alias
223227 self .allow_tuple_literal = allow_tuple_literal
224228 # Positive if we are analyzing arguments of another (outer) type
225229 self .nesting_level = 0
@@ -364,7 +368,12 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool)
364368 and (tvar_def is None or tvar_def not in self .allowed_alias_tvars )
365369 ):
366370 if self .not_declared_in_type_params (t .name ):
367- msg = f'Type variable "{ t .name } " is not included in type_params'
371+ if self .python_3_12_type_alias :
372+ msg = message_registry .TYPE_PARAMETERS_SHOULD_BE_DECLARED .format (
373+ f'"{ t .name } "'
374+ )
375+ else :
376+ msg = f'Type variable "{ t .name } " is not included in type_params'
368377 else :
369378 msg = f'Can\' t use bound type variable "{ t .name } " to define generic alias'
370379 self .fail (msg , t , code = codes .VALID_TYPE )
@@ -393,7 +402,12 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool)
393402 if self .allow_unbound_tvars :
394403 return t
395404 if self .defining_alias and self .not_declared_in_type_params (t .name ):
396- msg = f'TypeVarTuple "{ t .name } " is not included in type_params'
405+ if self .python_3_12_type_alias :
406+ msg = message_registry .TYPE_PARAMETERS_SHOULD_BE_DECLARED .format (
407+ f'"{ t .name } "'
408+ )
409+ else :
410+ msg = f'TypeVarTuple "{ t .name } " is not included in type_params'
397411 else :
398412 msg = f'TypeVarTuple "{ t .name } " is unbound'
399413 self .fail (msg , t , code = codes .VALID_TYPE )
@@ -1309,11 +1323,13 @@ def analyze_callable_args_for_paramspec(
13091323 and self .not_declared_in_type_params (tvar_def .name )
13101324 and tvar_def not in self .allowed_alias_tvars
13111325 ):
1312- self .fail (
1313- f'ParamSpec "{ tvar_def .name } " is not included in type_params' ,
1314- callable_args ,
1315- code = codes .VALID_TYPE ,
1316- )
1326+ if self .python_3_12_type_alias :
1327+ msg = message_registry .TYPE_PARAMETERS_SHOULD_BE_DECLARED .format (
1328+ f'"{ tvar_def .name } "'
1329+ )
1330+ else :
1331+ msg = f'ParamSpec "{ tvar_def .name } " is not included in type_params'
1332+ self .fail (msg , callable_args , code = codes .VALID_TYPE )
13171333 return callable_with_ellipsis (
13181334 AnyType (TypeOfAny .special_form ), ret_type = ret_type , fallback = fallback
13191335 )
0 commit comments