Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optparse: Improve Option typing #13282

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions stdlib/optparse.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import builtins
from _typeshed import Incomplete, MaybeNone
from abc import abstractmethod
from collections.abc import Callable, Iterable, Mapping, Sequence
from typing import IO, Any, AnyStr, Literal, NoReturn, overload
from typing_extensions import Self

__all__ = [
"Option",
Expand All @@ -27,8 +29,8 @@ NO_DEFAULT: tuple[str, ...]
SUPPRESS_HELP: str
SUPPRESS_USAGE: str

def check_builtin(option: Option, opt, value: str): ...
def check_choice(option: Option, opt, value: str) -> str: ...
def check_builtin(option: Option, opt: str, value: str) -> complex: ...
hamdanal marked this conversation as resolved.
Show resolved Hide resolved
def check_choice(option: Option, opt: str, value: str) -> str: ...

class OptParseError(Exception):
msg: str
Expand Down Expand Up @@ -99,25 +101,46 @@ class Option:
ACTIONS: tuple[str, ...]
ALWAYS_TYPED_ACTIONS: tuple[str, ...]
ATTRS: list[str]
CHECK_METHODS: list[Callable[..., Incomplete]] | None
CHECK_METHODS: list[Callable[[Self], object]] | None
CONST_ACTIONS: tuple[str, ...]
STORE_ACTIONS: tuple[str, ...]
TYPED_ACTIONS: tuple[str, ...]
TYPES: tuple[str, ...]
TYPE_CHECKER: dict[str, Callable[[Option, str, Incomplete], Any]]
TYPE_CHECKER: dict[str, Callable[[Option, str, str], Any]]
_long_opts: list[str]
_short_opts: list[str]
action: str
type: str | None
dest: str | None
default: Incomplete
default: Any
nargs: int
type: Incomplete
const: Any | None
choices: list[str] | tuple[str, ...] | None
callback: Callable[..., Incomplete] | None
callback_args: tuple[Incomplete, ...] | None
callback_kwargs: dict[str, Incomplete] | None
help: str | None
metavar: str | None
def __init__(self, *opts: str | None, **attrs) -> None: ...
def __init__(
self,
*opts: str | None,
# The following keywords are handled by the _set_attrs method. All default to
# `None` except for `default`, which defaults to `NO_DEFAULT`.
action: str | None = None,
type: str | builtins.type | None = None,
dest: str | None = None,
default: Any = ..., # = NO_DEFAULT
nargs: int | None = None,
const: Any | None = None,
choices: list[str] | tuple[str, ...] | None = None,
# TODO: callback, callback_args, callback_kwargs must be all supplied or all omitted. Add overloads.
# Revisit if ParamSpec is ever changed to support non-unpacked args and kwargs.
callback: Callable[..., Incomplete] | None = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these Incompletes be Any as the correct type cannot currently be represented in the type system?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. Let’s change it later, after I have done some testing. I’m not done with optparse yet.

callback_args: tuple[Incomplete, ...] | None = None,
callback_kwargs: dict[str, Incomplete] | None = None,
help: str | None = None,
metavar: str | None = None,
) -> None: ...
def _check_action(self) -> None: ...
def _check_callback(self) -> None: ...
def _check_choice(self) -> None: ...
Expand All @@ -128,11 +151,11 @@ class Option:
def _check_type(self) -> None: ...
def _set_attrs(self, attrs: dict[str, Incomplete]) -> None: ...
def _set_opt_strings(self, opts: Iterable[str]) -> None: ...
def check_value(self, opt: str, value): ...
def convert_value(self, opt: str, value): ...
def check_value(self, opt: str, value: str) -> Any: ...
def convert_value(self, opt: str, value: str | tuple[str, ...] | None) -> Any: ...
def get_opt_string(self) -> str: ...
def process(self, opt, value, values, parser: OptionParser) -> int: ...
def take_action(self, action: str, dest: str, opt, value, values, parser: OptionParser) -> int: ...
def process(self, opt: str, value: str | tuple[str, ...] | None, values: Values, parser: OptionParser) -> int: ...
def take_action(self, action: str, dest: str, opt: str, value: Any, values: Values, parser: OptionParser) -> int: ...
def takes_value(self) -> bool: ...

make_option = Option
Expand Down
Loading