-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Better callable: Callable[[Arg('x', int), VarArg(str)], int] now a thing you can do
#2607
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
Changes from 1 commit
249d70f
7ecdcc1
066bd5e
213944b
0e19070
3f2f617
0b69630
f4ccf92
967bb5a
bb5134e
d4a83e1
54a5da9
e79c527
52ffe5c
06416f7
398fbad
2c9ce02
51c6f56
5e679a3
0926fe9
288a8be
6e67ab2
97a859b
1c7d4c6
f153850
be954f5
07ae917
1b97362
552f49e
f2e3663
27e2a9d
793a663
3d212b3
0780149
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| from abc import abstractmethod | ||
|
|
||
| from typing import ( | ||
| Any, TypeVar, List, Tuple, cast, Set, Dict, Union, Optional | ||
| Any, TypeVar, List, Tuple, cast, Set, Dict, Union, Optional, Callable, | ||
| ) | ||
|
|
||
| import mypy.strconv | ||
|
|
@@ -2447,3 +2447,43 @@ def get_member_expr_fullname(expr: MemberExpr) -> str: | |
| for key, obj in globals().items() | ||
| if isinstance(obj, type) and issubclass(obj, SymbolNode) and obj is not SymbolNode | ||
| } | ||
|
|
||
| def check_arg_kinds(arg_kinds: List[int], nodes: List[T], fail: Callable[[str, T], None]) -> None: | ||
| is_var_arg = False | ||
| is_kw_arg = False | ||
| seen_named = False | ||
| seen_opt = False | ||
| for kind, node in zip(arg_kinds, nodes): | ||
| if kind == ARG_POS: | ||
| if is_var_arg or is_kw_arg or seen_named or seen_opt: | ||
| fail("Required positional args may not appear " | ||
| "after default, named or star args", | ||
| node) | ||
| break | ||
| elif kind == ARG_OPT: | ||
| if is_var_arg or is_kw_arg or seen_named: | ||
| fail("Positional default args may not appear after named or star args", node) | ||
| break | ||
| seen_opt = True | ||
| elif kind == ARG_STAR: | ||
| if is_var_arg or is_kw_arg or seen_named: | ||
| fail("Star args may not appear after named or star args", node) | ||
| break | ||
| is_var_arg = True | ||
| elif kind == ARG_NAMED or kind == ARG_NAMED_OPT: | ||
|
Collaborator
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. Maybe we shouldn't allow these after
Collaborator
Author
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. Done |
||
| seen_named = True | ||
| elif kind == ARG_STAR2: | ||
| if is_kw_arg: | ||
| fail("You may only have one **kwargs argument", node) | ||
| break | ||
| is_kw_arg = True | ||
|
|
||
|
|
||
| def check_arg_names(names: List[str], nodes: List[T], fail: Callable[[str, T], None], | ||
| description: str = 'function definition') -> None: | ||
| seen_names = set() # type: Set[str] | ||
| for name, node in zip(names, nodes): | ||
| if name is not None and name in seen_names: | ||
| fail("duplicate argument '{}' in {}".format(name, description), node) | ||
|
||
| break | ||
| seen_names.add(name) | ||
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.
Change messaging to refer to "var args"