Skip to content

Commit 1de7826

Browse files
authored
optparse: Improve Option typing (#13282)
1 parent ea91db2 commit 1de7826

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

stdlib/optparse.pyi

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import builtins
12
from _typeshed import Incomplete, MaybeNone
23
from abc import abstractmethod
34
from collections.abc import Callable, Iterable, Mapping, Sequence
45
from typing import IO, Any, AnyStr, Literal, NoReturn, overload
6+
from typing_extensions import Self
57

68
__all__ = [
79
"Option",
@@ -27,8 +29,9 @@ NO_DEFAULT: tuple[str, ...]
2729
SUPPRESS_HELP: str
2830
SUPPRESS_USAGE: str
2931

30-
def check_builtin(option: Option, opt, value: str): ...
31-
def check_choice(option: Option, opt, value: str) -> str: ...
32+
# Can return complex, float, or int depending on the option's type
33+
def check_builtin(option: Option, opt: str, value: str) -> complex: ...
34+
def check_choice(option: Option, opt: str, value: str) -> str: ...
3235

3336
class OptParseError(Exception):
3437
msg: str
@@ -109,25 +112,46 @@ class Option:
109112
ACTIONS: tuple[str, ...]
110113
ALWAYS_TYPED_ACTIONS: tuple[str, ...]
111114
ATTRS: list[str]
112-
CHECK_METHODS: list[Callable[..., Incomplete]] | None
115+
CHECK_METHODS: list[Callable[[Self], object]] | None
113116
CONST_ACTIONS: tuple[str, ...]
114117
STORE_ACTIONS: tuple[str, ...]
115118
TYPED_ACTIONS: tuple[str, ...]
116119
TYPES: tuple[str, ...]
117-
TYPE_CHECKER: dict[str, Callable[[Option, str, Incomplete], Any]]
120+
TYPE_CHECKER: dict[str, Callable[[Option, str, str], Any]]
118121
_long_opts: list[str]
119122
_short_opts: list[str]
120123
action: str
124+
type: str | None
121125
dest: str | None
122-
default: Incomplete
126+
default: Any
123127
nargs: int
124-
type: Incomplete
128+
const: Any | None
129+
choices: list[str] | tuple[str, ...] | None
125130
callback: Callable[..., Incomplete] | None
126131
callback_args: tuple[Incomplete, ...] | None
127132
callback_kwargs: dict[str, Incomplete] | None
128133
help: str | None
129134
metavar: str | None
130-
def __init__(self, *opts: str | None, **attrs) -> None: ...
135+
def __init__(
136+
self,
137+
*opts: str | None,
138+
# The following keywords are handled by the _set_attrs method. All default to
139+
# `None` except for `default`, which defaults to `NO_DEFAULT`.
140+
action: str | None = None,
141+
type: str | builtins.type | None = None,
142+
dest: str | None = None,
143+
default: Any = ..., # = NO_DEFAULT
144+
nargs: int | None = None,
145+
const: Any | None = None,
146+
choices: list[str] | tuple[str, ...] | None = None,
147+
# TODO: callback, callback_args, callback_kwargs must be all supplied or all omitted. Add overloads.
148+
# Revisit if ParamSpec is ever changed to support non-unpacked args and kwargs.
149+
callback: Callable[..., Incomplete] | None = None,
150+
callback_args: tuple[Incomplete, ...] | None = None,
151+
callback_kwargs: dict[str, Incomplete] | None = None,
152+
help: str | None = None,
153+
metavar: str | None = None,
154+
) -> None: ...
131155
def _check_action(self) -> None: ...
132156
def _check_callback(self) -> None: ...
133157
def _check_choice(self) -> None: ...
@@ -138,11 +162,11 @@ class Option:
138162
def _check_type(self) -> None: ...
139163
def _set_attrs(self, attrs: dict[str, Incomplete]) -> None: ...
140164
def _set_opt_strings(self, opts: Iterable[str]) -> None: ...
141-
def check_value(self, opt: str, value): ...
142-
def convert_value(self, opt: str, value): ...
165+
def check_value(self, opt: str, value: str) -> Any: ...
166+
def convert_value(self, opt: str, value: str | tuple[str, ...] | None) -> Any: ...
143167
def get_opt_string(self) -> str: ...
144-
def process(self, opt, value, values, parser: OptionParser) -> int: ...
145-
def take_action(self, action: str, dest: str, opt, value, values, parser: OptionParser) -> int: ...
168+
def process(self, opt: str, value: str | tuple[str, ...] | None, values: Values, parser: OptionParser) -> int: ...
169+
def take_action(self, action: str, dest: str, opt: str, value: Any, values: Values, parser: OptionParser) -> int: ...
146170
def takes_value(self) -> bool: ...
147171

148172
make_option = Option

0 commit comments

Comments
 (0)