Skip to content

Move message constants to mypy.message_registry #6194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 49 additions & 47 deletions mypy/checker.py

Large diffs are not rendered by default.

63 changes: 32 additions & 31 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from mypy.sametypes import is_same_type
from mypy.erasetype import replace_meta_vars, erase_type
from mypy.messages import MessageBuilder
from mypy import messages
from mypy import message_registry
from mypy.infer import infer_type_arguments, infer_function_type_arguments
from mypy import join
from mypy.meet import narrow_declared_type
Expand Down Expand Up @@ -395,7 +395,7 @@ def check_runtime_protocol_test(self, e: CallExpr) -> None:
if (isinstance(tp, CallableType) and tp.is_type_obj() and
tp.type_object().is_protocol and
not tp.type_object().runtime_protocol):
self.chk.fail(messages.RUNTIME_PROTOCOL_EXPECTED, e)
self.chk.fail(message_registry.RUNTIME_PROTOCOL_EXPECTED, e)

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

self.chk.fail(messages.INVALID_TYPEDDICT_ARGS, context)
self.chk.fail(message_registry.INVALID_TYPEDDICT_ARGS, context)
return AnyType(TypeOfAny.from_error)

def check_typeddict_call_with_dict(self, callee: TypedDictType,
Expand All @@ -446,7 +446,7 @@ def check_typeddict_call_with_dict(self, callee: TypedDictType,
for item_name_expr, item_arg in kwargs.items:
if not isinstance(item_name_expr, StrExpr):
key_context = item_name_expr or item_arg
self.chk.fail(messages.TYPEDDICT_KEY_MUST_BE_STRING_LITERAL, key_context)
self.chk.fail(message_registry.TYPEDDICT_KEY_MUST_BE_STRING_LITERAL, key_context)
return AnyType(TypeOfAny.from_error)
item_names.append(item_name_expr.value)

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

Expand Down Expand Up @@ -757,7 +757,7 @@ def check_callable_call(self,
elif (callee.is_type_obj() and callee.type_object().is_protocol
# Exception for Type[...]
and not callee.from_type_type):
self.chk.fail(messages.CANNOT_INSTANTIATE_PROTOCOL
self.chk.fail(message_registry.CANNOT_INSTANTIATE_PROTOCOL
.format(callee.type_object().name()), context)

formal_to_actual = map_actuals_to_formals(
Expand Down Expand Up @@ -1005,7 +1005,7 @@ def infer_function_type_arguments(self, callee_type: CallableType,
if isinstance(first_arg, (NoneTyp, UninhabitedType)):
inferred_args[0] = self.named_type('builtins.str')
elif not first_arg or not is_subtype(self.named_type('builtins.str'), first_arg):
self.msg.fail(messages.KEYWORD_ARGUMENT_REQUIRES_STR_KEY_TYPE,
self.msg.fail(message_registry.KEYWORD_ARGUMENT_REQUIRES_STR_KEY_TYPE,
context)
else:
# In dynamically typed functions use implicit 'Any' types for
Expand Down Expand Up @@ -2402,7 +2402,7 @@ def visit_index_expr_helper(self, e: IndexExpr) -> Type:
if n >= 0 and n < len(left_type.items):
return left_type.items[n]
else:
self.chk.fail(messages.TUPLE_INDEX_OUT_OF_RANGE, e)
self.chk.fail(message_registry.TUPLE_INDEX_OUT_OF_RANGE, e)
return AnyType(TypeOfAny.from_error)
else:
return self.nonliteral_tuple_index_helper(left_type, index)
Expand Down Expand Up @@ -2444,7 +2444,7 @@ def nonliteral_tuple_index_helper(self, left_type: TupleType, index: Expression)
expected_type = UnionType.make_union([self.named_type('builtins.int'),
self.named_type('builtins.slice')])
if not self.chk.check_subtype(index_type, expected_type, index,
messages.INVALID_TUPLE_INDEX_TYPE,
message_registry.INVALID_TUPLE_INDEX_TYPE,
'actual type', 'expected type'):
return AnyType(TypeOfAny.from_error)
else:
Expand Down Expand Up @@ -2553,14 +2553,14 @@ def visit_type_application(self, tapp: TypeApplication) -> Type:
tp = type_object_type(item.type, self.named_type)
return self.apply_type_arguments_to_callable(tp, item.args, tapp)
else:
self.chk.fail(messages.ONLY_CLASS_APPLICATION, tapp)
self.chk.fail(message_registry.ONLY_CLASS_APPLICATION, tapp)
return AnyType(TypeOfAny.from_error)
# Type application of a normal generic class in runtime context.
# This is typically used as `x = G[int]()`.
tp = self.accept(tapp.expr)
if isinstance(tp, (CallableType, Overloaded)):
if not tp.is_type_obj():
self.chk.fail(messages.ONLY_CLASS_APPLICATION, tapp)
self.chk.fail(message_registry.ONLY_CLASS_APPLICATION, tapp)
return self.apply_type_arguments_to_callable(tp, tapp.types, tapp)
if isinstance(tp, AnyType):
return AnyType(TypeOfAny.from_another_any, source_any=tp)
Expand Down Expand Up @@ -2888,7 +2888,7 @@ def infer_lambda_type_using_context(self, e: LambdaExpr) -> Tuple[Optional[Calla
return callable_ctx, None
if callable_ctx.arg_kinds != arg_kinds:
# Incompatible context; cannot use it to infer types.
self.chk.fail(messages.CANNOT_INFER_LAMBDA_TYPE, e)
self.chk.fail(message_registry.CANNOT_INFER_LAMBDA_TYPE, e)
return None, None

return callable_ctx, callable_ctx
Expand All @@ -2902,15 +2902,15 @@ def visit_super_expr(self, e: SuperExpr) -> Type:
def check_super_arguments(self, e: SuperExpr) -> None:
"""Check arguments in a super(...) call."""
if ARG_STAR in e.call.arg_kinds:
self.chk.fail(messages.SUPER_VARARGS_NOT_SUPPORTED, e)
self.chk.fail(message_registry.SUPER_VARARGS_NOT_SUPPORTED, e)
elif e.call.args and set(e.call.arg_kinds) != {ARG_POS}:
self.chk.fail(messages.SUPER_POSITIONAL_ARGS_REQUIRED, e)
self.chk.fail(message_registry.SUPER_POSITIONAL_ARGS_REQUIRED, e)
elif len(e.call.args) == 1:
self.chk.fail(messages.SUPER_WITH_SINGLE_ARG_NOT_SUPPORTED, e)
self.chk.fail(message_registry.SUPER_WITH_SINGLE_ARG_NOT_SUPPORTED, e)
elif len(e.call.args) > 2:
self.chk.fail(messages.TOO_MANY_ARGS_FOR_SUPER, e)
self.chk.fail(message_registry.TOO_MANY_ARGS_FOR_SUPER, e)
elif self.chk.options.python_version[0] == 2 and len(e.call.args) == 0:
self.chk.fail(messages.TOO_FEW_ARGS_FOR_SUPER, e)
self.chk.fail(message_registry.TOO_FEW_ARGS_FOR_SUPER, e)
elif len(e.call.args) == 2:
type_obj_type = self.accept(e.call.args[0])
instance_type = self.accept(e.call.args[1])
Expand All @@ -2926,7 +2926,7 @@ def check_super_arguments(self, e: SuperExpr) -> None:
if not isinstance(item, Instance):
# A complicated type object type. Too tricky, give up.
# TODO: Do something more clever here.
self.chk.fail(messages.UNSUPPORTED_ARG_1_FOR_SUPER, e)
self.chk.fail(message_registry.UNSUPPORTED_ARG_1_FOR_SUPER, e)
return
type_info = item.type
elif isinstance(type_obj_type, AnyType):
Expand All @@ -2942,19 +2942,19 @@ def check_super_arguments(self, e: SuperExpr) -> None:
if not isinstance(instance_type, (Instance, TupleType)):
# Too tricky, give up.
# TODO: Do something more clever here.
self.chk.fail(messages.UNSUPPORTED_ARG_2_FOR_SUPER, e)
self.chk.fail(message_registry.UNSUPPORTED_ARG_2_FOR_SUPER, e)
return
if isinstance(instance_type, TupleType):
# Needed for named tuples and other Tuple[...] subclasses.
instance_type = instance_type.fallback
if type_info not in instance_type.type.mro:
self.chk.fail(messages.SUPER_ARG_2_NOT_INSTANCE_OF_ARG_1, e)
self.chk.fail(message_registry.SUPER_ARG_2_NOT_INSTANCE_OF_ARG_1, e)
elif isinstance(instance_type, TypeType) or (isinstance(instance_type, FunctionLike)
and instance_type.is_type_obj()):
# TODO: Check whether this is a valid type object here.
pass
elif not isinstance(instance_type, AnyType):
self.chk.fail(messages.UNSUPPORTED_ARG_2_FOR_SUPER, e)
self.chk.fail(message_registry.UNSUPPORTED_ARG_2_FOR_SUPER, e)

def analyze_super(self, e: SuperExpr, is_lvalue: bool) -> Type:
"""Type check a super expression."""
Expand All @@ -2973,15 +2973,15 @@ def analyze_super(self, e: SuperExpr, is_lvalue: bool) -> Type:
if not self.chk.in_checked_function():
return AnyType(TypeOfAny.unannotated)
if self.chk.scope.active_class() is not None:
self.chk.fail(messages.SUPER_OUTSIDE_OF_METHOD_NOT_SUPPORTED, e)
self.chk.fail(message_registry.SUPER_OUTSIDE_OF_METHOD_NOT_SUPPORTED, e)
return AnyType(TypeOfAny.from_error)
method = self.chk.scope.top_function()
assert method is not None
args = method.arguments
# super() in a function with empty args is an error; we
# need something in declared_self.
if not args:
self.chk.fail(messages.SUPER_ENCLOSING_POSITIONAL_ARGS_REQUIRED, e)
self.chk.fail(message_registry.SUPER_ENCLOSING_POSITIONAL_ARGS_REQUIRED, e)
return AnyType(TypeOfAny.from_error)
declared_self = args[0].variable.type or fill_typevars(e.info)
return analyze_member_access(name=e.name,
Expand All @@ -3006,7 +3006,7 @@ def visit_slice_expr(self, e: SliceExpr) -> Type:
if index:
t = self.accept(index)
self.chk.check_subtype(t, expected,
index, messages.INVALID_SLICE_INDEX)
index, message_registry.INVALID_SLICE_INDEX)
return self.named_type('builtins.slice')

def visit_list_comprehension(self, e: ListComprehension) -> Type:
Expand Down Expand Up @@ -3277,11 +3277,11 @@ def visit_yield_expr(self, e: YieldExpr) -> Type:
if e.expr is None:
if (not isinstance(expected_item_type, (NoneTyp, AnyType))
and self.chk.in_checked_function()):
self.chk.fail(messages.YIELD_VALUE_EXPECTED, e)
self.chk.fail(message_registry.YIELD_VALUE_EXPECTED, e)
else:
actual_item_type = self.accept(e.expr, expected_item_type)
self.chk.check_subtype(actual_item_type, expected_item_type, e,
messages.INCOMPATIBLE_TYPES_IN_YIELD,
message_registry.INCOMPATIBLE_TYPES_IN_YIELD,
'actual type', 'expected type')
return self.chk.get_generator_receive_type(return_type, False)

Expand All @@ -3292,7 +3292,8 @@ def visit_await_expr(self, e: AwaitExpr) -> Type:
actual_type = self.accept(e.expr, expected_type)
if isinstance(actual_type, AnyType):
return AnyType(TypeOfAny.from_another_any, source_any=actual_type)
return self.check_awaitable_expr(actual_type, e, messages.INCOMPATIBLE_TYPES_IN_AWAIT)
return self.check_awaitable_expr(actual_type, e,
message_registry.INCOMPATIBLE_TYPES_IN_AWAIT)

def check_awaitable_expr(self, t: Type, ctx: Context, msg: str) -> Type:
"""Check the argument to `await` and extract the type of value.
Expand Down Expand Up @@ -3337,17 +3338,17 @@ def visit_yield_from_expr(self, e: YieldFromExpr, allow_none_return: bool = Fals
self.chk.msg.yield_from_invalid_operand_type(subexpr_type, e)
iter_type = AnyType(TypeOfAny.from_error)
else:
iter_type = self.check_awaitable_expr(subexpr_type, e,
messages.INCOMPATIBLE_TYPES_IN_YIELD_FROM)
iter_type = self.check_awaitable_expr(
subexpr_type, e, message_registry.INCOMPATIBLE_TYPES_IN_YIELD_FROM)

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

self.chk.check_subtype(actual_item_type, expected_item_type, e,
messages.INCOMPATIBLE_TYPES_IN_YIELD_FROM,
'actual type', 'expected type')
message_registry.INCOMPATIBLE_TYPES_IN_YIELD_FROM,
'actual type', 'expected type')

# Determine the type of the entire yield from expression.
if (isinstance(iter_type, Instance) and
Expand Down
16 changes: 8 additions & 8 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from mypy.typevars import fill_typevars
from mypy.plugin import AttributeContext
from mypy.typeanal import set_any_tvars
from mypy import messages
from mypy import message_registry
from mypy import subtypes
from mypy import meet

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

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

if dunder_get is None:
msg.fail(messages.DESCRIPTOR_GET_NOT_CALLABLE.format(descriptor_type), context)
msg.fail(message_registry.DESCRIPTOR_GET_NOT_CALLABLE.format(descriptor_type), context)
return AnyType(TypeOfAny.from_error)

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

if not isinstance(inferred_dunder_get_type, CallableType):
msg.fail(messages.DESCRIPTOR_GET_NOT_CALLABLE.format(descriptor_type), context)
msg.fail(message_registry.DESCRIPTOR_GET_NOT_CALLABLE.format(descriptor_type), context)
return AnyType(TypeOfAny.from_error)

return inferred_dunder_get_type.ret_type
Expand Down Expand Up @@ -586,12 +586,12 @@ def analyze_class_attribute_access(itype: Instance,
if is_method:
mx.msg.cant_assign_to_method(mx.context)
if isinstance(node.node, TypeInfo):
mx.msg.fail(messages.CANNOT_ASSIGN_TO_TYPE, mx.context)
mx.msg.fail(message_registry.CANNOT_ASSIGN_TO_TYPE, mx.context)

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

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

if isinstance(node.node, TypeVarExpr):
mx.msg.fail(messages.CANNOT_USE_TYPEVAR_AS_EXPRESSION.format(
mx.msg.fail(message_registry.CANNOT_USE_TYPEVAR_AS_EXPRESSION.format(
itype.type.name(), name), mx.context)
return AnyType(TypeOfAny.from_error)

Expand Down
10 changes: 5 additions & 5 deletions mypy/checkstrformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import mypy.checker
import mypy.checkexpr
from typing_extensions import Final
from mypy import messages
from mypy import message_registry
from mypy.messages import MessageBuilder

FormatStringExpr = Union[StrExpr, BytesExpr, UnicodeExpr]
Expand Down Expand Up @@ -192,7 +192,7 @@ def check_mapping_str_interpolation(self, specifiers: List[ConversionSpecifier],
if expected_type is None:
return
self.chk.check_subtype(rep_type, expected_type, replacements,
messages.INCOMPATIBLE_TYPES_IN_STR_INTERPOLATION,
message_registry.INCOMPATIBLE_TYPES_IN_STR_INTERPOLATION,
'expression has type',
'placeholder with key \'%s\' has type' % specifier.key)
else:
Expand All @@ -201,7 +201,7 @@ def check_mapping_str_interpolation(self, specifiers: List[ConversionSpecifier],
dict_type = self.chk.named_generic_type('builtins.dict',
[any_type, any_type])
self.chk.check_subtype(rep_type, dict_type, replacements,
messages.FORMAT_REQUIRES_MAPPING,
message_registry.FORMAT_REQUIRES_MAPPING,
'expression has type', 'expected type for mapping is')

def build_replacement_checkers(self, specifiers: List[ConversionSpecifier],
Expand Down Expand Up @@ -268,7 +268,7 @@ def checkers_for_regular_type(self, type: str,
def check_type(type: Type) -> None:
assert expected_type is not None
self.chk.check_subtype(type, expected_type, context,
messages.INCOMPATIBLE_TYPES_IN_STR_INTERPOLATION,
message_registry.INCOMPATIBLE_TYPES_IN_STR_INTERPOLATION,
'expression has type', 'placeholder has type')

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

def check_expr(expr: Expression) -> None:
Expand Down
Loading