70
70
from mypy .sharedparse import BINARY_MAGIC_METHODS
71
71
from mypy .scope import Scope
72
72
from mypy .typeops import tuple_fallback
73
- from mypy import state
73
+ from mypy import state , errorcodes as codes
74
74
from mypy .traverser import has_return_statement
75
75
from mypy .errorcodes import ErrorCode
76
76
@@ -961,7 +961,8 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str])
961
961
# This is a NoReturn function
962
962
self .msg .fail (message_registry .INVALID_IMPLICIT_RETURN , defn )
963
963
else :
964
- self .msg .fail (message_registry .MISSING_RETURN_STATEMENT , defn )
964
+ self .msg .fail (message_registry .MISSING_RETURN_STATEMENT , defn ,
965
+ code = codes .RETURN )
965
966
966
967
self .return_types .pop ()
967
968
@@ -980,7 +981,8 @@ def check_default_args(self, item: FuncItem, body_is_trivial: bool) -> None:
980
981
else :
981
982
msg += 'argument "{}"' .format (name )
982
983
self .check_simple_assignment (arg .variable .type , arg .initializer ,
983
- context = arg , msg = msg , lvalue_name = 'argument' , rvalue_name = 'default' )
984
+ context = arg , msg = msg , lvalue_name = 'argument' , rvalue_name = 'default' ,
985
+ code = codes .ASSIGNMENT )
984
986
985
987
def is_forward_op_method (self , method_name : str ) -> bool :
986
988
if self .options .python_version [0 ] == 2 and method_name == '__div__' :
@@ -1009,24 +1011,30 @@ def is_unannotated_any(t: Type) -> bool:
1009
1011
if fdef .type is None and self .options .disallow_untyped_defs :
1010
1012
if (not fdef .arguments or (len (fdef .arguments ) == 1 and
1011
1013
(fdef .arg_names [0 ] == 'self' or fdef .arg_names [0 ] == 'cls' ))):
1012
- self .fail (message_registry .RETURN_TYPE_EXPECTED , fdef )
1014
+ self .fail (message_registry .RETURN_TYPE_EXPECTED , fdef ,
1015
+ code = codes .NO_UNTYPED_DEF )
1013
1016
if not has_return_statement (fdef ):
1014
1017
self .note ('Use "-> None" if function does not return a value' , fdef )
1015
1018
else :
1016
- self .fail (message_registry .FUNCTION_TYPE_EXPECTED , fdef )
1019
+ self .fail (message_registry .FUNCTION_TYPE_EXPECTED , fdef ,
1020
+ code = codes .NO_UNTYPED_DEF )
1017
1021
elif isinstance (fdef .type , CallableType ):
1018
1022
ret_type = fdef .type .ret_type
1019
1023
if is_unannotated_any (ret_type ):
1020
- self .fail (message_registry .RETURN_TYPE_EXPECTED , fdef )
1024
+ self .fail (message_registry .RETURN_TYPE_EXPECTED , fdef ,
1025
+ code = codes .NO_UNTYPED_DEF )
1021
1026
elif fdef .is_generator :
1022
1027
if is_unannotated_any (self .get_generator_return_type (ret_type ,
1023
1028
fdef .is_coroutine )):
1024
- self .fail (message_registry .RETURN_TYPE_EXPECTED , fdef )
1029
+ self .fail (message_registry .RETURN_TYPE_EXPECTED , fdef ,
1030
+ code = codes .NO_UNTYPED_DEF )
1025
1031
elif fdef .is_coroutine and isinstance (ret_type , Instance ):
1026
1032
if is_unannotated_any (self .get_coroutine_return_type (ret_type )):
1027
- self .fail (message_registry .RETURN_TYPE_EXPECTED , fdef )
1033
+ self .fail (message_registry .RETURN_TYPE_EXPECTED , fdef ,
1034
+ code = codes .NO_UNTYPED_DEF )
1028
1035
if any (is_unannotated_any (t ) for t in fdef .type .arg_types ):
1029
- self .fail (message_registry .ARGUMENT_TYPE_EXPECTED , fdef )
1036
+ self .fail (message_registry .ARGUMENT_TYPE_EXPECTED , fdef ,
1037
+ code = codes .NO_UNTYPED_DEF )
1030
1038
1031
1039
def check___new___signature (self , fdef : FuncDef , typ : CallableType ) -> None :
1032
1040
self_type = fill_typevars_with_any (fdef .info )
@@ -1975,7 +1983,8 @@ def check_assignment(self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type
1975
1983
rvalue_type , lvalue_type , infer_lvalue_type = self .check_member_assignment (
1976
1984
instance_type , lvalue_type , rvalue , lvalue )
1977
1985
else :
1978
- rvalue_type = self .check_simple_assignment (lvalue_type , rvalue , lvalue )
1986
+ rvalue_type = self .check_simple_assignment (lvalue_type , rvalue , lvalue ,
1987
+ code = codes .ASSIGNMENT )
1979
1988
1980
1989
# Special case: only non-abstract non-protocol classes can be assigned to
1981
1990
# variables with explicit type Type[A], where A is protocol or abstract.
@@ -2111,7 +2120,8 @@ def check_compatibility_super(self, lvalue: RefExpr, lvalue_type: Optional[Type]
2111
2120
return self .check_subtype (compare_type , base_type , lvalue ,
2112
2121
message_registry .INCOMPATIBLE_TYPES_IN_ASSIGNMENT ,
2113
2122
'expression has type' ,
2114
- 'base class "%s" defined the type as' % base .name ())
2123
+ 'base class "%s" defined the type as' % base .name (),
2124
+ code = codes .ASSIGNMENT )
2115
2125
return True
2116
2126
2117
2127
def lvalue_type_from_base (self , expr_node : Var ,
@@ -2675,7 +2685,8 @@ def check_simple_assignment(self, lvalue_type: Optional[Type], rvalue: Expressio
2675
2685
context : Context ,
2676
2686
msg : str = message_registry .INCOMPATIBLE_TYPES_IN_ASSIGNMENT ,
2677
2687
lvalue_name : str = 'variable' ,
2678
- rvalue_name : str = 'expression' ) -> Type :
2688
+ rvalue_name : str = 'expression' , * ,
2689
+ code : Optional [ErrorCode ] = None ) -> Type :
2679
2690
if self .is_stub and isinstance (rvalue , EllipsisExpr ):
2680
2691
# '...' is always a valid initializer in a stub.
2681
2692
return AnyType (TypeOfAny .special_form )
@@ -2690,7 +2701,7 @@ def check_simple_assignment(self, lvalue_type: Optional[Type], rvalue: Expressio
2690
2701
elif lvalue_type :
2691
2702
self .check_subtype (rvalue_type , lvalue_type , context , msg ,
2692
2703
'{} has type' .format (rvalue_name ),
2693
- '{} has type' .format (lvalue_name ))
2704
+ '{} has type' .format (lvalue_name ), code = code )
2694
2705
return rvalue_type
2695
2706
2696
2707
def check_member_assignment (self , instance_type : Type , attribute_type : Type ,
@@ -2892,7 +2903,8 @@ def check_return_stmt(self, s: ReturnStmt) -> None:
2892
2903
supertype_label = 'expected' ,
2893
2904
supertype = return_type ,
2894
2905
context = s ,
2895
- msg = message_registry .INCOMPATIBLE_RETURN_VALUE_TYPE )
2906
+ msg = message_registry .INCOMPATIBLE_RETURN_VALUE_TYPE ,
2907
+ code = codes .RETURN_VALUE )
2896
2908
else :
2897
2909
# Empty returns are valid in Generators with Any typed returns, but not in
2898
2910
# coroutines.
@@ -3674,7 +3686,8 @@ def find_isinstance_check(self, node: Expression
3674
3686
def check_subtype (self , subtype : Type , supertype : Type , context : Context ,
3675
3687
msg : str = message_registry .INCOMPATIBLE_TYPES ,
3676
3688
subtype_label : Optional [str ] = None ,
3677
- supertype_label : Optional [str ] = None ) -> bool :
3689
+ supertype_label : Optional [str ] = None , * ,
3690
+ code : Optional [ErrorCode ] = None ) -> bool :
3678
3691
"""Generate an error if the subtype is not compatible with
3679
3692
supertype."""
3680
3693
if is_subtype (subtype , supertype ):
@@ -3697,7 +3710,7 @@ def check_subtype(self, subtype: Type, supertype: Type, context: Context,
3697
3710
notes = append_invariance_notes ([], subtype , supertype )
3698
3711
if extra_info :
3699
3712
msg += ' (' + ', ' .join (extra_info ) + ')'
3700
- self .fail (msg , context )
3713
+ self .fail (msg , context , code = code )
3701
3714
for note in notes :
3702
3715
self .msg .note (note , context )
3703
3716
if note_msg :
0 commit comments