@@ -138,7 +138,7 @@ def analyze_type_alias(
138
138
try :
139
139
type = expr_to_unanalyzed_type (node , options , api .is_stub_file )
140
140
except TypeTranslationError :
141
- api .fail ("Invalid type alias: expression is not a valid type" , node )
141
+ api .fail ("Invalid type alias: expression is not a valid type" , node , code = codes . VALID_TYPE )
142
142
return None
143
143
analyzer = TypeAnalyser (
144
144
api ,
@@ -281,11 +281,13 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool)
281
281
tvar_def = self .tvar_scope .get_binding (sym )
282
282
if isinstance (sym .node , ParamSpecExpr ):
283
283
if tvar_def is None :
284
- self .fail (f'ParamSpec "{ t .name } " is unbound' , t )
284
+ self .fail (f'ParamSpec "{ t .name } " is unbound' , t , code = codes . VALID_TYPE )
285
285
return AnyType (TypeOfAny .from_error )
286
286
assert isinstance (tvar_def , ParamSpecType )
287
287
if len (t .args ) > 0 :
288
- self .fail (f'ParamSpec "{ t .name } " used with arguments' , t )
288
+ self .fail (
289
+ f'ParamSpec "{ t .name } " used with arguments' , t , code = codes .VALID_TYPE
290
+ )
289
291
# Change the line number
290
292
return ParamSpecType (
291
293
tvar_def .name ,
@@ -298,15 +300,17 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool)
298
300
)
299
301
if isinstance (sym .node , TypeVarExpr ) and tvar_def is not None and self .defining_alias :
300
302
self .fail (
301
- 'Can\' t use bound type variable "{}"'
302
- " to define generic alias" .format (t .name ),
303
+ f'Can\' t use bound type variable "{ t .name } " to define generic alias' ,
303
304
t ,
305
+ code = codes .VALID_TYPE ,
304
306
)
305
307
return AnyType (TypeOfAny .from_error )
306
308
if isinstance (sym .node , TypeVarExpr ) and tvar_def is not None :
307
309
assert isinstance (tvar_def , TypeVarType )
308
310
if len (t .args ) > 0 :
309
- self .fail (f'Type variable "{ t .name } " used with arguments' , t )
311
+ self .fail (
312
+ f'Type variable "{ t .name } " used with arguments' , t , code = codes .VALID_TYPE
313
+ )
310
314
# Change the line number
311
315
return TypeVarType (
312
316
tvar_def .name ,
@@ -322,18 +326,20 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool)
322
326
tvar_def is not None and self .defining_alias
323
327
):
324
328
self .fail (
325
- 'Can\' t use bound type variable "{}"'
326
- " to define generic alias" .format (t .name ),
329
+ f'Can\' t use bound type variable "{ t .name } " to define generic alias' ,
327
330
t ,
331
+ code = codes .VALID_TYPE ,
328
332
)
329
333
return AnyType (TypeOfAny .from_error )
330
334
if isinstance (sym .node , TypeVarTupleExpr ):
331
335
if tvar_def is None :
332
- self .fail (f'TypeVarTuple "{ t .name } " is unbound' , t )
336
+ self .fail (f'TypeVarTuple "{ t .name } " is unbound' , t , code = codes . VALID_TYPE )
333
337
return AnyType (TypeOfAny .from_error )
334
338
assert isinstance (tvar_def , TypeVarTupleType )
335
339
if len (t .args ) > 0 :
336
- self .fail (f'Type variable "{ t .name } " used with arguments' , t )
340
+ self .fail (
341
+ f'Type variable "{ t .name } " used with arguments' , t , code = codes .VALID_TYPE
342
+ )
337
343
# Change the line number
338
344
return TypeVarTupleType (
339
345
tvar_def .name ,
@@ -401,19 +407,23 @@ def cannot_resolve_type(self, t: UnboundType) -> None:
401
407
402
408
def apply_concatenate_operator (self , t : UnboundType ) -> Type :
403
409
if len (t .args ) == 0 :
404
- self .api .fail ("Concatenate needs type arguments" , t )
410
+ self .api .fail ("Concatenate needs type arguments" , t , code = codes . VALID_TYPE )
405
411
return AnyType (TypeOfAny .from_error )
406
412
407
413
# last argument has to be ParamSpec
408
414
ps = self .anal_type (t .args [- 1 ], allow_param_spec = True )
409
415
if not isinstance (ps , ParamSpecType ):
410
- self .api .fail ("The last parameter to Concatenate needs to be a ParamSpec" , t )
416
+ self .api .fail (
417
+ "The last parameter to Concatenate needs to be a ParamSpec" ,
418
+ t ,
419
+ code = codes .VALID_TYPE ,
420
+ )
411
421
return AnyType (TypeOfAny .from_error )
412
422
413
423
# TODO: this may not work well with aliases, if those worked.
414
424
# Those should be special-cased.
415
425
elif ps .prefix .arg_types :
416
- self .api .fail ("Nested Concatenates are invalid" , t )
426
+ self .api .fail ("Nested Concatenates are invalid" , t , code = codes . VALID_TYPE )
417
427
418
428
args = self .anal_array (t .args [:- 1 ])
419
429
pre = ps .prefix
@@ -437,7 +447,9 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ
437
447
return AnyType (TypeOfAny .explicit )
438
448
elif fullname in FINAL_TYPE_NAMES :
439
449
self .fail (
440
- "Final can be only used as an outermost qualifier in a variable annotation" , t
450
+ "Final can be only used as an outermost qualifier in a variable annotation" ,
451
+ t ,
452
+ code = codes .VALID_TYPE ,
441
453
)
442
454
return AnyType (TypeOfAny .from_error )
443
455
elif fullname == "typing.Tuple" or (
@@ -468,7 +480,9 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ
468
480
return UnionType .make_union (items )
469
481
elif fullname == "typing.Optional" :
470
482
if len (t .args ) != 1 :
471
- self .fail ("Optional[...] must have exactly one type argument" , t )
483
+ self .fail (
484
+ "Optional[...] must have exactly one type argument" , t , code = codes .VALID_TYPE
485
+ )
472
486
return AnyType (TypeOfAny .from_error )
473
487
item = self .anal_type (t .args [0 ])
474
488
return make_optional_type (item )
@@ -488,19 +502,25 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ
488
502
return None
489
503
if len (t .args ) != 1 :
490
504
type_str = "Type[...]" if fullname == "typing.Type" else "type[...]"
491
- self .fail (type_str + " must have exactly one type argument" , t )
505
+ self .fail (
506
+ type_str + " must have exactly one type argument" , t , code = codes .VALID_TYPE
507
+ )
492
508
item = self .anal_type (t .args [0 ])
493
509
if bad_type_type_item (item ):
494
- self .fail ("Type[...] can't contain another Type[...]" , t )
510
+ self .fail ("Type[...] can't contain another Type[...]" , t , code = codes . VALID_TYPE )
495
511
item = AnyType (TypeOfAny .from_error )
496
512
return TypeType .make_normalized (item , line = t .line , column = t .column )
497
513
elif fullname == "typing.ClassVar" :
498
514
if self .nesting_level > 0 :
499
- self .fail ("Invalid type: ClassVar nested inside other type" , t )
515
+ self .fail (
516
+ "Invalid type: ClassVar nested inside other type" , t , code = codes .VALID_TYPE
517
+ )
500
518
if len (t .args ) == 0 :
501
519
return AnyType (TypeOfAny .from_omitted_generics , line = t .line , column = t .column )
502
520
if len (t .args ) != 1 :
503
- self .fail ("ClassVar[...] must have at most one type argument" , t )
521
+ self .fail (
522
+ "ClassVar[...] must have at most one type argument" , t , code = codes .VALID_TYPE
523
+ )
504
524
return AnyType (TypeOfAny .from_error )
505
525
return self .anal_type (t .args [0 ])
506
526
elif fullname in NEVER_NAMES :
@@ -513,23 +533,36 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ
513
533
"Annotated[...] must have exactly one type argument"
514
534
" and at least one annotation" ,
515
535
t ,
536
+ code = codes .VALID_TYPE ,
516
537
)
517
538
return AnyType (TypeOfAny .from_error )
518
539
return self .anal_type (t .args [0 ])
519
540
elif fullname in ("typing_extensions.Required" , "typing.Required" ):
520
541
if not self .allow_required :
521
- self .fail ("Required[] can be only used in a TypedDict definition" , t )
542
+ self .fail (
543
+ "Required[] can be only used in a TypedDict definition" ,
544
+ t ,
545
+ code = codes .VALID_TYPE ,
546
+ )
522
547
return AnyType (TypeOfAny .from_error )
523
548
if len (t .args ) != 1 :
524
- self .fail ("Required[] must have exactly one type argument" , t )
549
+ self .fail (
550
+ "Required[] must have exactly one type argument" , t , code = codes .VALID_TYPE
551
+ )
525
552
return AnyType (TypeOfAny .from_error )
526
553
return RequiredType (self .anal_type (t .args [0 ]), required = True )
527
554
elif fullname in ("typing_extensions.NotRequired" , "typing.NotRequired" ):
528
555
if not self .allow_required :
529
- self .fail ("NotRequired[] can be only used in a TypedDict definition" , t )
556
+ self .fail (
557
+ "NotRequired[] can be only used in a TypedDict definition" ,
558
+ t ,
559
+ code = codes .VALID_TYPE ,
560
+ )
530
561
return AnyType (TypeOfAny .from_error )
531
562
if len (t .args ) != 1 :
532
- self .fail ("NotRequired[] must have exactly one type argument" , t )
563
+ self .fail (
564
+ "NotRequired[] must have exactly one type argument" , t , code = codes .VALID_TYPE
565
+ )
533
566
return AnyType (TypeOfAny .from_error )
534
567
return RequiredType (self .anal_type (t .args [0 ]), required = False )
535
568
elif self .anal_type_guard_arg (t , fullname ) is not None :
@@ -627,7 +660,11 @@ def analyze_type_with_type_info(
627
660
)
628
661
629
662
if info .fullname == "types.NoneType" :
630
- self .fail ("NoneType should not be used as a type, please use None instead" , ctx )
663
+ self .fail (
664
+ "NoneType should not be used as a type, please use None instead" ,
665
+ ctx ,
666
+ code = codes .VALID_TYPE ,
667
+ )
631
668
return NoneType (ctx .line , ctx .column )
632
669
633
670
return instance
@@ -680,7 +717,7 @@ def analyze_unbound_type_without_type_info(
680
717
msg = message_registry .INVALID_TYPE_RAW_ENUM_VALUE .format (
681
718
base_enum_short_name , value
682
719
)
683
- self .fail (msg , t )
720
+ self .fail (msg . value , t , code = msg . code )
684
721
return AnyType (TypeOfAny .from_error )
685
722
return LiteralType (
686
723
value = value ,
@@ -763,12 +800,14 @@ def visit_type_list(self, t: TypeList) -> Type:
763
800
else :
764
801
return AnyType (TypeOfAny .from_error )
765
802
else :
766
- self .fail ('Bracketed expression "[...]" is not valid as a type' , t )
803
+ self .fail (
804
+ 'Bracketed expression "[...]" is not valid as a type' , t , code = codes .VALID_TYPE
805
+ )
767
806
self .note ('Did you mean "List[...]"?' , t )
768
807
return AnyType (TypeOfAny .from_error )
769
808
770
809
def visit_callable_argument (self , t : CallableArgument ) -> Type :
771
- self .fail ("Invalid type" , t )
810
+ self .fail ("Invalid type" , t , code = codes . VALID_TYPE )
772
811
return AnyType (TypeOfAny .from_error )
773
812
774
813
def visit_instance (self , t : Instance ) -> Type :
@@ -831,7 +870,9 @@ def anal_type_guard(self, t: Type) -> Type | None:
831
870
def anal_type_guard_arg (self , t : UnboundType , fullname : str ) -> Type | None :
832
871
if fullname in ("typing_extensions.TypeGuard" , "typing.TypeGuard" ):
833
872
if len (t .args ) != 1 :
834
- self .fail ("TypeGuard must have exactly one type argument" , t )
873
+ self .fail (
874
+ "TypeGuard must have exactly one type argument" , t , code = codes .VALID_TYPE
875
+ )
835
876
return AnyType (TypeOfAny .from_error )
836
877
return self .anal_type (t .args [0 ])
837
878
return None
@@ -848,11 +889,19 @@ def anal_star_arg_type(self, t: Type, kind: ArgKind, nested: bool) -> Type:
848
889
if kind == ARG_STAR :
849
890
make_paramspec = paramspec_args
850
891
if components [- 1 ] != "args" :
851
- self .fail (f'Use "{ tvar_name } .args" for variadic "*" parameter' , t )
892
+ self .fail (
893
+ f'Use "{ tvar_name } .args" for variadic "*" parameter' ,
894
+ t ,
895
+ code = codes .VALID_TYPE ,
896
+ )
852
897
elif kind == ARG_STAR2 :
853
898
make_paramspec = paramspec_kwargs
854
899
if components [- 1 ] != "kwargs" :
855
- self .fail (f'Use "{ tvar_name } .kwargs" for variadic "**" parameter' , t )
900
+ self .fail (
901
+ f'Use "{ tvar_name } .kwargs" for variadic "**" parameter' ,
902
+ t ,
903
+ code = codes .VALID_TYPE ,
904
+ )
856
905
else :
857
906
assert False , kind
858
907
return make_paramspec (
@@ -966,7 +1015,7 @@ def visit_union_type(self, t: UnionType) -> Type:
966
1015
and not self .always_allow_new_syntax
967
1016
and not self .options .python_version >= (3 , 10 )
968
1017
):
969
- self .fail ("X | Y syntax for unions requires Python 3.10" , t )
1018
+ self .fail ("X | Y syntax for unions requires Python 3.10" , t , code = codes . SYNTAX )
970
1019
return UnionType (self .anal_array (t .items ), t .line )
971
1020
972
1021
def visit_partial_type (self , t : PartialType ) -> Type :
@@ -1096,6 +1145,7 @@ def analyze_callable_type(self, t: UnboundType) -> Type:
1096
1145
"The first argument to Callable must be a "
1097
1146
'list of types, parameter specification, or "..."' ,
1098
1147
t ,
1148
+ code = codes .VALID_TYPE ,
1099
1149
)
1100
1150
self .note (
1101
1151
"See https://mypy.readthedocs.io/en/stable/kinds_of_types.html#callable-types-and-lambdas" , # noqa: E501
@@ -1149,7 +1199,7 @@ def analyze_callable_args(
1149
1199
1150
1200
def analyze_literal_type (self , t : UnboundType ) -> Type :
1151
1201
if len (t .args ) == 0 :
1152
- self .fail ("Literal[...] must have at least one parameter" , t )
1202
+ self .fail ("Literal[...] must have at least one parameter" , t , code = codes . VALID_TYPE )
1153
1203
return AnyType (TypeOfAny .from_error )
1154
1204
1155
1205
output : list [Type ] = []
@@ -1210,7 +1260,7 @@ def analyze_literal_param(self, idx: int, arg: Type, ctx: Context) -> list[Type]
1210
1260
msg = f'Parameter { idx } of Literal[...] cannot be of type "{ name } "'
1211
1261
else :
1212
1262
msg = "Invalid type: Literal[...] cannot contain arbitrary expressions"
1213
- self .fail (msg , ctx )
1263
+ self .fail (msg , ctx , code = codes . VALID_TYPE )
1214
1264
# Note: we deliberately ignore arg.note here: the extra info might normally be
1215
1265
# helpful, but it generally won't make sense in the context of a Literal[...].
1216
1266
return None
@@ -1296,7 +1346,11 @@ def bind_function_type_variables(
1296
1346
defs : list [TypeVarLikeType ] = []
1297
1347
for name , tvar in typevars :
1298
1348
if not self .tvar_scope .allow_binding (tvar .fullname ):
1299
- self .fail (f'Type variable "{ name } " is bound by an outer class' , defn )
1349
+ self .fail (
1350
+ f'Type variable "{ name } " is bound by an outer class' ,
1351
+ defn ,
1352
+ code = codes .VALID_TYPE ,
1353
+ )
1300
1354
self .tvar_scope .bind_new (name , tvar )
1301
1355
binding = self .tvar_scope .get_binding (tvar .fullname )
1302
1356
assert binding is not None
@@ -1335,10 +1389,12 @@ def anal_type(self, t: Type, nested: bool = True, *, allow_param_spec: bool = Fa
1335
1389
and analyzed .flavor == ParamSpecFlavor .BARE
1336
1390
):
1337
1391
if analyzed .prefix .arg_types :
1338
- self .fail ("Invalid location for Concatenate" , t )
1392
+ self .fail ("Invalid location for Concatenate" , t , code = codes . VALID_TYPE )
1339
1393
self .note ("You can use Concatenate as the first argument to Callable" , t )
1340
1394
else :
1341
- self .fail (f'Invalid location for ParamSpec "{ analyzed .name } "' , t )
1395
+ self .fail (
1396
+ f'Invalid location for ParamSpec "{ analyzed .name } "' , t , code = codes .VALID_TYPE
1397
+ )
1342
1398
self .note (
1343
1399
"You can use ParamSpec as the first argument to Callable, e.g., "
1344
1400
"'Callable[{}, int]'" .format (analyzed .name ),
0 commit comments