-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Ensure required keyword-only arguments are provided. #2441
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
Conversation
This should fix #2437 |
Before, mypy would assume all keyword-only arguments were optional, and not provide an error if you failed to provide a keyword-only argument at a call site. Now mypy provides you with a helpful error.
mypy/subtypes.py
Outdated
@@ -1,4 +1,4 @@ | |||
from typing import List, Dict, Callable | |||
from typing import List, Dict, Callable, Optional |
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.
Changes to this file seem to have no effect.
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.
You're right, they must have snuck in from another part of the stacked diffs I'm managing.
mypy/strconv.py
Outdated
a = self.func_helper(o) | ||
a.insert(0, o.name()) | ||
if mypy.nodes.ARG_NAMED in [arg.kind for arg in o.arguments]: | ||
arg_kinds = set([arg.kind for arg in o.arguments]) |
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.
Style nit: you can use a set comprehension here.
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.
I forgot about those! Thank you.
mypy/strconv.py
Outdated
a.insert(0, o.name()) | ||
if mypy.nodes.ARG_NAMED in [arg.kind for arg in o.arguments]: | ||
arg_kinds = set([arg.kind for arg in o.arguments]) | ||
if len(arg_kinds & set([mypy.nodes.ARG_NAMED, mypy.nodes.ARG_NAMED_OPT])) > 0: |
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.
Style nit: You can use a set literal here.
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.
And those!
main:3: error: Unexpected keyword argument "b" | ||
main:4: error: Unexpected keyword argument "b" | ||
|
||
[case testKeywordOnlyArguments] |
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.
Add a copy of this test case that uses the fast parser (# flags: --fast-parser
in the test case).
f(A(), B()) # E: Too many positional arguments for "f" | ||
g(A(), b=B()) | ||
g(b=B(), a=A()) | ||
g(A()) # E: Missing named argument "b" for function "g" |
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.
Add test for multiple missing keyword-only arguments. Maybe also test a mix of optional and required keyword-only arguments?
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.
Will do.
Also extends the test runner to allow more than one error per line.
mypy/test/data.py
Outdated
else: | ||
output.append('{}:{}:{}: {}: {}'.format( | ||
fnam, i + 1, col, severity, m.group('message'))) | ||
for possible_err_comment in input[i].split(' #'): |
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.
It would be slightly better if this was ... input[i].split(' #')[1:]:
so that the non-comment part of the line won't get processed (though this is unlikely to cause problems).
Can you look at the test failures? Otherwise looks good, thanks for the updates! |
Introduces ARG_NAMED_OPT, a kind for optional keyword-only arguments.