Skip to content

Commit cdfca4f

Browse files
chadrikJukkaL
authored andcommitted
Move message constants to mypy.message_registry (#6194)
This is in preparation for message filtering (#2417). By moving constants to their own module we can easily inspect them to get both their name and value, for use within a message filtering system.
1 parent f82319f commit cdfca4f

File tree

12 files changed

+260
-245
lines changed

12 files changed

+260
-245
lines changed

mypy/checker.py

Lines changed: 49 additions & 47 deletions
Large diffs are not rendered by default.

mypy/checkexpr.py

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from mypy.sametypes import is_same_type
4242
from mypy.erasetype import replace_meta_vars, erase_type
4343
from mypy.messages import MessageBuilder
44-
from mypy import messages
44+
from mypy import message_registry
4545
from mypy.infer import infer_type_arguments, infer_function_type_arguments
4646
from mypy import join
4747
from mypy.meet import narrow_declared_type
@@ -395,7 +395,7 @@ def check_runtime_protocol_test(self, e: CallExpr) -> None:
395395
if (isinstance(tp, CallableType) and tp.is_type_obj() and
396396
tp.type_object().is_protocol and
397397
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)
399399

400400
def check_protocol_issubclass(self, e: CallExpr) -> None:
401401
for expr in mypy.checker.flatten(e.args[1]):
@@ -434,7 +434,7 @@ def check_typeddict_call(self, callee: TypedDictType,
434434
return self.check_typeddict_call_with_kwargs(
435435
callee, OrderedDict(), context)
436436

437-
self.chk.fail(messages.INVALID_TYPEDDICT_ARGS, context)
437+
self.chk.fail(message_registry.INVALID_TYPEDDICT_ARGS, context)
438438
return AnyType(TypeOfAny.from_error)
439439

440440
def check_typeddict_call_with_dict(self, callee: TypedDictType,
@@ -446,7 +446,7 @@ def check_typeddict_call_with_dict(self, callee: TypedDictType,
446446
for item_name_expr, item_arg in kwargs.items:
447447
if not isinstance(item_name_expr, StrExpr):
448448
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)
450450
return AnyType(TypeOfAny.from_error)
451451
item_names.append(item_name_expr.value)
452452

@@ -472,7 +472,7 @@ def check_typeddict_call_with_kwargs(self, callee: TypedDictType,
472472
item_value = kwargs[item_name]
473473
self.chk.check_simple_assignment(
474474
lvalue_type=item_expected_type, rvalue=item_value, context=item_value,
475-
msg=messages.INCOMPATIBLE_TYPES,
475+
msg=message_registry.INCOMPATIBLE_TYPES,
476476
lvalue_name='TypedDict item "{}"'.format(item_name),
477477
rvalue_name='expression')
478478

@@ -757,7 +757,7 @@ def check_callable_call(self,
757757
elif (callee.is_type_obj() and callee.type_object().is_protocol
758758
# Exception for Type[...]
759759
and not callee.from_type_type):
760-
self.chk.fail(messages.CANNOT_INSTANTIATE_PROTOCOL
760+
self.chk.fail(message_registry.CANNOT_INSTANTIATE_PROTOCOL
761761
.format(callee.type_object().name()), context)
762762

763763
formal_to_actual = map_actuals_to_formals(
@@ -1005,7 +1005,7 @@ def infer_function_type_arguments(self, callee_type: CallableType,
10051005
if isinstance(first_arg, (NoneTyp, UninhabitedType)):
10061006
inferred_args[0] = self.named_type('builtins.str')
10071007
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,
10091009
context)
10101010
else:
10111011
# In dynamically typed functions use implicit 'Any' types for
@@ -2402,7 +2402,7 @@ def visit_index_expr_helper(self, e: IndexExpr) -> Type:
24022402
if n >= 0 and n < len(left_type.items):
24032403
return left_type.items[n]
24042404
else:
2405-
self.chk.fail(messages.TUPLE_INDEX_OUT_OF_RANGE, e)
2405+
self.chk.fail(message_registry.TUPLE_INDEX_OUT_OF_RANGE, e)
24062406
return AnyType(TypeOfAny.from_error)
24072407
else:
24082408
return self.nonliteral_tuple_index_helper(left_type, index)
@@ -2444,7 +2444,7 @@ def nonliteral_tuple_index_helper(self, left_type: TupleType, index: Expression)
24442444
expected_type = UnionType.make_union([self.named_type('builtins.int'),
24452445
self.named_type('builtins.slice')])
24462446
if not self.chk.check_subtype(index_type, expected_type, index,
2447-
messages.INVALID_TUPLE_INDEX_TYPE,
2447+
message_registry.INVALID_TUPLE_INDEX_TYPE,
24482448
'actual type', 'expected type'):
24492449
return AnyType(TypeOfAny.from_error)
24502450
else:
@@ -2553,14 +2553,14 @@ def visit_type_application(self, tapp: TypeApplication) -> Type:
25532553
tp = type_object_type(item.type, self.named_type)
25542554
return self.apply_type_arguments_to_callable(tp, item.args, tapp)
25552555
else:
2556-
self.chk.fail(messages.ONLY_CLASS_APPLICATION, tapp)
2556+
self.chk.fail(message_registry.ONLY_CLASS_APPLICATION, tapp)
25572557
return AnyType(TypeOfAny.from_error)
25582558
# Type application of a normal generic class in runtime context.
25592559
# This is typically used as `x = G[int]()`.
25602560
tp = self.accept(tapp.expr)
25612561
if isinstance(tp, (CallableType, Overloaded)):
25622562
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)
25642564
return self.apply_type_arguments_to_callable(tp, tapp.types, tapp)
25652565
if isinstance(tp, AnyType):
25662566
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
28882888
return callable_ctx, None
28892889
if callable_ctx.arg_kinds != arg_kinds:
28902890
# 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)
28922892
return None, None
28932893

28942894
return callable_ctx, callable_ctx
@@ -2902,15 +2902,15 @@ def visit_super_expr(self, e: SuperExpr) -> Type:
29022902
def check_super_arguments(self, e: SuperExpr) -> None:
29032903
"""Check arguments in a super(...) call."""
29042904
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)
29062906
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)
29082908
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)
29102910
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)
29122912
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)
29142914
elif len(e.call.args) == 2:
29152915
type_obj_type = self.accept(e.call.args[0])
29162916
instance_type = self.accept(e.call.args[1])
@@ -2926,7 +2926,7 @@ def check_super_arguments(self, e: SuperExpr) -> None:
29262926
if not isinstance(item, Instance):
29272927
# A complicated type object type. Too tricky, give up.
29282928
# 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)
29302930
return
29312931
type_info = item.type
29322932
elif isinstance(type_obj_type, AnyType):
@@ -2942,19 +2942,19 @@ def check_super_arguments(self, e: SuperExpr) -> None:
29422942
if not isinstance(instance_type, (Instance, TupleType)):
29432943
# Too tricky, give up.
29442944
# 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)
29462946
return
29472947
if isinstance(instance_type, TupleType):
29482948
# Needed for named tuples and other Tuple[...] subclasses.
29492949
instance_type = instance_type.fallback
29502950
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)
29522952
elif isinstance(instance_type, TypeType) or (isinstance(instance_type, FunctionLike)
29532953
and instance_type.is_type_obj()):
29542954
# TODO: Check whether this is a valid type object here.
29552955
pass
29562956
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)
29582958

