From 25ad2b2092354722b53df45c729b1136f6ae2b84 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Sun, 20 Feb 2022 19:24:15 +0100 Subject: [PATCH 1/2] argparse: Use Literal for action & nargs add_argument parameter types --- stdlib/argparse.pyi | 37 ++++++++++++++++++------ tests/stubtest_allowlists/py3_common.txt | 1 + 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/stdlib/argparse.pyi b/stdlib/argparse.pyi index 78fa03dc42c1..373eba1be9df 100644 --- a/stdlib/argparse.pyi +++ b/stdlib/argparse.pyi @@ -1,5 +1,20 @@ import sys -from typing import IO, Any, Callable, Generator, Generic, Iterable, NoReturn, Pattern, Protocol, Sequence, TypeVar, overload +from typing import ( + IO, + Any, + Callable, + Generator, + Generic, + Iterable, + NewType, + NoReturn, + Pattern, + Protocol, + Sequence, + TypeVar, + overload, +) +from typing_extensions import Literal if sys.version_info >= (3, 9): __all__ = [ @@ -48,12 +63,13 @@ _ActionT = TypeVar("_ActionT", bound=Action) _ArgumentParserT = TypeVar("_ArgumentParserT", bound=ArgumentParser) _N = TypeVar("_N") -ONE_OR_MORE: str -OPTIONAL: str -PARSER: str -REMAINDER: str -SUPPRESS: str -ZERO_OR_MORE: str +ONE_OR_MORE: Literal["+"] +OPTIONAL: Literal["?"] +PARSER: Literal["A..."] +REMAINDER: Literal["..."] +_SUPPRESS_T = NewType("_SUPPRESS_T", str) +SUPPRESS: _SUPPRESS_T # not using Literal because argparse sometimes compares SUPPRESS with is +ZERO_OR_MORE: Literal["*"] _UNRECOGNIZED_ARGS_ATTR: str # undocumented class ArgumentError(Exception): @@ -89,8 +105,11 @@ class _ActionsContainer: def add_argument( self, *name_or_flags: str, - action: str | type[Action] = ..., - nargs: int | str = ..., + action: Literal[ + "store", "store_const", "store_true", "store_false", "append", "append_const", "count", "help", "version", "extend" + ] + | type[Action] = ..., + nargs: int | Literal["?", "*", "+", "...", "A...", "==SUPPRESS=="] | _SUPPRESS_T = ..., const: Any = ..., default: Any = ..., type: Callable[[str], _T] | FileType = ..., diff --git a/tests/stubtest_allowlists/py3_common.txt b/tests/stubtest_allowlists/py3_common.txt index c37e12f26f9e..ed37dd0fa053 100644 --- a/tests/stubtest_allowlists/py3_common.txt +++ b/tests/stubtest_allowlists/py3_common.txt @@ -236,6 +236,7 @@ xml.parsers.expat.expat_CAPI # Allowlist entries that cannot or should not be fixed # ========== _pydecimal.* # See comments in file +argparse.SUPPRESS # See comment in file ast.NodeVisitor.visit_\w+ # Methods are discovered dynamically, see #3796 # Weird special builtins that are typed as functions, but aren't functions builtins.copyright From e43176e8b7549833763d59dceed7b027a3724514 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Mon, 21 Feb 2022 00:57:08 +0100 Subject: [PATCH 2/2] argparse: add |str to SUPPRESS type --- stdlib/argparse.pyi | 3 ++- tests/stubtest_allowlists/py3_common.txt | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/argparse.pyi b/stdlib/argparse.pyi index 373eba1be9df..ad54660cc45d 100644 --- a/stdlib/argparse.pyi +++ b/stdlib/argparse.pyi @@ -68,7 +68,8 @@ OPTIONAL: Literal["?"] PARSER: Literal["A..."] REMAINDER: Literal["..."] _SUPPRESS_T = NewType("_SUPPRESS_T", str) -SUPPRESS: _SUPPRESS_T # not using Literal because argparse sometimes compares SUPPRESS with is +SUPPRESS: _SUPPRESS_T | str # not using Literal because argparse sometimes compares SUPPRESS with is +# the | str is there so that foo = argparse.SUPPRESS; foo = "test" checks out in mypy ZERO_OR_MORE: Literal["*"] _UNRECOGNIZED_ARGS_ATTR: str # undocumented diff --git a/tests/stubtest_allowlists/py3_common.txt b/tests/stubtest_allowlists/py3_common.txt index ed37dd0fa053..c37e12f26f9e 100644 --- a/tests/stubtest_allowlists/py3_common.txt +++ b/tests/stubtest_allowlists/py3_common.txt @@ -236,7 +236,6 @@ xml.parsers.expat.expat_CAPI # Allowlist entries that cannot or should not be fixed # ========== _pydecimal.* # See comments in file -argparse.SUPPRESS # See comment in file ast.NodeVisitor.visit_\w+ # Methods are discovered dynamically, see #3796 # Weird special builtins that are typed as functions, but aren't functions builtins.copyright