Skip to content
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

Fix for issue #505 (Better error message for missing keyword argument) #512

Merged
merged 15 commits into from Jan 20, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/checkexpr.py
Expand Up @@ -472,7 +472,7 @@ def check_argument_count(self, callee: Callable, actual_types: List[Type],
if kind == nodes.ARG_POS and (not formal_to_actual[i] and
not is_error):
# No actual for a mandatory positional formal.
self.msg.too_few_arguments(callee, context)
self.msg.too_few_arguments(callee, context, actual_names)
elif kind in [nodes.ARG_POS, nodes.ARG_OPT,
nodes.ARG_NAMED] and is_duplicate_mapping(
formal_to_actual[i], actual_kinds):
Expand Down
24 changes: 14 additions & 10 deletions mypy/messages.py
Expand Up @@ -396,17 +396,21 @@ def invalid_index_type(self, index_type: Type, base_str: str,
self.fail('Invalid index type {} for {}'.format(
self.format(index_type), base_str), context)

def invalid_argument_count(self, callee: Callable, num_args: int,
context: Context) -> None:
if num_args < len(callee.arg_types):
self.too_few_arguments(callee, context)
def too_few_arguments(self, callee: Callable, context: Context,
argument_names: List[str]) -> None:
if (argument_names is not None and not all(k is None for k in argument_names)
and len(argument_names) >= 1):
diff = [k for k in callee.arg_names if k not in argument_names]
if len(diff) == 1:
msg = 'Missing positional argument'
else:
msg = 'Missing positional arguments'
if callee.name and diff:
msg += ' "{}" in call to {}'.format('", "'.join(diff), callee.name)
else:
self.too_many_arguments(callee, context)

def too_few_arguments(self, callee: Callable, context: Context) -> None:
msg = 'Too few arguments'
if callee.name:
msg += ' for {}'.format(callee.name)
msg = 'Too few arguments'
if callee.name:
msg += ' for {}'.format(callee.name)
self.fail(msg, context)

def too_many_arguments(self, callee: Callable, context: Context) -> None:
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/data/check-kwargs.test
Expand Up @@ -153,7 +153,7 @@ import typing
def f(x, y=A()): pass
f(x=A(), y=A())
f(y=A(), x=A())
f(y=A()) # E: Too few arguments for "f"
f(y=A()) # E: Missing positional argument "x" in call to "f"
f(A(), z=A()) # E: Unexpected keyword argument "z" for "f"
class A: pass

Expand Down
2 changes: 1 addition & 1 deletion mypy/test/data/check-namedtuple.test
Expand Up @@ -35,7 +35,7 @@ X = namedtuple('X', ['x', 'y'])
x = X(x=1, y='x')
x = X(1, y='x')
x = X(x=1, z=1) # E: Unexpected keyword argument "z" for "X"
x = X(y=1) # E: Too few arguments for "X"
x = X(y=1) # E: Missing positional argument "x" in call to "X"

[case testCreateNamedTupleAndUseAsTuple]
from collections import namedtuple
Expand Down