29592959
def analyze_super(self, e: SuperExpr, is_lvalue: bool) -> Type:
29602960
"""Type check a super expression."""
@@ -2973,15 +2973,15 @@ def analyze_super(self, e: SuperExpr, is_lvalue: bool) -> Type:
29732973
if not self.chk.in_checked_function():
29742974
return AnyType(TypeOfAny.unannotated)
29752975
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)
29772977
return AnyType(TypeOfAny.from_error)
29782978
method = self.chk.scope.top_function()
29792979
assert method is not None
29802980
args = method.arguments
29812981
# super() in a function with empty args is an error; we
29822982
# need something in declared_self.
29832983
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)
29852985
return AnyType(TypeOfAny.from_error)
29862986
declared_self = args[0].variable.type or fill_typevars(e.info)
29872987
return analyze_member_access(name=e.name,
@@ -3006,7 +3006,7 @@ def visit_slice_expr(self, e: SliceExpr) -> Type:
30063006
if index:
30073007
t = self.accept(index)
30083008
self.chk.check_subtype(t, expected,
3009-
index, messages.INVALID_SLICE_INDEX)
3009+
index, message_registry.INVALID_SLICE_INDEX)
30103010
return self.named_type('builtins.slice')
30113011

30123012
def visit_list_comprehension(self, e: ListComprehension) -> Type:
@@ -3277,11 +3277,11 @@ def visit_yield_expr(self, e: YieldExpr) -> Type:
32773277
if e.expr is None:
32783278
if (not isinstance(expected_item_type, (NoneTyp, AnyType))
32793279
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)
32813281
else:
32823282
actual_item_type = self.accept(e.expr, expected_item_type)
32833283
self.chk.check_subtype(actual_item_type, expected_item_type, e,
3284-
messages.INCOMPATIBLE_TYPES_IN_YIELD,
3284+
message_registry.INCOMPATIBLE_TYPES_IN_YIELD,
32853285
'actual type', 'expected type')
32863286
return self.chk.get_generator_receive_type(return_type, False)
32873287

