Skip to content

Commit 52b97bc

Browse files
Bugfixes
1 parent a63cba8 commit 52b97bc

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

mypy/message_registry.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def format(self, *args: object, **kwargs: object) -> "ErrorMessage":
7676
NON_INSTANCE_NEW_TYPE: Final = ErrorMessage('"__new__" must return a class instance (got {})')
7777
INVALID_NEW_TYPE: Final = ErrorMessage('Incompatible return type for "__new__"')
7878
BAD_CONSTRUCTOR_TYPE: Final = ErrorMessage("Unsupported decorated constructor type")
79-
CANNOT_ASSIGN_TO_METHOD: Final = "Cannot assign to a method"
79+
CANNOT_ASSIGN_TO_METHOD: Final = ErrorMessage("Cannot assign to a method", codes.ASSIGNMENT)
8080
CANNOT_ASSIGN_TO_TYPE: Final = ErrorMessage("Cannot assign to a type")
8181
INCONSISTENT_ABSTRACT_OVERLOAD: Final = ErrorMessage(
8282
"Overloaded method has both abstract and non-abstract variants"
@@ -268,10 +268,16 @@ def format(self, *args: object, **kwargs: object) -> "ErrorMessage":
268268
"{} (expression has type {}, target has type {})", codes.ASSIGNMENT
269269
)
270270
VALUE_INCOMPATIBLE_TYPE: Final = ErrorMessage(
271-
'Value of "{}" has incompatible type {}; expected {}'
271+
'Value of "{}" has incompatible type {}; expected {}', codes.ARG_TYPE
272272
)
273273
ARGUMENT_INCOMPATIBLE_TYPE: Final = ErrorMessage(
274-
'Argument {} {}has incompatible type {}; expected {}'
274+
'Argument {} {}has incompatible type {}; expected {}', codes.ARG_TYPE
275+
)
276+
TYPEDDICT_VALUE_INCOMPATIBLE_TYPE: Final = ErrorMessage(
277+
'Value of "{}" has incompatible type {}; expected {}', codes.TYPEDDICT_ITEM
278+
)
279+
TYPEDDICT_ARGUMENT_INCOMPATIBLE_TYPE: Final = ErrorMessage(
280+
'Argument {} {}has incompatible type {}; expected {}', codes.TYPEDDICT_ITEM
275281
)
276282
LIST_ITEM_INCOMPATIBLE_TYPE: Final = ErrorMessage(
277283
"{} item {} has incompatible type {}; expected {}", codes.LIST_ITEM

mypy/messages.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,6 @@ def incompatible_argument(self,
476476

477477
target = 'to {} '.format(name)
478478

479-
msg = ''
480-
code = codes.MISC
481479
notes: List[str] = []
482480
if callee_name == '<list>':
483481
name = callee_name[1:-1]
@@ -564,19 +562,23 @@ def incompatible_argument(self,
564562
expected_type,
565563
bare=True)
566564
arg_label = '"{}"'.format(arg_name)
565+
object_type = get_proper_type(object_type)
567566
if isinstance(outer_context, IndexExpr) and isinstance(outer_context.index, StrExpr):
568-
msg = message_registry.VALUE_INCOMPATIBLE_TYPE.format(
567+
if isinstance(object_type, TypedDictType):
568+
msg = message_registry.TYPEDDICT_VALUE_INCOMPATIBLE_TYPE
569+
else:
570+
msg = message_registry.VALUE_INCOMPATIBLE_TYPE
571+
msg = msg.format(
569572
outer_context.index.value, quote_type_string(arg_type_str),
570573
quote_type_string(expected_type_str))
571574
else:
572-
msg = message_registry.ARGUMENT_INCOMPATIBLE_TYPE.format(
575+
if isinstance(object_type, TypedDictType):
576+
msg = message_registry.TYPEDDICT_ARGUMENT_INCOMPATIBLE_TYPE
577+
else:
578+
msg = message_registry.ARGUMENT_INCOMPATIBLE_TYPE
579+
msg = msg.format(
573580
arg_label, target, quote_type_string(arg_type_str),
574581
quote_type_string(expected_type_str))
575-
object_type = get_proper_type(object_type)
576-
if isinstance(object_type, TypedDictType):
577-
code = codes.TYPEDDICT_ITEM
578-
else:
579-
code = codes.ARG_TYPE
580582
expected_type = get_proper_type(expected_type)
581583
if isinstance(expected_type, UnionType):
582584
expected_types = list(expected_type.items)
@@ -588,7 +590,7 @@ def incompatible_argument(self,
588590
self.fail(msg, context)
589591
if notes:
590592
for note_msg in notes:
591-
self.note(note_msg, context, code=code)
593+
self.note(note_msg, context, code=msg.code)
592594
return msg.code
593595

594596
def incompatible_argument_note(self,
@@ -1058,7 +1060,7 @@ def incompatible_typevar_value(self,
10581060
context: Context) -> None:
10591061
self.fail(message_registry.INCOMPATIBLE_TYPEVAR_VALUE
10601062
.format(typevar_name, callable_name(callee) or 'function', format_type(typ)),
1061-
context)
1063+
context, code=codes.TYPE_VAR) # TODO: migrate with TypeVar messages
10621064

10631065
def dangerous_comparison(self, left: Type, right: Type, kind: str, ctx: Context) -> None:
10641066
left_str = 'element' if kind == 'container' else 'left operand'
@@ -1231,7 +1233,7 @@ def typeddict_key_not_found(
12311233
matches = best_matches(item_name, typ.items.keys())
12321234
if matches:
12331235
self.note("Did you mean {}?".format(
1234-
pretty_seq(matches[:3], "or")), context)
1236+
pretty_seq(matches[:3], "or")), context, code=codes.TYPEDDICT_ITEM)
12351237

12361238
def typeddict_context_ambiguous(
12371239
self,

0 commit comments

Comments
 (0)