Skip to content

Commit 9624b54

Browse files
committed
Prevent a crash in MessageBuilder.report() when context is None. (#1583)
Also fix the reported source. Fixes #1564
1 parent 790bd66 commit 9624b54

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

mypy/checkexpr.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ def overload_call_target(self, arg_types: List[Type], arg_kinds: List[int],
663663
best_match = 0
664664
for typ in overload.items():
665665
similarity = self.erased_signature_similarity(arg_types, arg_kinds, arg_names,
666-
typ)
666+
typ, context=context)
667667
if similarity > 0 and similarity >= best_match:
668668
if (match and not is_same_type(match[-1].ret_type,
669669
typ.ret_type) and
@@ -702,12 +702,14 @@ def overload_call_target(self, arg_types: List[Type], arg_kinds: List[int],
702702
# matching signature, or default to the first one if none
703703
# match.
704704
for m in match:
705-
if self.match_signature_types(arg_types, arg_kinds, arg_names, m):
705+
if self.match_signature_types(arg_types, arg_kinds, arg_names, m,
706+
context=context):
706707
return m
707708
return match[0]
708709

709710
def erased_signature_similarity(self, arg_types: List[Type], arg_kinds: List[int],
710-
arg_names: List[str], callee: CallableType) -> int:
711+
arg_names: List[str], callee: CallableType,
712+
context: Context) -> int:
711713
"""Determine whether arguments could match the signature at runtime.
712714
713715
Return similarity level (0 = no match, 1 = can match, 2 = non-promotion match). See
@@ -739,14 +741,15 @@ def check_arg(caller_type: Type, original_caller_type: Type, caller_kind: int,
739741

740742
try:
741743
self.check_argument_types(arg_types, arg_kinds, callee, formal_to_actual,
742-
None, check_arg=check_arg)
744+
context=context, check_arg=check_arg)
743745
except Finished:
744746
pass
745747

746748
return similarity
747749

748750
def match_signature_types(self, arg_types: List[Type], arg_kinds: List[int],
749-
arg_names: List[str], callee: CallableType) -> bool:
751+
arg_names: List[str], callee: CallableType,
752+
context: Context) -> bool:
750753
"""Determine whether arguments types match the signature.
751754
752755
Assume that argument counts are compatible.
@@ -768,7 +771,7 @@ def check_arg(caller_type: Type, original_caller_type: Type, caller_kind: int,
768771
ok = False
769772

770773
self.check_argument_types(arg_types, arg_kinds, callee, formal_to_actual,
771-
None, check_arg=check_arg)
774+
context=context, check_arg=check_arg)
772775
return ok
773776

774777
def apply_generic_arguments(self, callable: CallableType, types: List[Type],

mypy/messages.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ def is_errors(self) -> bool:
140140
def report(self, msg: str, context: Context, severity: str, file: str = None) -> None:
141141
"""Report an error or note (unless disabled)."""
142142
if self.disable_count <= 0:
143-
self.errors.report(context.get_line(), msg.strip(), severity=severity, file=file)
143+
self.errors.report(context.get_line() if context else -1,
144+
msg.strip(), severity=severity, file=file)
144145

145146
def fail(self, msg: str, context: Context, file: str = None) -> None:
146147
"""Report an error message (unless disabled)."""

0 commit comments

Comments
 (0)