diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index 103af47c7524..84132e4cbae0 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -190,6 +190,8 @@ WriteableBuffer = Union[bytearray, memoryview, array.array[Any], mmap.mmap, ctyp # Same as _WriteableBuffer, but also includes read-only buffer types (like bytes). ReadableBuffer = Union[ReadOnlyBuffer, WriteableBuffer] # stable +MatchString = TypeVar("MatchString", bound=str | bytes | ReadableBuffer) + # stable if sys.version_info >= (3, 10): from types import NoneType as NoneType diff --git a/stdlib/re.pyi b/stdlib/re.pyi index 01a60d170c50..e0468f73e08b 100644 --- a/stdlib/re.pyi +++ b/stdlib/re.pyi @@ -3,6 +3,8 @@ import sys from sre_constants import error as error from typing import Any, AnyStr, Callable, Iterator, Union, overload +from stdlib._typeshed import MatchString + # ----- re variables and constants ----- if sys.version_info >= (3, 7): from typing import Match as Match, Pattern as Pattern @@ -60,9 +62,9 @@ def search(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Match[An @overload def search(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... @overload -def match(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... +def match(pattern: AnyStr, string: MatchString, flags: _FlagsType = ...) -> Match[AnyStr, MatchString] | None: ... @overload -def match(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... +def match(pattern: Pattern[AnyStr], string: MatchString, flags: _FlagsType = ...) -> Match[AnyStr, MatchString] | None: ... # New in Python 3.4 @overload diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 5a8557f909e5..b7294ab18c7f 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -1,6 +1,6 @@ import collections # Needed by aliases like DefaultDict, see mypy issue 2986 import sys -from _typeshed import Self, SupportsKeysAndGetItem +from _typeshed import MatchString, ReadableBuffer, Self, SupportsKeysAndGetItem, T_ReadableBuffer from abc import ABCMeta, abstractmethod from types import BuiltinFunctionType, CodeType, FrameType, FunctionType, MethodType, ModuleType, TracebackType from typing_extensions import Literal as _Literal, ParamSpec as _ParamSpec, final as _final @@ -587,12 +587,12 @@ class TextIO(IO[str]): class ByteString(Sequence[int], metaclass=ABCMeta): ... @_final -class Match(Generic[AnyStr]): +class Match(Generic[AnyStr, MatchString]): pos: int endpos: int lastindex: int | None lastgroup: str | None - string: AnyStr + string: MatchString # The regular expression object whose match() or search() method produced # this match instance. @@ -636,8 +636,9 @@ class Pattern(Generic[AnyStr]): groupindex: Mapping[str, int] groups: int pattern: AnyStr - def search(self, string: AnyStr, pos: int = ..., endpos: int = ...) -> Match[AnyStr] | None: ... - def match(self, string: AnyStr, pos: int = ..., endpos: int = ...) -> Match[AnyStr] | None: ... + + def search(self, string: AnyStr, pos: int = ..., endpos: int = ...) -> Match[AnyStr, AnyStr] | None: ... + def match(self, string: MatchString, pos: int = ..., endpos: int = ...) -> Match[AnyStr, MatchString] | None: ... def fullmatch(self, string: AnyStr, pos: int = ..., endpos: int = ...) -> Match[AnyStr] | None: ... def split(self, string: AnyStr, maxsplit: int = ...) -> list[AnyStr | Any]: ... def findall(self, string: AnyStr, pos: int = ..., endpos: int = ...) -> list[Any]: ...