Skip to content

Commit

Permalink
Show closest candidates for misspellings of keyword arguments names p…
Browse files Browse the repository at this point in the history
  • Loading branch information
Martijn Wuis authored and Martijn Wuis committed Nov 1, 2019
1 parent 4ffa9bc commit ede8c4d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,11 @@ def too_many_positional_arguments(self, callee: CallableType,
def unexpected_keyword_argument(self, callee: CallableType, name: str,
context: Context) -> None:
msg = 'Unexpected keyword argument "{}"'.format(name) + for_function(callee)
# Suggest intended keyword, if any match is found.
matches = best_matches(name, list(filter(None, callee.arg_names)))[:3]
if matches:
suggestion = "; did you mean {}?".format(pretty_or(matches))
msg += "{}".format(suggestion)
self.fail(msg, context, code=codes.CALL_ARG)
module = find_defining_module(self.modules, callee)
if module:
Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-kwargs.test
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ def f(a: 'A') -> None: pass # N: "f" defined here
f(b=object()) # E: Unexpected keyword argument "b" for "f"
class A: pass

[case testKeywordsMisspelling]
def f(thing, other): ...
f(otter='test') # E: Unexpected keyword argument "otter" for "f"; did you mean "other"?

[case testMultipleKeywordsForMisspelling]
def f(thing, other, atter, btter): ...
f(otter='test') # E: Unexpected keyword argument "otter" for "f"; did you mean "other", "btter", or "atter"?

[case testKeywordArgumentsWithDynamicallyTypedCallable]
from typing import Any
f = None # type: Any
Expand Down

0 comments on commit ede8c4d

Please sign in to comment.