mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-08 04:54:47 +08:00
argparse: Use Literal for action & nargs add_argument parameter types (#7329)
This commit is contained in:
@@ -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,14 @@ _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 | 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
|
||||
|
||||
class ArgumentError(Exception):
|
||||
@@ -89,8 +106,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 = ...,
|
||||
|
||||
Reference in New Issue
Block a user