41
41
from mypy .sametypes import is_same_type
42
42
from mypy .erasetype import replace_meta_vars , erase_type
43
43
from mypy .messages import MessageBuilder
44
- from mypy import messages
44
+ from mypy import message_registry
45
45
from mypy .infer import infer_type_arguments , infer_function_type_arguments
46
46
from mypy import join
47
47
from mypy .meet import narrow_declared_type
@@ -395,7 +395,7 @@ def check_runtime_protocol_test(self, e: CallExpr) -> None:
395
395
if (isinstance (tp , CallableType ) and tp .is_type_obj () and
396
396
tp .type_object ().is_protocol and
397
397
not tp .type_object ().runtime_protocol ):
398
- self .chk .fail (messages .RUNTIME_PROTOCOL_EXPECTED , e )
398
+ self .chk .fail (message_registry .RUNTIME_PROTOCOL_EXPECTED , e )
399
399
400
400
def check_protocol_issubclass (self , e : CallExpr ) -> None :
401
401
for expr in mypy .checker .flatten (e .args [1 ]):
@@ -434,7 +434,7 @@ def check_typeddict_call(self, callee: TypedDictType,
434
434
return self .check_typeddict_call_with_kwargs (
435
435
callee , OrderedDict (), context )
436
436
437
- self .chk .fail (messages .INVALID_TYPEDDICT_ARGS , context )
437
+ self .chk .fail (message_registry .INVALID_TYPEDDICT_ARGS , context )
438
438
return AnyType (TypeOfAny .from_error )
439
439
440
440
def check_typeddict_call_with_dict (self , callee : TypedDictType ,
@@ -446,7 +446,7 @@ def check_typeddict_call_with_dict(self, callee: TypedDictType,
446
446
for item_name_expr , item_arg in kwargs .items :
447
447
if not isinstance (item_name_expr , StrExpr ):
448
448
key_context = item_name_expr or item_arg
449
- self .chk .fail (messages .TYPEDDICT_KEY_MUST_BE_STRING_LITERAL , key_context )
449
+ self .chk .fail (message_registry .TYPEDDICT_KEY_MUST_BE_STRING_LITERAL , key_context )
450
450
return AnyType (TypeOfAny .from_error )
451
451
item_names .append (item_name_expr .value )
452
452
@@ -472,7 +472,7 @@ def check_typeddict_call_with_kwargs(self, callee: TypedDictType,
472
472
item_value = kwargs [item_name ]
473
473
self .chk .check_simple_assignment (
474
474
lvalue_type = item_expected_type , rvalue = item_value , context = item_value ,
475
- msg = messages .INCOMPATIBLE_TYPES ,
475
+ msg = message_registry .INCOMPATIBLE_TYPES ,
476
476
lvalue_name = 'TypedDict item "{}"' .format (item_name ),
477
477
rvalue_name = 'expression' )
478
478
@@ -757,7 +757,7 @@ def check_callable_call(self,
757
757
elif (callee .is_type_obj () and callee .type_object ().is_protocol
758
758
# Exception for Type[...]
759
759
and not callee .from_type_type ):
760
- self .chk .fail (messages .CANNOT_INSTANTIATE_PROTOCOL
760
+ self .chk .fail (message_registry .CANNOT_INSTANTIATE_PROTOCOL
761
761
.format (callee .type_object ().name ()), context )
762
762
763
763
formal_to_actual = map_actuals_to_formals (
@@ -1005,7 +1005,7 @@ def infer_function_type_arguments(self, callee_type: CallableType,
1005
1005
if isinstance (first_arg , (NoneTyp , UninhabitedType )):
1006
1006
inferred_args [0 ] = self .named_type ('builtins.str' )
1007
1007
elif not first_arg or not is_subtype (self .named_type ('builtins.str' ), first_arg ):
1008
- self .msg .fail (messages .KEYWORD_ARGUMENT_REQUIRES_STR_KEY_TYPE ,
1008
+ self .msg .fail (message_registry .KEYWORD_ARGUMENT_REQUIRES_STR_KEY_TYPE ,
1009
1009
context )
1010
1010
else :
1011
1011
# In dynamically typed functions use implicit 'Any' types for
@@ -2402,7 +2402,7 @@ def visit_index_expr_helper(self, e: IndexExpr) -> Type:
2402
2402
if n >= 0 and n < len (left_type .items ):
2403
2403
return left_type .items [n ]
2404
2404
else :
2405
- self .chk .fail (messages .TUPLE_INDEX_OUT_OF_RANGE , e )
2405
+ self .chk .fail (message_registry .TUPLE_INDEX_OUT_OF_RANGE , e )
2406
2406
return AnyType (TypeOfAny .from_error )
2407
2407
else :
2408
2408
return self .nonliteral_tuple_index_helper (left_type , index )
@@ -2444,7 +2444,7 @@ def nonliteral_tuple_index_helper(self, left_type: TupleType, index: Expression)
2444
2444
expected_type = UnionType .make_union ([self .named_type ('builtins.int' ),
2445
2445
self .named_type ('builtins.slice' )])
2446
2446
if not self .chk .check_subtype (index_type , expected_type , index ,
2447
- messages .INVALID_TUPLE_INDEX_TYPE ,
2447
+ message_registry .INVALID_TUPLE_INDEX_TYPE ,
2448
2448
'actual type' , 'expected type' ):
2449
2449
return AnyType (TypeOfAny .from_error )
2450
2450
else :
@@ -2553,14 +2553,14 @@ def visit_type_application(self, tapp: TypeApplication) -> Type:
2553
2553
tp = type_object_type (item .type , self .named_type )
2554
2554
return self .apply_type_arguments_to_callable (tp , item .args , tapp )
2555
2555
else :
2556
- self .chk .fail (messages .ONLY_CLASS_APPLICATION , tapp )
2556
+ self .chk .fail (message_registry .ONLY_CLASS_APPLICATION , tapp )
2557
2557
return AnyType (TypeOfAny .from_error )
2558
2558
# Type application of a normal generic class in runtime context.
2559
2559
# This is typically used as `x = G[int]()`.
2560
2560
tp = self .accept (tapp .expr )
2561
2561
if isinstance (tp , (CallableType , Overloaded )):
2562
2562
if not tp .is_type_obj ():
2563
- self .chk .fail (messages .ONLY_CLASS_APPLICATION , tapp )
2563
+ self .chk .fail (message_registry .ONLY_CLASS_APPLICATION , tapp )
2564
2564
return self .apply_type_arguments_to_callable (tp , tapp .types , tapp )
2565
2565
if isinstance (tp , AnyType ):
2566
2566
return AnyType (TypeOfAny .from_another_any , source_any = tp )
@@ -2888,7 +2888,7 @@ def infer_lambda_type_using_context(self, e: LambdaExpr) -> Tuple[Optional[Calla
2888
2888
return callable_ctx , None
2889
2889
if callable_ctx .arg_kinds != arg_kinds :
2890
2890
# Incompatible context; cannot use it to infer types.
2891
- self .chk .fail (messages .CANNOT_INFER_LAMBDA_TYPE , e )
2891
+ self .chk .fail (message_registry .CANNOT_INFER_LAMBDA_TYPE , e )
2892
2892
return None , None
2893
2893
2894
2894
return callable_ctx , callable_ctx
@@ -2902,15 +2902,15 @@ def visit_super_expr(self, e: SuperExpr) -> Type:
2902
2902
def check_super_arguments (self , e : SuperExpr ) -> None :
2903
2903
"""Check arguments in a super(...) call."""
2904
2904
if ARG_STAR in e .call .arg_kinds :
2905
- self .chk .fail (messages .SUPER_VARARGS_NOT_SUPPORTED , e )
2905
+ self .chk .fail (message_registry .SUPER_VARARGS_NOT_SUPPORTED , e )
2906
2906
elif e .call .args and set (e .call .arg_kinds ) != {ARG_POS }:
2907
- self .chk .fail (messages .SUPER_POSITIONAL_ARGS_REQUIRED , e )
2907
+ self .chk .fail (message_registry .SUPER_POSITIONAL_ARGS_REQUIRED , e )
2908
2908
elif len (e .call .args ) == 1 :
2909
- self .chk .fail (messages .SUPER_WITH_SINGLE_ARG_NOT_SUPPORTED , e )
2909
+ self .chk .fail (message_registry .SUPER_WITH_SINGLE_ARG_NOT_SUPPORTED , e )
2910
2910
elif len (e .call .args ) > 2 :
2911
- self .chk .fail (messages .TOO_MANY_ARGS_FOR_SUPER , e )
2911
+ self .chk .fail (message_registry .TOO_MANY_ARGS_FOR_SUPER , e )
2912
2912
elif self .chk .options .python_version [0 ] == 2 and len (e .call .args ) == 0 :
2913
- self .chk .fail (messages .TOO_FEW_ARGS_FOR_SUPER , e )
2913
+ self .chk .fail (message_registry .TOO_FEW_ARGS_FOR_SUPER , e )
2914
2914
elif len (e .call .args ) == 2 :
2915
2915
type_obj_type = self .accept (e .call .args [0 ])
2916
2916
instance_type = self .accept (e .call .args [1 ])
@@ -2926,7 +2926,7 @@ def check_super_arguments(self, e: SuperExpr) -> None:
2926
2926
if not isinstance (item , Instance ):
2927
2927
# A complicated type object type. Too tricky, give up.
2928
2928
# TODO: Do something more clever here.
2929
- self .chk .fail (messages .UNSUPPORTED_ARG_1_FOR_SUPER , e )
2929
+ self .chk .fail (message_registry .UNSUPPORTED_ARG_1_FOR_SUPER , e )
2930
2930
return
2931
2931
type_info = item .type
2932
2932
elif isinstance (type_obj_type , AnyType ):
@@ -2942,19 +2942,19 @@ def check_super_arguments(self, e: SuperExpr) -> None:
2942
2942
if not isinstance (instance_type , (Instance , TupleType )):
2943
2943
# Too tricky, give up.
2944
2944
# TODO: Do something more clever here.
2945
- self .chk .fail (messages .UNSUPPORTED_ARG_2_FOR_SUPER , e )
2945
+ self .chk .fail (message_registry .UNSUPPORTED_ARG_2_FOR_SUPER , e )
2946
2946
return
2947
2947
if isinstance (instance_type , TupleType ):
2948
2948
# Needed for named tuples and other Tuple[...] subclasses.
2949
2949
instance_type = instance_type .fallback
2950
2950
if type_info not in instance_type .type .mro :
2951
- self .chk .fail (messages .SUPER_ARG_2_NOT_INSTANCE_OF_ARG_1 , e )
2951
+ self .chk .fail (message_registry .SUPER_ARG_2_NOT_INSTANCE_OF_ARG_1 , e )
2952
2952
elif isinstance (instance_type , TypeType ) or (isinstance (instance_type , FunctionLike )
2953
2953
and instance_type .is_type_obj ()):
2954
2954
# TODO: Check whether this is a valid type object here.
2955
2955
pass
2956
2956
elif not isinstance (instance_type , AnyType ):
2957
- self .chk .fail (messages .UNSUPPORTED_ARG_2_FOR_SUPER , e )
2957
+ self .chk .fail (message_registry .UNSUPPORTED_ARG_2_FOR_SUPER , e )
2958
2958
2959
2959
def analyze_super (self , e : SuperExpr , is_lvalue : bool ) -> Type :
2960
2960
"""Type check a super expression."""
@@ -2973,15 +2973,15 @@ def analyze_super(self, e: SuperExpr, is_lvalue: bool) -> Type:
2973
2973
if not self .chk .in_checked_function ():
2974
2974
return AnyType (TypeOfAny .unannotated )
2975
2975
if self .chk .scope .active_class () is not None :
2976
- self .chk .fail (messages .SUPER_OUTSIDE_OF_METHOD_NOT_SUPPORTED , e )
2976
+ self .chk .fail (message_registry .SUPER_OUTSIDE_OF_METHOD_NOT_SUPPORTED , e )
2977
2977
return AnyType (TypeOfAny .from_error )
2978
2978
method = self .chk .scope .top_function ()
2979
2979
assert method is not None
2980
2980
args = method .arguments
2981
2981
# super() in a function with empty args is an error; we
2982
2982
# need something in declared_self.
2983
2983
if not args :
2984
- self .chk .fail (messages .SUPER_ENCLOSING_POSITIONAL_ARGS_REQUIRED , e )
2984
+ self .chk .fail (message_registry .SUPER_ENCLOSING_POSITIONAL_ARGS_REQUIRED , e )
2985
2985
return AnyType (TypeOfAny .from_error )
2986
2986
declared_self = args [0 ].variable .type or fill_typevars (e .info )
2987
2987
return analyze_member_access (name = e .name ,
@@ -3006,7 +3006,7 @@ def visit_slice_expr(self, e: SliceExpr) -> Type:
3006
3006
if index :
3007
3007
t = self .accept (index )
3008
3008
self .chk .check_subtype (t , expected ,
3009
- index , messages .INVALID_SLICE_INDEX )
3009
+ index , message_registry .INVALID_SLICE_INDEX )
3010
3010
return self .named_type ('builtins.slice' )
3011
3011
3012
3012
def visit_list_comprehension (self , e : ListComprehension ) -> Type :
@@ -3277,11 +3277,11 @@ def visit_yield_expr(self, e: YieldExpr) -> Type:
3277
3277
if e .expr is None :
3278
3278
if (not isinstance (expected_item_type , (NoneTyp , AnyType ))
3279
3279
and self .chk .in_checked_function ()):
3280
- self .chk .fail (messages .YIELD_VALUE_EXPECTED , e )
3280
+ self .chk .fail (message_registry .YIELD_VALUE_EXPECTED , e )
3281
3281
else :
3282
3282
actual_item_type = self .accept (e .expr , expected_item_type )
3283
3283
self .chk .check_subtype (actual_item_type , expected_item_type , e ,
3284
- messages .INCOMPATIBLE_TYPES_IN_YIELD ,
3284
+ message_registry .INCOMPATIBLE_TYPES_IN_YIELD ,
3285
3285
'actual type' , 'expected type' )
3286
3286
return self .chk .get_generator_receive_type (return_type , False )
3287
3287
@@ -3292,7 +3292,8 @@ def visit_await_expr(self, e: AwaitExpr) -> Type:
3292
3292
actual_type = self .accept (e .expr , expected_type )
3293
3293
if isinstance (actual_type , AnyType ):
3294
3294
return AnyType (TypeOfAny .from_another_any , source_any = actual_type )
3295
- return self .check_awaitable_expr (actual_type , e , messages .INCOMPATIBLE_TYPES_IN_AWAIT )
3295
+ return self .check_awaitable_expr (actual_type , e ,
3296
+ message_registry .INCOMPATIBLE_TYPES_IN_AWAIT )
3296
3297
3297
3298
def check_awaitable_expr (self , t : Type , ctx : Context , msg : str ) -> Type :
3298
3299
"""Check the argument to `await` and extract the type of value.
@@ -3337,17 +3338,17 @@ def visit_yield_from_expr(self, e: YieldFromExpr, allow_none_return: bool = Fals
3337
3338
self .chk .msg .yield_from_invalid_operand_type (subexpr_type , e )
3338
3339
iter_type = AnyType (TypeOfAny .from_error )
3339
3340
else :
3340
- iter_type = self .check_awaitable_expr (subexpr_type , e ,
3341
- messages .INCOMPATIBLE_TYPES_IN_YIELD_FROM )
3341
+ iter_type = self .check_awaitable_expr (
3342
+ subexpr_type , e , message_registry .INCOMPATIBLE_TYPES_IN_YIELD_FROM )
3342
3343
3343
3344
# Check that the iterator's item type matches the type yielded by the Generator function
3344
3345
# containing this `yield from` expression.
3345
3346
expected_item_type = self .chk .get_generator_yield_type (return_type , False )
3346
3347
actual_item_type = self .chk .get_generator_yield_type (iter_type , False )
3347
3348
3348
3349
self .chk .check_subtype (actual_item_type , expected_item_type , e ,
3349
- messages .INCOMPATIBLE_TYPES_IN_YIELD_FROM ,
3350
- 'actual type' , 'expected type' )
3350
+ message_registry .INCOMPATIBLE_TYPES_IN_YIELD_FROM ,
3351
+ 'actual type' , 'expected type' )
3351
3352
3352
3353
# Determine the type of the entire yield from expression.
3353
3354
if (isinstance (iter_type , Instance ) and
0 commit comments