Skip to content

Commit 1b736f8

Browse files
argparse: Use Literal for action & nargs add_argument parameter types
1 parent 2279c87 commit 1b736f8

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

stdlib/argparse.pyi

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
2-
from typing import IO, Any, Callable, Generator, Generic, Iterable, NoReturn, Pattern, Protocol, Sequence, TypeVar, overload
2+
from typing import IO, Any, Callable, Generator, Generic, Iterable, NewType, NoReturn, Pattern, Protocol, Sequence, TypeVar, overload
3+
from typing_extensions import Literal
34

45
if sys.version_info >= (3, 9):
56
__all__ = [
@@ -48,12 +49,13 @@ _ActionT = TypeVar("_ActionT", bound=Action)
4849
_ArgumentParserT = TypeVar("_ArgumentParserT", bound=ArgumentParser)
4950
_N = TypeVar("_N")
5051

51-
ONE_OR_MORE: str
52-
OPTIONAL: str
53-
PARSER: str
54-
REMAINDER: str
55-
SUPPRESS: str
56-
ZERO_OR_MORE: str
52+
ONE_OR_MORE: Literal["+"]
53+
OPTIONAL: Literal["?"]
54+
PARSER: Literal["A..."]
55+
REMAINDER: Literal["..."]
56+
_SUPPRESS_T = NewType('_SUPPRESS_T', str)
57+
SUPPRESS: _SUPPRESS_T # not using Literal because argparse sometimes compares SUPPRESS with is
58+
ZERO_OR_MORE: Literal["*"]
5759
_UNRECOGNIZED_ARGS_ATTR: str # undocumented
5860

5961
class ArgumentError(Exception):
@@ -89,8 +91,11 @@ class _ActionsContainer:
8991
def add_argument(
9092
self,
9193
*name_or_flags: str,
92-
action: str | type[Action] = ...,
93-
nargs: int | str = ...,
94+
action: Literal[
95+
"store", "store_const", "store_true", "store_false", "append", "append_const", "count", "help", "version", "extend"
96+
]
97+
| type[Action] = ...,
98+
nargs: int | Literal["?", "*", "+", "...", "A...", "==SUPPRESS=="] | _SUPPRESS_T = ...,
9499
const: Any = ...,
95100
default: Any = ...,
96101
type: Callable[[str], _T] | FileType = ...,

0 commit comments

Comments
 (0)