@@ -3292,7 +3292,8 @@ def visit_await_expr(self, e: AwaitExpr) -> Type:
32923292
actual_type = self.accept(e.expr, expected_type)
32933293
if isinstance(actual_type, AnyType):
32943294
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)
32963297

32973298
def check_awaitable_expr(self, t: Type, ctx: Context, msg: str) -> Type:
32983299
"""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
33373338
self.chk.msg.yield_from_invalid_operand_type(subexpr_type, e)
33383339
iter_type = AnyType(TypeOfAny.from_error)
33393340
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)
33423343

33433344
# Check that the iterator's item type matches the type yielded by the Generator function
33443345
# containing this `yield from` expression.
33453346
expected_item_type = self.chk.get_generator_yield_type(return_type, False)
33463347
actual_item_type = self.chk.get_generator_yield_type(iter_type, False)
33473348

33483349
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')
33513352

33523353
# Determine the type of the entire yield from expression.
33533354
if (isinstance(iter_type, Instance) and

mypy/checkmember.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from mypy.typevars import fill_typevars
1919
from mypy.plugin import AttributeContext
2020
from mypy.typeanal import set_any_tvars
21-
from mypy import messages
21+
from mypy import message_registry
2222
from mypy import subtypes
2323
from mypy import meet
2424

@@ -147,7 +147,7 @@ def analyze_instance_member_access(name: str,
147147
if name == '__init__' and not mx.is_super:
148148
# Accessing __init__ in statically typed code would compromise
149149
# type safety unless used via super().
150-
mx.msg.fail(messages.CANNOT_ACCESS_INIT, mx.context)
150+
mx.msg.fail(message_registry.CANNOT_ACCESS_INIT, mx.context)
151151
return AnyType(TypeOfAny.from_error)
152152

153153
# The base object has an instance type.
@@ -405,7 +405,7 @@ def analyze_descriptor_access(instance_type: Type,
405405
dunder_get = descriptor_type.type.get_method('__get__')
406406

407407
if dunder_get is None:
408-
msg.fail(messages.DESCRIPTOR_GET_NOT_CALLABLE.format(descriptor_type), context)
408+
msg.fail(message_registry.DESCRIPTOR_GET_NOT_CALLABLE.format(descriptor_type), context)
409409
return AnyType(TypeOfAny.from_error)
410410

411411
function = function_type(dunder_get, builtin_type('builtins.function'))
@@ -432,7 +432,7 @@ def analyze_descriptor_access(instance_type: Type,
432432
return inferred_dunder_get_type
433433

434434
if not isinstance(inferred_dunder_get_type, CallableType):
435-
msg.fail(messages.DESCRIPTOR_GET_NOT_CALLABLE.format(descriptor_type), context)
435+
msg.fail(message_registry.DESCRIPTOR_GET_NOT_CALLABLE.format(descriptor_type), context)
436436
return AnyType(TypeOfAny.from_error)
437437

438438
return inferred_dunder_get_type.ret_type
@@ -586,12 +586,12 @@ def analyze_class_attribute_access(itype: Instance,
586586
if is_method:
587587
mx.msg.cant_assign_to_method(mx.context)
588588
if isinstance(node.node, TypeInfo):
589-
mx.msg.fail(messages.CANNOT_ASSIGN_TO_TYPE, mx.context)
589+
mx.msg.fail(message_registry.CANNOT_ASSIGN_TO_TYPE, mx.context)
590590

591591
# If a final attribute was declared on `self` in `__init__`, then it
592592
# can't be accessed on the class object.
593593
if node.implicit and isinstance(node.node, Var) and node.node.is_final:
594-
mx.msg.fail(messages.CANNOT_ACCESS_FINAL_INSTANCE_ATTR
594+
mx.msg.fail(message_registry.CANNOT_ACCESS_FINAL_INSTANCE_ATTR
595595
.format(node.node.name()), mx.context)
596596

597597
# An assignment to final attribute on class object is also always an error,
@@ -609,7 +609,7 @@ def analyze_class_attribute_access(itype: Instance,
609609
assert isinstance(symnode, Var)
610610
return mx.chk.handle_partial_var_type(t, mx.is_lvalue, symnode, mx.context)
611611
if not is_method and (isinstance(t, TypeVarType) or get_type_vars(t)):
612-
mx.msg.fail(messages.GENERIC_INSTANCE_VAR_CLASS_ACCESS, mx.context)
612+
mx.msg.fail(message_registry.GENERIC_INSTANCE_VAR_CLASS_ACCESS, mx.context)
613613
is_classmethod = ((is_decorated and cast(Decorator, node.node).func.is_class)
614614
or (isinstance(node.node, FuncBase) and node.node.is_class))
615615
result = add_class_tvars(t, itype, is_classmethod, mx.builtin_type, mx.original_type)
@@ -622,7 +622,7 @@ def analyze_class_attribute_access(itype: Instance,
622622
return AnyType(TypeOfAny.special_form)
623623

624624
if isinstance(node.node, TypeVarExpr):
625-
mx.msg.fail(messages.CANNOT_USE_TYPEVAR_AS_EXPRESSION.format(
625+
mx.msg.fail(message_registry.CANNOT_USE_TYPEVAR_AS_EXPRESSION.format(
626626
itype.type.name(), name), mx.context)
627627
return AnyType(TypeOfAny.from_error)
628628

mypy/checkstrformat.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import mypy.checker
1818
import mypy.checkexpr
1919
from typing_extensions import Final
20-
from mypy import messages
20+
from mypy import message_registry
2121
from mypy.messages import MessageBuilder
2222

2323
FormatStringExpr = Union[StrExpr, BytesExpr, UnicodeExpr]
@@ -192,7 +192,7 @@ def check_mapping_str_interpolation(self, specifiers: List[ConversionSpecifier],
192192
if expected_type is None:
193193
return
194194
self.chk.check_subtype(rep_type, expected_type, replacements,
195-
messages.INCOMPATIBLE_TYPES_IN_STR_INTERPOLATION,
195+
message_registry.INCOMPATIBLE_TYPES_IN_STR_INTERPOLATION,
196196
'expression has type',
197197
'placeholder with key \'%s\' has type' % specifier.key)
198198
else:
@@ -201,7 +201,7 @@ def check_mapping_str_interpolation(self, specifiers: List[ConversionSpecifier],
201201
dict_type = self.chk.named_generic_type('builtins.dict',
202202
[any_type, any_type])
203203
self.chk.check_subtype(rep_type, dict_type, replacements,
204-
messages.FORMAT_REQUIRES_MAPPING,
204+
message_registry.FORMAT_REQUIRES_MAPPING,
205205
'expression has type', 'expected type for mapping is')
206206

207207
def build_replacement_checkers(self, specifiers: List[ConversionSpecifier],
@@ -268,7 +268,7 @@ def checkers_for_regular_type(self, type: str,
268268
def check_type(type: Type) -> None:
269269
assert expected_type is not None
270270
self.chk.check_subtype(type, expected_type, context,
271-
messages.INCOMPATIBLE_TYPES_IN_STR_INTERPOLATION,
271+
message_registry.INCOMPATIBLE_TYPES_IN_STR_INTERPOLATION,
272272
'expression has type', 'placeholder has type')
273273

274274
def check_expr(expr: Expression) -> None:
@@ -290,7 +290,7 @@ def checkers_for_c_type(self, type: str,
290290
def check_type(type: Type) -> None:
291291
assert expected_type is not None
292292
self.chk.check_subtype(type, expected_type, context,
293-
messages.INCOMPATIBLE_TYPES_IN_STR_INTERPOLATION,
293+
message_registry.INCOMPATIBLE_TYPES_IN_STR_INTERPOLATION,
294294
'expression has type', 'placeholder has type')
295295

296296
def check_expr(expr: Expression) -> None:

0 commit comments

Comments
 (0)