@@ -226,6 +226,7 @@ def __init__(
226
226
allow_unbound_tvars : bool = False ,
227
227
allow_placeholder : bool = False ,
228
228
allow_typed_dict_special_forms : bool = False ,
229
+ allow_final : bool = True ,
229
230
allow_param_spec_literals : bool = False ,
230
231
allow_unpack : bool = False ,
231
232
report_invalid_types : bool = True ,
@@ -261,6 +262,8 @@ def __init__(
261
262
self .allow_placeholder = allow_placeholder
262
263
# Are we in a context where Required[] is allowed?
263
264
self .allow_typed_dict_special_forms = allow_typed_dict_special_forms
265
+ # Set True when we analyze ClassVar else False
266
+ self .allow_final = allow_final
264
267
# Are we in a context where ParamSpec literals are allowed?
265
268
self .allow_param_spec_literals = allow_param_spec_literals
266
269
# Are we in context where literal "..." specifically is allowed?
@@ -607,11 +610,12 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ
607
610
code = codes .VALID_TYPE ,
608
611
)
609
612
else :
610
- self .fail (
611
- "Final can be only used as an outermost qualifier in a variable annotation" ,
612
- t ,
613
- code = codes .VALID_TYPE ,
614
- )
613
+ if not self .allow_final :
614
+ self .fail (
615
+ "Final can be only used as an outermost qualifier in a variable annotation" ,
616
+ t ,
617
+ code = codes .VALID_TYPE ,
618
+ )
615
619
return AnyType (TypeOfAny .from_error )
616
620
elif fullname == "typing.Tuple" or (
617
621
fullname == "builtins.tuple"
@@ -692,7 +696,7 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ
692
696
"ClassVar[...] must have at most one type argument" , t , code = codes .VALID_TYPE
693
697
)
694
698
return AnyType (TypeOfAny .from_error )
695
- return self .anal_type (t .args [0 ])
699
+ return self .anal_type (t .args [0 ], allow_final = self . options . python_version >= ( 3 , 13 ) )
696
700
elif fullname in NEVER_NAMES :
697
701
return UninhabitedType ()
698
702
elif fullname in LITERAL_TYPE_NAMES :
@@ -1878,11 +1882,13 @@ def anal_type(
1878
1882
allow_unpack : bool = False ,
1879
1883
allow_ellipsis : bool = False ,
1880
1884
allow_typed_dict_special_forms : bool = False ,
1885
+ allow_final : bool = False ,
1881
1886
) -> Type :
1882
1887
if nested :
1883
1888
self .nesting_level += 1
1884
1889
old_allow_typed_dict_special_forms = self .allow_typed_dict_special_forms
1885
1890
self .allow_typed_dict_special_forms = allow_typed_dict_special_forms
1891
+ self .allow_final = allow_final
1886
1892
old_allow_ellipsis = self .allow_ellipsis
1887
1893
self .allow_ellipsis = allow_ellipsis
1888
1894
old_allow_unpack = self .allow_unpack
0 commit comments