Ensure required keyword-only arguments are provided.#2441
Ensure required keyword-only arguments are provided.#2441JukkaL merged 7 commits intopython:masterfrom
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.
Changes to this file seem to have no effect.
There was a problem hiding this comment.
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.
Style nit: you can use a set comprehension here.
There was a problem hiding this comment.
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.
Style nit: You can use a set literal here.
| @@ -115,13 +115,20 @@ main:4: error: Unexpected keyword argument "b" | |||
| [case testKeywordOnlyArguments] | |||
There was a problem hiding this comment.
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.
Add test for multiple missing keyword-only arguments. Maybe also test a mix of optional and required keyword-only arguments?
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.
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.