Skip to content

Commit 1e71351

Browse files
authored
Allow any sliceable sequence as getopt args (#13116)
1 parent b834c88 commit 1e71351

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

stdlib/distutils/fancy_getopt.pyi

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from collections.abc import Iterable, Mapping
2+
from getopt import _SliceableT, _StrSequenceT_co
23
from re import Pattern
34
from typing import Any, Final, overload
45
from typing_extensions import TypeAlias
56

67
_Option: TypeAlias = tuple[str, str | None, str]
7-
_GR: TypeAlias = tuple[list[str], OptionDummy]
88

99
longopt_pat: Final = r"[a-zA-Z](?:[a-zA-Z0-9-]*)"
1010
longopt_re: Final[Pattern[str]]
@@ -15,15 +15,25 @@ class FancyGetopt:
1515
def __init__(self, option_table: list[_Option] | None = None) -> None: ...
1616
# TODO kinda wrong, `getopt(object=object())` is invalid
1717
@overload
18-
def getopt(self, args: list[str] | None = None) -> _GR: ...
18+
def getopt(
19+
self, args: _SliceableT[_StrSequenceT_co] | None = None, object: None = None
20+
) -> tuple[_StrSequenceT_co, OptionDummy]: ...
1921
@overload
20-
def getopt(self, args: list[str] | None, object: Any) -> list[str]: ...
22+
def getopt(
23+
self, args: _SliceableT[_StrSequenceT_co] | None, object: Any
24+
) -> _StrSequenceT_co: ... # object is an arbitrary non-slotted object
2125
def get_option_order(self) -> list[tuple[str, str]]: ...
2226
def generate_help(self, header: str | None = None) -> list[str]: ...
2327

28+
# Same note as FancyGetopt.getopt
29+
@overload
2430
def fancy_getopt(
25-
options: list[_Option], negative_opt: Mapping[_Option, _Option], object: Any, args: list[str] | None
26-
) -> list[str] | _GR: ...
31+
options: list[_Option], negative_opt: Mapping[_Option, _Option], object: None, args: _SliceableT[_StrSequenceT_co] | None
32+
) -> tuple[_StrSequenceT_co, OptionDummy]: ...
33+
@overload
34+
def fancy_getopt(
35+
options: list[_Option], negative_opt: Mapping[_Option, _Option], object: Any, args: _SliceableT[_StrSequenceT_co] | None
36+
) -> _StrSequenceT_co: ...
2737

2838
WS_TRANS: Final[dict[int, str]]
2939

stdlib/getopt.pyi

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
from collections.abc import Iterable
1+
from collections.abc import Iterable, Sequence
2+
from typing import Protocol, TypeVar, overload, type_check_only
3+
4+
_StrSequenceT_co = TypeVar("_StrSequenceT_co", covariant=True, bound=Sequence[str])
5+
6+
@type_check_only
7+
class _SliceableT(Protocol[_StrSequenceT_co]):
8+
@overload
9+
def __getitem__(self, key: int, /) -> str: ...
10+
@overload
11+
def __getitem__(self, key: slice, /) -> _StrSequenceT_co: ...
212

313
__all__ = ["GetoptError", "error", "getopt", "gnu_getopt"]
414

5-
def getopt(args: list[str], shortopts: str, longopts: Iterable[str] | str = []) -> tuple[list[tuple[str, str]], list[str]]: ...
15+
def getopt(
16+
args: _SliceableT[_StrSequenceT_co], shortopts: str, longopts: Iterable[str] | str = []
17+
) -> tuple[list[tuple[str, str]], _StrSequenceT_co]: ...
618
def gnu_getopt(
7-
args: list[str], shortopts: str, longopts: Iterable[str] | str = []
19+
args: Sequence[str], shortopts: str, longopts: Iterable[str] | str = []
820
) -> tuple[list[tuple[str, str]], list[str]]: ...
921

1022
class GetoptError(Exception):

0 commit comments

Comments
 (0)