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
Point error to incompatible argument instead of call expression #7470
Merged
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
83d09fd
Point error message to incompatible argument expression
JukkaL 4db0af3
Turn off column numbers by default
JukkaL ade34e2
Fix issues
JukkaL aeb8f2b
Fix line/column of slice expressions
JukkaL 00fc08a
Fix various issues with wrong lines/columns; fix tests
JukkaL 3261540
Fix various tests
JukkaL 80f5fe7
Update test case
JukkaL e1eb7e2
Add test case
JukkaL a193558
Fix test case
JukkaL 03aad6e
Fix slices
JukkaL 837bb9e
Merge branch 'master' into columns-3-arg
JukkaL 629ab4d
Update test case
JukkaL fbd4dfc
Address review
JukkaL 49128f1
Fix tests on Python 3.8
JukkaL File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,7 +64,16 @@ | |
|
||
# Type of callback user for checking individual function arguments. See | ||
# check_args() below for details. | ||
ArgChecker = Callable[[Type, Type, int, Type, int, int, CallableType, Context, MessageBuilder], | ||
ArgChecker = Callable[[Type, | ||
Type, | ||
int, | ||
Type, | ||
int, | ||
int, | ||
CallableType, | ||
Context, | ||
Context, | ||
MessageBuilder], | ||
None] | ||
|
||
# Maximum nesting level for math union in overloads, setting this to large values | ||
|
@@ -842,7 +851,7 @@ def check_callable_call(self, | |
self.check_argument_count(callee, arg_types, arg_kinds, | ||
arg_names, formal_to_actual, context, self.msg) | ||
|
||
self.check_argument_types(arg_types, arg_kinds, callee, formal_to_actual, context, | ||
self.check_argument_types(arg_types, arg_kinds, args, callee, formal_to_actual, context, | ||
messages=arg_messages) | ||
|
||
if (callee.is_type_obj() and (len(arg_types) == 1) | ||
|
@@ -1169,7 +1178,8 @@ def check_argument_count(self, | |
if messages: | ||
assert context, "Internal error: messages given without context" | ||
elif context is None: | ||
context = TempNode(AnyType(TypeOfAny.special_form)) # Avoid "is None" checks | ||
# Avoid "is None" checks | ||
context = TempNode(AnyType(TypeOfAny.special_form), context=context) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
# TODO(jukka): We could return as soon as we find an error if messages is None. | ||
|
||
|
@@ -1271,6 +1281,7 @@ def check_for_extra_actual_arguments(self, | |
def check_argument_types(self, | ||
arg_types: List[Type], | ||
arg_kinds: List[int], | ||
args: List[Expression], | ||
callee: CallableType, | ||
formal_to_actual: List[List[int]], | ||
context: Context, | ||
|
@@ -1303,12 +1314,19 @@ def check_argument_types(self, | |
callee.arg_names[i], callee.arg_kinds[i]) | ||
check_arg(expanded_actual, actual_type, arg_kinds[actual], | ||
callee.arg_types[i], | ||
actual + 1, i + 1, callee, context, messages) | ||
actual + 1, i + 1, callee, args[actual], context, messages) | ||
|
||
def check_arg(self, caller_type: Type, original_caller_type: Type, | ||
def check_arg(self, | ||
caller_type: Type, | ||
original_caller_type: Type, | ||
caller_kind: int, | ||
callee_type: Type, n: int, m: int, callee: CallableType, | ||
context: Context, messages: MessageBuilder) -> None: | ||
callee_type: Type, | ||
n: int, | ||
m: int, | ||
callee: CallableType, | ||
context: Context, | ||
outer_context: Context, | ||
messages: MessageBuilder) -> None: | ||
"""Check the type of a single argument in a call.""" | ||
caller_type = get_proper_type(caller_type) | ||
original_caller_type = get_proper_type(original_caller_type) | ||
|
@@ -1326,8 +1344,13 @@ def check_arg(self, caller_type: Type, original_caller_type: Type, | |
elif not is_subtype(caller_type, callee_type): | ||
if self.chk.should_suppress_optional_error([caller_type, callee_type]): | ||
return | ||
code = messages.incompatible_argument(n, m, callee, original_caller_type, | ||
caller_kind, context) | ||
code = messages.incompatible_argument(n, | ||
m, | ||
callee, | ||
original_caller_type, | ||
caller_kind, | ||
context=context, | ||
outer_context=outer_context) | ||
messages.incompatible_argument_note(original_caller_type, callee_type, context, | ||
code=code) | ||
|
||
|
@@ -1404,7 +1427,7 @@ def check_overload_call(self, | |
# Neither alternative matches, but we can guess the user probably wants the | ||
# second one. | ||
erased_targets = self.overload_erased_call_targets(plausible_targets, arg_types, | ||
arg_kinds, arg_names, context) | ||
arg_kinds, arg_names, args, context) | ||
|
||
# Step 5: We try and infer a second-best alternative if possible. If not, fall back | ||
# to using 'Any'. | ||
|
@@ -1569,14 +1592,16 @@ def overload_erased_call_targets(self, | |
arg_types: List[Type], | ||
arg_kinds: List[int], | ||
arg_names: Optional[Sequence[Optional[str]]], | ||
args: List[Expression], | ||
context: Context) -> List[CallableType]: | ||
"""Returns a list of all targets that match the caller after erasing types. | ||
|
||
Assumes all of the given targets have argument counts compatible with the caller. | ||
""" | ||
matches = [] # type: List[CallableType] | ||
for typ in plausible_targets: | ||
if self.erased_signature_similarity(arg_types, arg_kinds, arg_names, typ, context): | ||
if self.erased_signature_similarity(arg_types, arg_kinds, arg_names, args, typ, | ||
context): | ||
matches.append(typ) | ||
return matches | ||
|
||
|
@@ -1755,8 +1780,11 @@ def combine_function_signatures(self, types: Sequence[Type]) -> Union[AnyType, C | |
variables=variables, | ||
implicit=True) | ||
|
||
def erased_signature_similarity(self, arg_types: List[Type], arg_kinds: List[int], | ||
def erased_signature_similarity(self, | ||
arg_types: List[Type], | ||
arg_kinds: List[int], | ||
arg_names: Optional[Sequence[Optional[str]]], | ||
args: List[Expression], | ||
callee: CallableType, | ||
context: Context) -> bool: | ||
"""Determine whether arguments could match the signature at runtime, after | ||
|
@@ -1772,16 +1800,23 @@ def erased_signature_similarity(self, arg_types: List[Type], arg_kinds: List[int | |
# Too few or many arguments -> no match. | ||
return False | ||
|
||
def check_arg(caller_type: Type, original_caller_type: Type, caller_kind: int, | ||
callee_type: Type, n: int, m: int, callee: CallableType, | ||
context: Context, messages: MessageBuilder) -> None: | ||
def check_arg(caller_type: Type, | ||
original_ccaller_type: Type, | ||
caller_kind: int, | ||
callee_type: Type, | ||
n: int, | ||
m: int, | ||
callee: CallableType, | ||
context: Context, | ||
outer_context: Context, | ||
messages: MessageBuilder) -> None: | ||
if not arg_approximate_similarity(caller_type, callee_type): | ||
# No match -- exit early since none of the remaining work can change | ||
# the result. | ||
raise Finished | ||
|
||
try: | ||
self.check_argument_types(arg_types, arg_kinds, callee, | ||
self.check_argument_types(arg_types, arg_kinds, args, callee, | ||
formal_to_actual, context=context, check_arg=check_arg) | ||
return True | ||
except Finished: | ||
|
@@ -2428,7 +2463,7 @@ def check_op(self, method: str, base_type: Type, | |
result, inferred = self.check_op_reversible( | ||
op_name=method, | ||
left_type=left_possible_type, | ||
left_expr=TempNode(left_possible_type), | ||
left_expr=TempNode(left_possible_type, context=context), | ||
right_type=right_type, | ||
right_expr=arg, | ||
context=context, | ||
|
@@ -2455,7 +2490,8 @@ def check_op(self, method: str, base_type: Type, | |
right_variants = [(right_type, arg)] | ||
right_type = get_proper_type(right_type) | ||
if isinstance(right_type, UnionType): | ||
right_variants = [(item, TempNode(item)) for item in right_type.relevant_items()] | ||
right_variants = [(item, TempNode(item, context=context)) | ||
for item in right_type.relevant_items()] | ||
|
||
msg = self.msg.clean_copy() | ||
msg.disable_count = 0 | ||
|
@@ -2467,7 +2503,7 @@ def check_op(self, method: str, base_type: Type, | |
result, inferred = self.check_op_reversible( | ||
op_name=method, | ||
left_type=left_possible_type, | ||
left_expr=TempNode(left_possible_type), | ||
left_expr=TempNode(left_possible_type, context=context), | ||
right_type=right_possible_type, | ||
right_expr=right_expr, | ||
context=context, | ||
|
@@ -2480,9 +2516,9 @@ def check_op(self, method: str, base_type: Type, | |
if len(left_variants) >= 2 and len(right_variants) >= 2: | ||
self.msg.warn_both_operands_are_from_unions(context) | ||
elif len(left_variants) >= 2: | ||
self.msg.warn_operand_was_from_union("Left", base_type, context) | ||
self.msg.warn_operand_was_from_union("Left", base_type, context=right_expr) | ||
elif len(right_variants) >= 2: | ||
self.msg.warn_operand_was_from_union("Right", right_type, context) | ||
self.msg.warn_operand_was_from_union("Right", right_type, context=right_expr) | ||
|
||
# See the comment in 'check_overload_call' for more details on why | ||
# we call 'combine_function_signature' instead of just unioning the inferred | ||
|
@@ -2811,10 +2847,10 @@ def visit_reveal_expr(self, expr: RevealExpr) -> Type: | |
assert expr.expr is not None | ||
revealed_type = self.accept(expr.expr, type_context=self.type_context[-1]) | ||
if not self.chk.current_node_deferred: | ||
self.msg.reveal_type(revealed_type, expr) | ||
self.msg.reveal_type(revealed_type, expr.expr) | ||
if not self.chk.in_checked_function(): | ||
self.msg.note("'reveal_type' always outputs 'Any' in unchecked functions", | ||
expr) | ||
expr.expr) | ||
return revealed_type | ||
else: | ||
# REVEAL_LOCALS | ||
|
@@ -3063,7 +3099,14 @@ def visit_dict_expr(self, e: DictExpr) -> Type: | |
if key is None: | ||
stargs.append(value) | ||
else: | ||
args.append(TupleExpr([key, value])) | ||
tup = TupleExpr([key, value]) | ||
if key.line >= 0: | ||
tup.line = key.line | ||
tup.column = key.column | ||
else: | ||
tup.line = value.line | ||
tup.column = value.column | ||
args.append(tup) | ||
# Define type variables (used in constructors below). | ||
ktdef = TypeVarDef('KT', 'KT', -1, [], self.object_type()) | ||
vtdef = TypeVarDef('VT', 'VT', -2, [], self.object_type()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you trying black? :-)