argparse: Forbid invalid type and choices combinations#12211
argparse: Forbid invalid type and choices combinations#12211srittau wants to merge 7 commits intopython:mainfrom
type and choices combinations#12211Conversation
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Diff from mypy_primer, showing the effect of this PR on open source code: pylint (https://github.com/pycqa/pylint)
- pylint/config/arguments_manager.py:136: error: Unused "type: ignore" comment [unused-ignore]
- pylint/config/arguments_manager.py:147: error: Unused "type: ignore" comment [unused-ignore]
- pylint/config/arguments_manager.py:160: error: Unused "type: ignore" comment [unused-ignore]
- pylint/config/arguments_manager.py:171: error: Unused "type: ignore" comment [unused-ignore]
- pylint/config/arguments_manager.py:196: error: Unused "type: ignore" comment [unused-ignore]
mkosi (https://github.com/systemd/mkosi)
+ mkosi/config.py:3146:5: error: No overload variant of "add_argument" of "_ActionsContainer" matches argument types "str", "str", "object", "Path", "str", "str" [call-overload]
+ mkosi/config.py:3146:5: note: Possible overload variants:
+ mkosi/config.py:3146:5: note: def add_argument(self, *name_or_flags: str, action: Union[str, type[Action]] = ..., nargs: Union[int, str, _SUPPRESS_T, None] = ..., const: Any = ..., default: Any = ..., type: FileType, choices: None = ..., required: bool = ..., help: Optional[str] = ..., metavar: Union[str, tuple[str, ...], None] = ..., dest: Optional[str] = ..., version: str = ..., **kwargs: Any) -> Action
+ mkosi/config.py:3146:5: note: def [_T] add_argument(self, *name_or_flags: str, action: Union[str, type[Action]] = ..., nargs: Union[int, str, _SUPPRESS_T, None] = ..., const: Any = ..., default: Any = ..., type: Callable[[str], _T], choices: Optional[Iterable[_T]] = ..., required: bool = ..., help: Optional[str] = ..., metavar: Union[str, tuple[str, ...], None] = ..., dest: Optional[str] = ..., version: str = ..., **kwargs: Any) -> Action
+ mkosi/config.py:3146:5: note: def add_argument(self, *name_or_flags: str, action: Union[str, type[Action]] = ..., nargs: Union[int, str, _SUPPRESS_T, None] = ..., const: Any = ..., default: Any = ..., type: str, choices: Optional[Iterable[Any]] = ..., required: bool = ..., help: Optional[str] = ..., metavar: Union[str, tuple[str, ...], None] = ..., dest: Optional[str] = ..., version: str = ..., **kwargs: Any) -> Action
+ mkosi/config.py:3146:5: note: def add_argument(self, *name_or_flags: str, action: Union[str, type[Action]] = ..., nargs: Union[int, str, _SUPPRESS_T, None] = ..., const: Any = ..., default: Any = ..., type: NoReturn = ..., choices: Optional[Iterable[str]] = ..., required: bool = ..., help: Optional[str] = ..., metavar: Union[str, tuple[str, ...], None] = ..., dest: Optional[str] = ..., version: str = ..., **kwargs: Any) -> Action
|
|
I'm unsure why the pyright regression tests fail. I can't see how the two ignored tests can match any of the overloads: |
|
Regarding the primer output: pylint is actually using The mkosi problem is mypy's overzealous base class finding behavior. I.e., mypy thinks that the type of |
|
python/mypy#17427 should hopefully fix the issue with ternaries. |
|
(But I don't think we need to wait for that mypy PR.) |
|
@erictraut Could you have a look at https://github.com/python/typeshed/pull/12211/files? I'm not sure why pyright thinks the lines in question are okay. I don't think any potential should match, but maybe I'm overlooking something? |
|
I presume you're talking about the lines with The second overload ('If from typing import Callable, Iterable
def add_argument[_T](
*name_or_flags: str, type: Callable[[str], _T], choices: Iterable[_T]
) -> _T: ...
x = add_argument("--foo", type=int, choices=[""])
reveal_type(x) # int | str |
|
That's interesting. mypy comes to a different conclusion (when using a slightly adapted example): mypy's interpretation (constraining Edit: https://discuss.python.org/t/constraining-generic-argument-types/56809 |
|
It seems that this is currently inexpressible in a way that's compatible with all type checkers. |
Deferred: See this discussion: https://discuss.python.org/t/constraining-generic-argument-types/56809