Skip to content

Commit 52dd232

Browse files
authored
Rework Match.group handling (#5557)
* Rework Match.group handling Standardize group(), groups(), groupdict(), and __getattr__() to return Any instead of AnyStr. Also special case group(0) and __getattr__(0) to always return AnyStr. Use PEP 604 for unions in changed lines.
1 parent 63fd168 commit 52dd232

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

stdlib/typing.pyi

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import collections # Needed by aliases like DefaultDict, see mypy issue 2986
22
import sys
33
from abc import ABCMeta, abstractmethod
44
from types import BuiltinFunctionType, CodeType, FrameType, FunctionType, MethodType, ModuleType, TracebackType
5+
from typing_extensions import Literal as _Literal
56

67
if sys.version_info >= (3, 7):
78
from types import MethodDescriptorType, MethodWrapperType, WrapperDescriptorType
@@ -563,19 +564,35 @@ class Match(Generic[AnyStr]):
563564
# this match instance.
564565
re: Pattern[AnyStr]
565566
def expand(self, template: AnyStr) -> AnyStr: ...
566-
# TODO: The return for a group may be None, except if __group is 0 or not given.
567+
# group() returns "AnyStr" or "AnyStr | None", depending on the pattern.
567568
@overload
568-
def group(self, __group: Union[str, int] = ...) -> AnyStr: ...
569+
def group(self, __group: _Literal[0] = ...) -> AnyStr: ...
569570
@overload
570-
def group(self, __group1: Union[str, int], __group2: Union[str, int], *groups: Union[str, int]) -> Tuple[AnyStr, ...]: ...
571-
def groups(self, default: AnyStr = ...) -> Sequence[AnyStr]: ...
572-
def groupdict(self, default: AnyStr = ...) -> dict[str, AnyStr]: ...
571+
def group(self, __group: str | int) -> AnyStr | Any: ...
572+
@overload
573+
def group(self, __group1: str | int, __group2: str | int, *groups: str | int) -> Tuple[AnyStr | Any, ...]: ...
574+
# Each item of groups()'s return tuple is either "AnyStr" or
575+
# "AnyStr | None", depending on the pattern.
576+
@overload
577+
def groups(self) -> Tuple[AnyStr | Any, ...]: ...
578+
@overload
579+
def groups(self, default: _T) -> Tuple[AnyStr | _T, ...]: ...
580+
# Each value in groupdict()'s return dict is either "AnyStr" or
581+
# "AnyStr | None", depending on the pattern.
582+
@overload
583+
def groupdict(self) -> dict[str, AnyStr | Any]: ...
584+
@overload
585+
def groupdict(self, default: _T) -> dict[str, AnyStr | _T]: ...
573586
def start(self, __group: Union[int, str] = ...) -> int: ...
574587
def end(self, __group: Union[int, str] = ...) -> int: ...
575588
def span(self, __group: Union[int, str] = ...) -> Tuple[int, int]: ...
576589
@property
577590
def regs(self) -> Tuple[Tuple[int, int], ...]: ... # undocumented
578-
def __getitem__(self, g: Union[int, str]) -> AnyStr: ...
591+
# __getitem__() returns "AnyStr" or "AnyStr | None", depending on the pattern.
592+
@overload
593+
def __getitem__(self, __key: _Literal[0]) -> AnyStr: ...
594+
@overload
595+
def __getitem__(self, __key: int | str) -> AnyStr | Any: ...
579596
if sys.version_info >= (3, 9):
580597
def __class_getitem__(cls, item: Any) -> GenericAlias: ...
581598

@@ -586,7 +603,6 @@ class Pattern(Generic[AnyStr]):
586603
pattern: AnyStr
587604
def search(self, string: AnyStr, pos: int = ..., endpos: int = ...) -> Optional[Match[AnyStr]]: ...
588605
def match(self, string: AnyStr, pos: int = ..., endpos: int = ...) -> Optional[Match[AnyStr]]: ...
589-
# New in Python 3.4
590606
def fullmatch(self, string: AnyStr, pos: int = ..., endpos: int = ...) -> Optional[Match[AnyStr]]: ...
591607
def split(self, string: AnyStr, maxsplit: int = ...) -> list[AnyStr]: ...
592608
def findall(self, string: AnyStr, pos: int = ..., endpos: int = ...) -> list[Any]: ...

0 commit comments

Comments
 (0)