Skip to content

Commit 89b5afa

Browse files
authored
Clean up argparse hacks (#13450)
1 parent d704a7d commit 89b5afa

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

stdlib/argparse.pyi

+10-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import sys
22
from _typeshed import SupportsWrite, sentinel
33
from collections.abc import Callable, Generator, Iterable, Sequence
44
from re import Pattern
5-
from typing import IO, Any, ClassVar, Final, Generic, NewType, NoReturn, Protocol, TypeVar, overload
5+
from typing import IO, Any, ClassVar, Final, Generic, NoReturn, Protocol, TypeVar, overload
66
from typing_extensions import Self, TypeAlias, deprecated
77

88
__all__ = [
@@ -33,25 +33,14 @@ _ActionT = TypeVar("_ActionT", bound=Action)
3333
_ArgumentParserT = TypeVar("_ArgumentParserT", bound=ArgumentParser)
3434
_N = TypeVar("_N")
3535
_ActionType: TypeAlias = Callable[[str], Any] | FileType | str
36-
# more precisely, Literal["store", "store_const", "store_true",
37-
# "store_false", "append", "append_const", "count", "help", "version",
38-
# "extend"], but using this would make it hard to annotate callers
39-
# that don't use a literal argument
40-
_ActionStr: TypeAlias = str
41-
# more precisely, Literal["?", "*", "+", "...", "A...",
42-
# "==SUPPRESS=="], but using this would make it hard to annotate
43-
# callers that don't use a literal argument
44-
_NArgsStr: TypeAlias = str
4536

4637
ONE_OR_MORE: Final = "+"
4738
OPTIONAL: Final = "?"
4839
PARSER: Final = "A..."
4940
REMAINDER: Final = "..."
50-
_SUPPRESS_T = NewType("_SUPPRESS_T", str)
51-
SUPPRESS: _SUPPRESS_T | str # not using Literal because argparse sometimes compares SUPPRESS with is
52-
# the | str is there so that foo = argparse.SUPPRESS; foo = "test" checks out in mypy
41+
SUPPRESS: Final = "==SUPPRESS=="
5342
ZERO_OR_MORE: Final = "*"
54-
_UNRECOGNIZED_ARGS_ATTR: Final[str] # undocumented
43+
_UNRECOGNIZED_ARGS_ATTR: Final = "_unrecognized_args" # undocumented
5544

5645
class ArgumentError(Exception):
5746
argument_name: str | None
@@ -86,8 +75,13 @@ class _ActionsContainer:
8675
def add_argument(
8776
self,
8877
*name_or_flags: str,
89-
action: _ActionStr | type[Action] = ...,
90-
nargs: int | _NArgsStr | _SUPPRESS_T | None = None,
78+
# str covers predefined actions ("store_true", "count", etc.)
79+
# and user registered actions via the `register` method.
80+
action: str | type[Action] = ...,
81+
# more precisely, Literal["?", "*", "+", "...", "A...", "==SUPPRESS=="],
82+
# but using this would make it hard to annotate callers that don't use a
83+
# literal argument and for subclasses to override this method.
84+
nargs: int | str | None = None,
9185
const: Any = ...,
9286
default: Any = ...,
9387
type: _ActionType = ...,

0 commit comments

Comments
 (0)