From 334431621d8eef8dded040c5c53cc22c79b9b548 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Mon, 7 Feb 2022 22:25:53 +0200 Subject: [PATCH 01/29] Make `mmap.mmap` extend `bytearray` --- stdlib/mmap.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/mmap.pyi b/stdlib/mmap.pyi index 1f44651dda00..58b67fd09bed 100644 --- a/stdlib/mmap.pyi +++ b/stdlib/mmap.pyi @@ -27,7 +27,7 @@ if sys.platform != "win32": PAGESIZE: int -class mmap(AbstractContextManager[mmap], Iterable[int], Sized): +class mmap(AbstractContextManager[mmap], bytearray, Iterable[int], Sized): if sys.platform == "win32": def __init__(self, fileno: int, length: int, tagname: str | None = ..., access: int = ..., offset: int = ...) -> None: ... else: From 60397325d15469ba0ed6039be4640a9424b74735 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Tue, 8 Feb 2022 20:05:59 +0200 Subject: [PATCH 02/29] Extend `ReadableBuffer` instead of `bytearray` --- stdlib/mmap.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/mmap.pyi b/stdlib/mmap.pyi index 58b67fd09bed..e5cd57d01011 100644 --- a/stdlib/mmap.pyi +++ b/stdlib/mmap.pyi @@ -27,7 +27,7 @@ if sys.platform != "win32": PAGESIZE: int -class mmap(AbstractContextManager[mmap], bytearray, Iterable[int], Sized): +class mmap(AbstractContextManager[mmap], ReadableBuffer, Iterable[int], Sized): if sys.platform == "win32": def __init__(self, fileno: int, length: int, tagname: str | None = ..., access: int = ..., offset: int = ...) -> None: ... else: From 74aa4641197b2bab2fb1d54ac883f4760a75ce90 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Tue, 8 Feb 2022 20:32:06 +0200 Subject: [PATCH 03/29] Fixup --- stdlib/mmap.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/mmap.pyi b/stdlib/mmap.pyi index e5cd57d01011..269f4191abd9 100644 --- a/stdlib/mmap.pyi +++ b/stdlib/mmap.pyi @@ -1,5 +1,5 @@ import sys -from _typeshed import ReadableBuffer +from _typeshed import ReadOnlyBuffer, ReadableBuffer from contextlib import AbstractContextManager from typing import Iterable, Iterator, NoReturn, Sized, overload @@ -27,7 +27,7 @@ if sys.platform != "win32": PAGESIZE: int -class mmap(AbstractContextManager[mmap], ReadableBuffer, Iterable[int], Sized): +class mmap(AbstractContextManager[mmap], ReadOnlyBuffer, Iterable[int], Sized): if sys.platform == "win32": def __init__(self, fileno: int, length: int, tagname: str | None = ..., access: int = ..., offset: int = ...) -> None: ... else: From 8666c6158dc59d8854a1079d84314dbed7ee6c0b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 8 Feb 2022 18:33:34 +0000 Subject: [PATCH 04/29] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/mmap.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/mmap.pyi b/stdlib/mmap.pyi index 269f4191abd9..521ea7a94117 100644 --- a/stdlib/mmap.pyi +++ b/stdlib/mmap.pyi @@ -1,5 +1,5 @@ import sys -from _typeshed import ReadOnlyBuffer, ReadableBuffer +from _typeshed import ReadableBuffer, ReadOnlyBuffer from contextlib import AbstractContextManager from typing import Iterable, Iterator, NoReturn, Sized, overload From fdd90cd365ef97a2939e753ae1d3b9c057ce626e Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Wed, 9 Feb 2022 00:35:16 +0200 Subject: [PATCH 05/29] Change `AnyStr` to include `ByteString` instead of `bytes` --- stdlib/mmap.pyi | 9 +++++++-- stdlib/typing.pyi | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/stdlib/mmap.pyi b/stdlib/mmap.pyi index 269f4191abd9..091be8a5293e 100644 --- a/stdlib/mmap.pyi +++ b/stdlib/mmap.pyi @@ -1,7 +1,12 @@ import sys from _typeshed import ReadOnlyBuffer, ReadableBuffer from contextlib import AbstractContextManager -from typing import Iterable, Iterator, NoReturn, Sized, overload +from typing import Iterable, Iterator, NoReturn, Sized, overload, ByteString + +if sys.version_info >= (3, 9): + from collections.abc import ByteString +else: + from typing import ByteString ACCESS_DEFAULT: int ACCESS_READ: int @@ -27,7 +32,7 @@ if sys.platform != "win32": PAGESIZE: int -class mmap(AbstractContextManager[mmap], ReadOnlyBuffer, Iterable[int], Sized): +class mmap(AbstractContextManager[mmap], ByteString, Iterable[int], Sized): if sys.platform == "win32": def __init__(self, fileno: int, length: int, tagname: str | None = ..., access: int = ..., offset: int = ...) -> None: ... else: diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 5a8557f909e5..0c82fa04cd19 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -140,7 +140,7 @@ if sys.version_info >= (3, 9): Annotated: _SpecialForm # Predefined type variables. -AnyStr = TypeVar("AnyStr", str, bytes) # noqa: Y001 +AnyStr = TypeVar("AnyStr", str, ByteString) # noqa: Y001 # Technically in 3.7 this inherited from GenericMeta. But let's not reflect that, since # type checkers tend to assume that Protocols all have the ABCMeta metaclass. From efe5ee013a6945bcbcf1a944d1a35c0e26a506c4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 8 Feb 2022 22:37:25 +0000 Subject: [PATCH 06/29] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/mmap.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/mmap.pyi b/stdlib/mmap.pyi index 51de707ab5ab..7f513ddbe7cb 100644 --- a/stdlib/mmap.pyi +++ b/stdlib/mmap.pyi @@ -1,7 +1,7 @@ import sys from _typeshed import ReadableBuffer, ReadOnlyBuffer from contextlib import AbstractContextManager -from typing import Iterable, Iterator, NoReturn, Sized, overload, ByteString +from typing import ByteString, Iterable, Iterator, NoReturn, Sized, overload if sys.version_info >= (3, 9): from collections.abc import ByteString From 6978ce6fae457ac06216987c291fe8845c4ae3fe Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Mon, 14 Feb 2022 20:41:38 +0200 Subject: [PATCH 07/29] Revert unwanted changes --- stdlib/mmap.pyi | 9 ++------- stdlib/typing.pyi | 2 +- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/stdlib/mmap.pyi b/stdlib/mmap.pyi index 7f513ddbe7cb..9d85ff77917e 100644 --- a/stdlib/mmap.pyi +++ b/stdlib/mmap.pyi @@ -1,12 +1,7 @@ import sys -from _typeshed import ReadableBuffer, ReadOnlyBuffer +from _typeshed import ReadableBuffer from contextlib import AbstractContextManager -from typing import ByteString, Iterable, Iterator, NoReturn, Sized, overload - -if sys.version_info >= (3, 9): - from collections.abc import ByteString -else: - from typing import ByteString +from typing import Iterable, Iterator, NoReturn, Sized, overload ACCESS_DEFAULT: int ACCESS_READ: int diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 0c82fa04cd19..5a8557f909e5 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -140,7 +140,7 @@ if sys.version_info >= (3, 9): Annotated: _SpecialForm # Predefined type variables. -AnyStr = TypeVar("AnyStr", str, ByteString) # noqa: Y001 +AnyStr = TypeVar("AnyStr", str, bytes) # noqa: Y001 # Technically in 3.7 this inherited from GenericMeta. But let's not reflect that, since # type checkers tend to assume that Protocols all have the ABCMeta metaclass. From 24e2a34e63fdfa45289597e02c8031585e326074 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Mon, 14 Feb 2022 20:42:07 +0200 Subject: [PATCH 08/29] Finish reverting --- stdlib/mmap.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/mmap.pyi b/stdlib/mmap.pyi index 9d85ff77917e..1f44651dda00 100644 --- a/stdlib/mmap.pyi +++ b/stdlib/mmap.pyi @@ -27,7 +27,7 @@ if sys.platform != "win32": PAGESIZE: int -class mmap(AbstractContextManager[mmap], ByteString, Iterable[int], Sized): +class mmap(AbstractContextManager[mmap], Iterable[int], Sized): if sys.platform == "win32": def __init__(self, fileno: int, length: int, tagname: str | None = ..., access: int = ..., offset: int = ...) -> None: ... else: From b9b1176dbc5b46d801081fe205252c84cf491e24 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Mon, 14 Feb 2022 21:27:49 +0200 Subject: [PATCH 09/29] Fix PoC --- stdlib/mmap.pyi | 4 ++-- stdlib/typing.pyi | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/mmap.pyi b/stdlib/mmap.pyi index 1f44651dda00..2113544dc2f0 100644 --- a/stdlib/mmap.pyi +++ b/stdlib/mmap.pyi @@ -1,7 +1,7 @@ import sys from _typeshed import ReadableBuffer from contextlib import AbstractContextManager -from typing import Iterable, Iterator, NoReturn, Sized, overload +from typing import Iterator, NoReturn, Sized, overload, ByteString ACCESS_DEFAULT: int ACCESS_READ: int @@ -27,7 +27,7 @@ if sys.platform != "win32": PAGESIZE: int -class mmap(AbstractContextManager[mmap], Iterable[int], Sized): +class mmap(AbstractContextManager[mmap], ByteString, Sized): if sys.platform == "win32": def __init__(self, fileno: int, length: int, tagname: str | None = ..., access: int = ..., offset: int = ...) -> None: ... else: diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 5a8557f909e5..b6219f035dcf 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -635,9 +635,9 @@ class Pattern(Generic[AnyStr]): flags: int groupindex: Mapping[str, int] groups: int - pattern: AnyStr + pattern: ByteString 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 match(self, string: ByteString, pos: int = ..., endpos: int = ...) -> Match[AnyStr] | 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]: ... From 2ded7aad8b8476c348bf44abf821cd291263c346 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Feb 2022 19:32:54 +0000 Subject: [PATCH 10/29] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/mmap.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/mmap.pyi b/stdlib/mmap.pyi index 2113544dc2f0..bc5b870e476f 100644 --- a/stdlib/mmap.pyi +++ b/stdlib/mmap.pyi @@ -1,7 +1,7 @@ import sys from _typeshed import ReadableBuffer from contextlib import AbstractContextManager -from typing import Iterator, NoReturn, Sized, overload, ByteString +from typing import ByteString, Iterator, NoReturn, Sized, overload ACCESS_DEFAULT: int ACCESS_READ: int From eb0e9535a78e214e8b98bc71b09abe1e0a04a7c4 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Mon, 14 Feb 2022 21:46:05 +0200 Subject: [PATCH 11/29] Attempt to create custom type for regex strings --- stdlib/typing.pyi | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index b6219f035dcf..27e7982d4fe5 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 Self, SupportsKeysAndGetItem, 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 @@ -630,26 +630,28 @@ class Match(Generic[AnyStr]): if sys.version_info >= (3, 9): def __class_getitem__(cls, item: Any) -> GenericAlias: ... +RegexString = TypeVar("RegexString", Union[AnyStr, ReadableBuffer]) + @_final class Pattern(Generic[AnyStr]): flags: int groupindex: Mapping[str, int] groups: int - pattern: ByteString - def search(self, string: AnyStr, pos: int = ..., endpos: int = ...) -> Match[AnyStr] | None: ... - def match(self, string: ByteString, pos: int = ..., endpos: int = ...) -> Match[AnyStr] | 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]: ... - def finditer(self, string: AnyStr, pos: int = ..., endpos: int = ...) -> Iterator[Match[AnyStr]]: ... + pattern: RegexString + def search(self, string: RegexString, pos: int = ..., endpos: int = ...) -> Match[RegexString] | None: ... + def match(self, string: ByteString, pos: int = ..., endpos: int = ...) -> Match[RegexString] | None: ... + def fullmatch(self, string: RegexString, pos: int = ..., endpos: int = ...) -> Match[RegexString] | None: ... + def split(self, string: RegexString, maxsplit: int = ...) -> list[RegexString | Any]: ... + def findall(self, string: RegexString, pos: int = ..., endpos: int = ...) -> list[Any]: ... + def finditer(self, string: RegexString, pos: int = ..., endpos: int = ...) -> Iterator[Match[RegexString]]: ... @overload - def sub(self, repl: AnyStr, string: AnyStr, count: int = ...) -> AnyStr: ... + def sub(self, repl: AnyStr, string: RegexString, count: int = ...) -> RegexString: ... @overload - def sub(self, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ...) -> AnyStr: ... + def sub(self, repl: Callable[[Match[RegexString]], RegexString], string: RegexString, count: int = ...) -> RegexString: ... @overload - def subn(self, repl: AnyStr, string: AnyStr, count: int = ...) -> tuple[AnyStr, int]: ... + def subn(self, repl: AnyStr, string: RegexString, count: int = ...) -> tuple[RegexString, int]: ... @overload - def subn(self, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ...) -> tuple[AnyStr, int]: ... + def subn(self, repl: Callable[[Match[RegexString]], RegexString], string: RegexString, count: int = ...) -> tuple[RegexString, int]: ... if sys.version_info >= (3, 9): def __class_getitem__(cls, item: Any) -> GenericAlias: ... From 38f599656254f92e71dfdf8fe673861e5c7c84dd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Feb 2022 19:47:14 +0000 Subject: [PATCH 12/29] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/typing.pyi | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 27e7982d4fe5..1bce1c48a25e 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, ReadableBuffer +from _typeshed import ReadableBuffer, Self, SupportsKeysAndGetItem 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 @@ -651,7 +651,9 @@ class Pattern(Generic[AnyStr]): @overload def subn(self, repl: AnyStr, string: RegexString, count: int = ...) -> tuple[RegexString, int]: ... @overload - def subn(self, repl: Callable[[Match[RegexString]], RegexString], string: RegexString, count: int = ...) -> tuple[RegexString, int]: ... + def subn( + self, repl: Callable[[Match[RegexString]], RegexString], string: RegexString, count: int = ... + ) -> tuple[RegexString, int]: ... if sys.version_info >= (3, 9): def __class_getitem__(cls, item: Any) -> GenericAlias: ... From 1a56af6a6a10e5d58256ff09ae84eb19b56144e1 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Mon, 14 Feb 2022 21:48:56 +0200 Subject: [PATCH 13/29] Fixup --- stdlib/typing.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 1bce1c48a25e..f2d8791c8f36 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -633,7 +633,7 @@ class Match(Generic[AnyStr]): RegexString = TypeVar("RegexString", Union[AnyStr, ReadableBuffer]) @_final -class Pattern(Generic[AnyStr]): +class Pattern(Generic[RegexString]): flags: int groupindex: Mapping[str, int] groups: int From e26ef4ec562aa339fcbcf327894f35a1959f999e Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Mon, 14 Feb 2022 21:59:34 +0200 Subject: [PATCH 14/29] Fix regex string typevar --- stdlib/typing.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index f2d8791c8f36..2e75300d8c65 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -630,7 +630,7 @@ class Match(Generic[AnyStr]): if sys.version_info >= (3, 9): def __class_getitem__(cls, item: Any) -> GenericAlias: ... -RegexString = TypeVar("RegexString", Union[AnyStr, ReadableBuffer]) +RegexString = TypeVar("RegexString", AnyStr, ReadableBuffer) @_final class Pattern(Generic[RegexString]): From 884af9246e974692e4c4645959862ac266c38f4b Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Mon, 14 Feb 2022 22:14:13 +0200 Subject: [PATCH 15/29] Update `Match` to use `RegexString` --- stdlib/typing.pyi | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 2e75300d8c65..a05e7ae70aa1 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -586,52 +586,52 @@ class TextIO(IO[str]): class ByteString(Sequence[int], metaclass=ABCMeta): ... +RegexString = TypeVar("RegexString", AnyStr, ReadableBuffer) + @_final -class Match(Generic[AnyStr]): +class Match(Generic[RegexString]): pos: int endpos: int lastindex: int | None lastgroup: str | None - string: AnyStr + string: RegexString # The regular expression object whose match() or search() method produced # this match instance. - re: Pattern[AnyStr] - def expand(self, template: AnyStr) -> AnyStr: ... - # group() returns "AnyStr" or "AnyStr | None", depending on the pattern. + re: Pattern[RegexString] + def expand(self, template: RegexString) -> RegexString: ... + # group() returns "RegexString" or "RegexString | None", depending on the pattern. @overload - def group(self, __group: _Literal[0] = ...) -> AnyStr: ... + def group(self, __group: _Literal[0] = ...) -> RegexString: ... @overload - def group(self, __group: str | int) -> AnyStr | Any: ... + def group(self, __group: str | int) -> RegexString | Any: ... @overload - def group(self, __group1: str | int, __group2: str | int, *groups: str | int) -> tuple[AnyStr | Any, ...]: ... - # Each item of groups()'s return tuple is either "AnyStr" or - # "AnyStr | None", depending on the pattern. + def group(self, __group1: str | int, __group2: str | int, *groups: str | int) -> tuple[RegexString | Any, ...]: ... + # Each item of groups()'s return tuple is either "RegexString" or + # "RegexString | None", depending on the pattern. @overload - def groups(self) -> tuple[AnyStr | Any, ...]: ... + def groups(self) -> tuple[RegexString | Any, ...]: ... @overload - def groups(self, default: _T) -> tuple[AnyStr | _T, ...]: ... - # Each value in groupdict()'s return dict is either "AnyStr" or - # "AnyStr | None", depending on the pattern. + def groups(self, default: _T) -> tuple[RegexString | _T, ...]: ... + # Each value in groupdict()'s return dict is either "RegexString" or + # "RegexString | None", depending on the pattern. @overload - def groupdict(self) -> dict[str, AnyStr | Any]: ... + def groupdict(self) -> dict[str, RegexString | Any]: ... @overload - def groupdict(self, default: _T) -> dict[str, AnyStr | _T]: ... + def groupdict(self, default: _T) -> dict[str, RegexString | _T]: ... def start(self, __group: int | str = ...) -> int: ... def end(self, __group: int | str = ...) -> int: ... def span(self, __group: int | str = ...) -> tuple[int, int]: ... @property def regs(self) -> tuple[tuple[int, int], ...]: ... # undocumented - # __getitem__() returns "AnyStr" or "AnyStr | None", depending on the pattern. + # __getitem__() returns "RegexString" or "RegexString | None", depending on the pattern. @overload - def __getitem__(self, __key: _Literal[0]) -> AnyStr: ... + def __getitem__(self, __key: _Literal[0]) -> RegexString: ... @overload - def __getitem__(self, __key: int | str) -> AnyStr | Any: ... + def __getitem__(self, __key: int | str) -> RegexString | Any: ... if sys.version_info >= (3, 9): def __class_getitem__(cls, item: Any) -> GenericAlias: ... -RegexString = TypeVar("RegexString", AnyStr, ReadableBuffer) - @_final class Pattern(Generic[RegexString]): flags: int @@ -645,11 +645,11 @@ class Pattern(Generic[RegexString]): def findall(self, string: RegexString, pos: int = ..., endpos: int = ...) -> list[Any]: ... def finditer(self, string: RegexString, pos: int = ..., endpos: int = ...) -> Iterator[Match[RegexString]]: ... @overload - def sub(self, repl: AnyStr, string: RegexString, count: int = ...) -> RegexString: ... + def sub(self, repl: RegexString, string: RegexString, count: int = ...) -> RegexString: ... @overload def sub(self, repl: Callable[[Match[RegexString]], RegexString], string: RegexString, count: int = ...) -> RegexString: ... @overload - def subn(self, repl: AnyStr, string: RegexString, count: int = ...) -> tuple[RegexString, int]: ... + def subn(self, repl: RegexString, string: RegexString, count: int = ...) -> tuple[RegexString, int]: ... @overload def subn( self, repl: Callable[[Match[RegexString]], RegexString], string: RegexString, count: int = ... From c9af022d2499b017f8936d33d98c2676d910449d Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Mon, 14 Feb 2022 22:22:22 +0200 Subject: [PATCH 16/29] Fix extended typevar --- stdlib/typing.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index a05e7ae70aa1..6c48423d4669 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -586,7 +586,7 @@ class TextIO(IO[str]): class ByteString(Sequence[int], metaclass=ABCMeta): ... -RegexString = TypeVar("RegexString", AnyStr, ReadableBuffer) +RegexString = TypeVar("RegexString", str, ReadableBuffer) @_final class Match(Generic[RegexString]): From 6e320ae9f101fe5d45e2b167ace979b06e8b6e13 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Mon, 14 Feb 2022 22:29:11 +0200 Subject: [PATCH 17/29] Attempt to fix --- stdlib/re.pyi | 60 +++++++++++++++++++++++------------------------ stdlib/typing.pyi | 4 ++-- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/stdlib/re.pyi b/stdlib/re.pyi index 01a60d170c50..610cea6e1f22 100644 --- a/stdlib/re.pyi +++ b/stdlib/re.pyi @@ -1,7 +1,7 @@ import enum import sys from sre_constants import error as error -from typing import Any, AnyStr, Callable, Iterator, Union, overload +from typing import Any, AnyStr, Callable, Iterator, Union, overload, RegexString # ----- re variables and constants ----- if sys.version_info >= (3, 7): @@ -52,66 +52,66 @@ if sys.version_info < (3, 7): _pattern_type: type @overload -def compile(pattern: AnyStr, flags: _FlagsType = ...) -> Pattern[AnyStr]: ... +def compile(pattern: RegexString, flags: _FlagsType = ...) -> Pattern[RegexString]: ... @overload -def compile(pattern: Pattern[AnyStr], flags: _FlagsType = ...) -> Pattern[AnyStr]: ... +def compile(pattern: Pattern[RegexString], flags: _FlagsType = ...) -> Pattern[RegexString]: ... @overload -def search(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... +def search(pattern: RegexString, string: RegexString, flags: _FlagsType = ...) -> Match[RegexString] | None: ... @overload -def search(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... +def search(pattern: Pattern[RegexString], string: RegexString, flags: _FlagsType = ...) -> Match[RegexString] | None: ... @overload -def match(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... +def match(pattern: RegexString, string: RegexString, flags: _FlagsType = ...) -> Match[RegexString] | None: ... @overload -def match(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... +def match(pattern: Pattern[RegexString], string: RegexString, flags: _FlagsType = ...) -> Match[RegexString] | None: ... # New in Python 3.4 @overload -def fullmatch(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... +def fullmatch(pattern: RegexString, string: RegexString, flags: _FlagsType = ...) -> Match[RegexString] | None: ... @overload -def fullmatch(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... +def fullmatch(pattern: Pattern[RegexString], string: RegexString, flags: _FlagsType = ...) -> Match[RegexString] | None: ... @overload -def split(pattern: AnyStr, string: AnyStr, maxsplit: int = ..., flags: _FlagsType = ...) -> list[AnyStr | Any]: ... +def split(pattern: RegexString, string: RegexString, maxsplit: int = ..., flags: _FlagsType = ...) -> list[RegexString | Any]: ... @overload -def split(pattern: Pattern[AnyStr], string: AnyStr, maxsplit: int = ..., flags: _FlagsType = ...) -> list[AnyStr | Any]: ... +def split(pattern: Pattern[RegexString], string: RegexString, maxsplit: int = ..., flags: _FlagsType = ...) -> list[RegexString | Any]: ... @overload -def findall(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> list[Any]: ... +def findall(pattern: RegexString, string: RegexString, flags: _FlagsType = ...) -> list[Any]: ... @overload -def findall(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> list[Any]: ... +def findall(pattern: Pattern[RegexString], string: RegexString, flags: _FlagsType = ...) -> list[Any]: ... # Return an iterator yielding match objects over all non-overlapping matches # for the RE pattern in string. The string is scanned left-to-right, and # matches are returned in the order found. Empty matches are included in the # result unless they touch the beginning of another match. @overload -def finditer(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Iterator[Match[AnyStr]]: ... +def finditer(pattern: RegexString, string: RegexString, flags: _FlagsType = ...) -> Iterator[Match[RegexString]]: ... @overload -def finditer(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Iterator[Match[AnyStr]]: ... +def finditer(pattern: Pattern[RegexString], string: RegexString, flags: _FlagsType = ...) -> Iterator[Match[RegexString]]: ... @overload -def sub(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> AnyStr: ... +def sub(pattern: RegexString, repl: RegexString, string: RegexString, count: int = ..., flags: _FlagsType = ...) -> RegexString: ... @overload def sub( - pattern: AnyStr, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ... -) -> AnyStr: ... + pattern: RegexString, repl: Callable[[Match[RegexString]], RegexString], string: RegexString, count: int = ..., flags: _FlagsType = ... +) -> RegexString: ... @overload -def sub(pattern: Pattern[AnyStr], repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> AnyStr: ... +def sub(pattern: Pattern[RegexString], repl: RegexString, string: RegexString, count: int = ..., flags: _FlagsType = ...) -> RegexString: ... @overload def sub( - pattern: Pattern[AnyStr], repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ... -) -> AnyStr: ... + pattern: Pattern[RegexString], repl: Callable[[Match[RegexString]], RegexString], string: RegexString, count: int = ..., flags: _FlagsType = ... +) -> RegexString: ... @overload -def subn(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> tuple[AnyStr, int]: ... +def subn(pattern: RegexString, repl: RegexString, string: RegexString, count: int = ..., flags: _FlagsType = ...) -> tuple[RegexString, int]: ... @overload def subn( - pattern: AnyStr, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ... -) -> tuple[AnyStr, int]: ... + pattern: RegexString, repl: Callable[[Match[RegexString]], RegexString], string: RegexString, count: int = ..., flags: _FlagsType = ... +) -> tuple[RegexString, int]: ... @overload def subn( - pattern: Pattern[AnyStr], repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ... -) -> tuple[AnyStr, int]: ... + pattern: Pattern[RegexString], repl: RegexString, string: RegexString, count: int = ..., flags: _FlagsType = ... +) -> tuple[RegexString, int]: ... @overload def subn( - pattern: Pattern[AnyStr], repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ... -) -> tuple[AnyStr, int]: ... -def escape(pattern: AnyStr) -> AnyStr: ... + pattern: Pattern[RegexString], repl: Callable[[Match[RegexString]], RegexString], string: RegexString, count: int = ..., flags: _FlagsType = ... +) -> tuple[RegexString, int]: ... +def escape(pattern: RegexString) -> RegexString: ... def purge() -> None: ... -def template(pattern: AnyStr | Pattern[AnyStr], flags: _FlagsType = ...) -> Pattern[AnyStr]: ... +def template(pattern: RegexString | Pattern[RegexString], flags: _FlagsType = ...) -> Pattern[RegexString]: ... diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 6c48423d4669..01b56afb3c87 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 ReadableBuffer, Self, SupportsKeysAndGetItem +from _typeshed import ReadableBuffer, Self, SupportsKeysAndGetItem, ReadOnlyBuffer, WriteableBuffer 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 @@ -586,7 +586,7 @@ class TextIO(IO[str]): class ByteString(Sequence[int], metaclass=ABCMeta): ... -RegexString = TypeVar("RegexString", str, ReadableBuffer) +RegexString = TypeVar("RegexString", str, ReadOnlyBuffer, WriteableBuffer) @_final class Match(Generic[RegexString]): From d3094f9c4ca577bf0207c269b9376f0c4ae65487 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Feb 2022 20:30:17 +0000 Subject: [PATCH 18/29] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/re.pyi | 42 +++++++++++++++++++++++++++++++++--------- stdlib/typing.pyi | 2 +- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/stdlib/re.pyi b/stdlib/re.pyi index 610cea6e1f22..c76d37bb6538 100644 --- a/stdlib/re.pyi +++ b/stdlib/re.pyi @@ -1,7 +1,7 @@ import enum import sys from sre_constants import error as error -from typing import Any, AnyStr, Callable, Iterator, Union, overload, RegexString +from typing import Any, AnyStr, Callable, Iterator, RegexString, Union, overload # ----- re variables and constants ----- if sys.version_info >= (3, 7): @@ -72,7 +72,9 @@ def fullmatch(pattern: Pattern[RegexString], string: RegexString, flags: _FlagsT @overload def split(pattern: RegexString, string: RegexString, maxsplit: int = ..., flags: _FlagsType = ...) -> list[RegexString | Any]: ... @overload -def split(pattern: Pattern[RegexString], string: RegexString, maxsplit: int = ..., flags: _FlagsType = ...) -> list[RegexString | Any]: ... +def split( + pattern: Pattern[RegexString], string: RegexString, maxsplit: int = ..., flags: _FlagsType = ... +) -> list[RegexString | Any]: ... @overload def findall(pattern: RegexString, string: RegexString, flags: _FlagsType = ...) -> list[Any]: ... @overload @@ -87,22 +89,40 @@ def finditer(pattern: RegexString, string: RegexString, flags: _FlagsType = ...) @overload def finditer(pattern: Pattern[RegexString], string: RegexString, flags: _FlagsType = ...) -> Iterator[Match[RegexString]]: ... @overload -def sub(pattern: RegexString, repl: RegexString, string: RegexString, count: int = ..., flags: _FlagsType = ...) -> RegexString: ... +def sub( + pattern: RegexString, repl: RegexString, string: RegexString, count: int = ..., flags: _FlagsType = ... +) -> RegexString: ... @overload def sub( - pattern: RegexString, repl: Callable[[Match[RegexString]], RegexString], string: RegexString, count: int = ..., flags: _FlagsType = ... + pattern: RegexString, + repl: Callable[[Match[RegexString]], RegexString], + string: RegexString, + count: int = ..., + flags: _FlagsType = ..., ) -> RegexString: ... @overload -def sub(pattern: Pattern[RegexString], repl: RegexString, string: RegexString, count: int = ..., flags: _FlagsType = ...) -> RegexString: ... +def sub( + pattern: Pattern[RegexString], repl: RegexString, string: RegexString, count: int = ..., flags: _FlagsType = ... +) -> RegexString: ... @overload def sub( - pattern: Pattern[RegexString], repl: Callable[[Match[RegexString]], RegexString], string: RegexString, count: int = ..., flags: _FlagsType = ... + pattern: Pattern[RegexString], + repl: Callable[[Match[RegexString]], RegexString], + string: RegexString, + count: int = ..., + flags: _FlagsType = ..., ) -> RegexString: ... @overload -def subn(pattern: RegexString, repl: RegexString, string: RegexString, count: int = ..., flags: _FlagsType = ...) -> tuple[RegexString, int]: ... +def subn( + pattern: RegexString, repl: RegexString, string: RegexString, count: int = ..., flags: _FlagsType = ... +) -> tuple[RegexString, int]: ... @overload def subn( - pattern: RegexString, repl: Callable[[Match[RegexString]], RegexString], string: RegexString, count: int = ..., flags: _FlagsType = ... + pattern: RegexString, + repl: Callable[[Match[RegexString]], RegexString], + string: RegexString, + count: int = ..., + flags: _FlagsType = ..., ) -> tuple[RegexString, int]: ... @overload def subn( @@ -110,7 +130,11 @@ def subn( ) -> tuple[RegexString, int]: ... @overload def subn( - pattern: Pattern[RegexString], repl: Callable[[Match[RegexString]], RegexString], string: RegexString, count: int = ..., flags: _FlagsType = ... + pattern: Pattern[RegexString], + repl: Callable[[Match[RegexString]], RegexString], + string: RegexString, + count: int = ..., + flags: _FlagsType = ..., ) -> tuple[RegexString, int]: ... def escape(pattern: RegexString) -> RegexString: ... def purge() -> None: ... diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 01b56afb3c87..904f5c852653 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 ReadableBuffer, Self, SupportsKeysAndGetItem, ReadOnlyBuffer, WriteableBuffer +from _typeshed import ReadableBuffer, ReadOnlyBuffer, Self, SupportsKeysAndGetItem, WriteableBuffer 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 From d775b51a70d371910df2c81eb1535e08ed08bc96 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Mon, 14 Feb 2022 22:37:01 +0200 Subject: [PATCH 19/29] Remove unused imports --- stdlib/re.pyi | 2 +- stdlib/typing.pyi | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/re.pyi b/stdlib/re.pyi index 610cea6e1f22..fd93f33f07f0 100644 --- a/stdlib/re.pyi +++ b/stdlib/re.pyi @@ -1,7 +1,7 @@ import enum import sys from sre_constants import error as error -from typing import Any, AnyStr, Callable, Iterator, Union, overload, RegexString +from typing import Any, Callable, Iterator, Union, overload, RegexString # ----- re variables and constants ----- if sys.version_info >= (3, 7): diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 01b56afb3c87..f6b5c49d7987 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 ReadableBuffer, Self, SupportsKeysAndGetItem, ReadOnlyBuffer, WriteableBuffer +from _typeshed import Self, SupportsKeysAndGetItem, ReadOnlyBuffer, WriteableBuffer 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 From c63f691b8b843702d54bb0192059e5a746722d84 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Feb 2022 20:39:08 +0000 Subject: [PATCH 20/29] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/re.pyi | 2 +- stdlib/typing.pyi | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/re.pyi b/stdlib/re.pyi index 0b3976936443..d8fa33f163af 100644 --- a/stdlib/re.pyi +++ b/stdlib/re.pyi @@ -1,7 +1,7 @@ import enum import sys from sre_constants import error as error -from typing import Any, Callable, Iterator, Union, overload, RegexString +from typing import Any, Callable, Iterator, RegexString, Union, overload # ----- re variables and constants ----- if sys.version_info >= (3, 7): diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index f6b5c49d7987..d1cbe9c720e5 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, ReadOnlyBuffer, WriteableBuffer +from _typeshed import ReadOnlyBuffer, Self, SupportsKeysAndGetItem, WriteableBuffer 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 From ee5573abb58cb458e7150c8c5976580080664b16 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Tue, 15 Feb 2022 00:11:41 +0200 Subject: [PATCH 21/29] Fix linting --- stdlib/_typeshed/__init__.pyi | 2 + stdlib/re.pyi | 85 ++++++++++++++++++----------------- stdlib/typing.pyi | 68 ++++++++++++++-------------- 3 files changed, 78 insertions(+), 77 deletions(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index 103af47c7524..b823398616e2 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 +StrOrBuffer = TypeVar("StrOrBuffer", str, ReadOnlyBuffer, WriteableBuffer) + # stable if sys.version_info >= (3, 10): from types import NoneType as NoneType diff --git a/stdlib/re.pyi b/stdlib/re.pyi index 0b3976936443..ce134e7215cc 100644 --- a/stdlib/re.pyi +++ b/stdlib/re.pyi @@ -1,7 +1,8 @@ import enum import sys from sre_constants import error as error -from typing import Any, Callable, Iterator, Union, overload, RegexString +from typing import Any, Callable, Iterator, Union, overload +from _typeshed import StrOrBuffer # ----- re variables and constants ----- if sys.version_info >= (3, 7): @@ -52,90 +53,90 @@ if sys.version_info < (3, 7): _pattern_type: type @overload -def compile(pattern: RegexString, flags: _FlagsType = ...) -> Pattern[RegexString]: ... +def compile(pattern: StrOrBuffer, flags: _FlagsType = ...) -> Pattern[StrOrBuffer]: ... @overload -def compile(pattern: Pattern[RegexString], flags: _FlagsType = ...) -> Pattern[RegexString]: ... +def compile(pattern: Pattern[StrOrBuffer], flags: _FlagsType = ...) -> Pattern[StrOrBuffer]: ... @overload -def search(pattern: RegexString, string: RegexString, flags: _FlagsType = ...) -> Match[RegexString] | None: ... +def search(pattern: StrOrBuffer, string: StrOrBuffer, flags: _FlagsType = ...) -> Match[StrOrBuffer] | None: ... @overload -def search(pattern: Pattern[RegexString], string: RegexString, flags: _FlagsType = ...) -> Match[RegexString] | None: ... +def search(pattern: Pattern[StrOrBuffer], string: StrOrBuffer, flags: _FlagsType = ...) -> Match[StrOrBuffer] | None: ... @overload -def match(pattern: RegexString, string: RegexString, flags: _FlagsType = ...) -> Match[RegexString] | None: ... +def match(pattern: StrOrBuffer, string: StrOrBuffer, flags: _FlagsType = ...) -> Match[StrOrBuffer] | None: ... @overload -def match(pattern: Pattern[RegexString], string: RegexString, flags: _FlagsType = ...) -> Match[RegexString] | None: ... +def match(pattern: Pattern[StrOrBuffer], string: StrOrBuffer, flags: _FlagsType = ...) -> Match[StrOrBuffer] | None: ... # New in Python 3.4 @overload -def fullmatch(pattern: RegexString, string: RegexString, flags: _FlagsType = ...) -> Match[RegexString] | None: ... +def fullmatch(pattern: StrOrBuffer, string: StrOrBuffer, flags: _FlagsType = ...) -> Match[StrOrBuffer] | None: ... @overload -def fullmatch(pattern: Pattern[RegexString], string: RegexString, flags: _FlagsType = ...) -> Match[RegexString] | None: ... +def fullmatch(pattern: Pattern[StrOrBuffer], string: StrOrBuffer, flags: _FlagsType = ...) -> Match[StrOrBuffer] | None: ... @overload -def split(pattern: RegexString, string: RegexString, maxsplit: int = ..., flags: _FlagsType = ...) -> list[RegexString | Any]: ... +def split(pattern: StrOrBuffer, string: StrOrBuffer, maxsplit: int = ..., flags: _FlagsType = ...) -> list[StrOrBuffer | Any]: ... @overload def split( - pattern: Pattern[RegexString], string: RegexString, maxsplit: int = ..., flags: _FlagsType = ... -) -> list[RegexString | Any]: ... + pattern: Pattern[StrOrBuffer], string: StrOrBuffer, maxsplit: int = ..., flags: _FlagsType = ... +) -> list[StrOrBuffer | Any]: ... @overload -def findall(pattern: RegexString, string: RegexString, flags: _FlagsType = ...) -> list[Any]: ... +def findall(pattern: StrOrBuffer, string: StrOrBuffer, flags: _FlagsType = ...) -> list[Any]: ... @overload -def findall(pattern: Pattern[RegexString], string: RegexString, flags: _FlagsType = ...) -> list[Any]: ... +def findall(pattern: Pattern[StrOrBuffer], string: StrOrBuffer, flags: _FlagsType = ...) -> list[Any]: ... # Return an iterator yielding match objects over all non-overlapping matches # for the RE pattern in string. The string is scanned left-to-right, and # matches are returned in the order found. Empty matches are included in the # result unless they touch the beginning of another match. @overload -def finditer(pattern: RegexString, string: RegexString, flags: _FlagsType = ...) -> Iterator[Match[RegexString]]: ... +def finditer(pattern: StrOrBuffer, string: StrOrBuffer, flags: _FlagsType = ...) -> Iterator[Match[StrOrBuffer]]: ... @overload -def finditer(pattern: Pattern[RegexString], string: RegexString, flags: _FlagsType = ...) -> Iterator[Match[RegexString]]: ... +def finditer(pattern: Pattern[StrOrBuffer], string: StrOrBuffer, flags: _FlagsType = ...) -> Iterator[Match[StrOrBuffer]]: ... @overload def sub( - pattern: RegexString, repl: RegexString, string: RegexString, count: int = ..., flags: _FlagsType = ... -) -> RegexString: ... + pattern: StrOrBuffer, repl: StrOrBuffer, string: StrOrBuffer, count: int = ..., flags: _FlagsType = ... +) -> StrOrBuffer: ... @overload def sub( - pattern: RegexString, - repl: Callable[[Match[RegexString]], RegexString], - string: RegexString, + pattern: StrOrBuffer, + repl: Callable[[Match[StrOrBuffer]], StrOrBuffer], + string: StrOrBuffer, count: int = ..., flags: _FlagsType = ..., -) -> RegexString: ... +) -> StrOrBuffer: ... @overload def sub( - pattern: Pattern[RegexString], repl: RegexString, string: RegexString, count: int = ..., flags: _FlagsType = ... -) -> RegexString: ... + pattern: Pattern[StrOrBuffer], repl: StrOrBuffer, string: StrOrBuffer, count: int = ..., flags: _FlagsType = ... +) -> StrOrBuffer: ... @overload def sub( - pattern: Pattern[RegexString], - repl: Callable[[Match[RegexString]], RegexString], - string: RegexString, + pattern: Pattern[StrOrBuffer], + repl: Callable[[Match[StrOrBuffer]], StrOrBuffer], + string: StrOrBuffer, count: int = ..., flags: _FlagsType = ..., -) -> RegexString: ... +) -> StrOrBuffer: ... @overload def subn( - pattern: RegexString, repl: RegexString, string: RegexString, count: int = ..., flags: _FlagsType = ... -) -> tuple[RegexString, int]: ... + pattern: StrOrBuffer, repl: StrOrBuffer, string: StrOrBuffer, count: int = ..., flags: _FlagsType = ... +) -> tuple[StrOrBuffer, int]: ... @overload def subn( - pattern: RegexString, - repl: Callable[[Match[RegexString]], RegexString], - string: RegexString, + pattern: StrOrBuffer, + repl: Callable[[Match[StrOrBuffer]], StrOrBuffer], + string: StrOrBuffer, count: int = ..., flags: _FlagsType = ..., -) -> tuple[RegexString, int]: ... +) -> tuple[StrOrBuffer, int]: ... @overload def subn( - pattern: Pattern[RegexString], repl: RegexString, string: RegexString, count: int = ..., flags: _FlagsType = ... -) -> tuple[RegexString, int]: ... + pattern: Pattern[StrOrBuffer], repl: StrOrBuffer, string: StrOrBuffer, count: int = ..., flags: _FlagsType = ... +) -> tuple[StrOrBuffer, int]: ... @overload def subn( - pattern: Pattern[RegexString], - repl: Callable[[Match[RegexString]], RegexString], - string: RegexString, + pattern: Pattern[StrOrBuffer], + repl: Callable[[Match[StrOrBuffer]], StrOrBuffer], + string: StrOrBuffer, count: int = ..., flags: _FlagsType = ..., -) -> tuple[RegexString, int]: ... -def escape(pattern: RegexString) -> RegexString: ... +) -> tuple[StrOrBuffer, int]: ... +def escape(pattern: StrOrBuffer) -> StrOrBuffer: ... def purge() -> None: ... -def template(pattern: RegexString | Pattern[RegexString], flags: _FlagsType = ...) -> Pattern[RegexString]: ... +def template(pattern: StrOrBuffer | Pattern[StrOrBuffer], flags: _FlagsType = ...) -> Pattern[StrOrBuffer]: ... diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index f6b5c49d7987..2b32a7238db5 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, ReadOnlyBuffer, WriteableBuffer +from _typeshed import Self, SupportsKeysAndGetItem, StrOrBuffer 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 @@ -586,74 +586,72 @@ class TextIO(IO[str]): class ByteString(Sequence[int], metaclass=ABCMeta): ... -RegexString = TypeVar("RegexString", str, ReadOnlyBuffer, WriteableBuffer) - @_final -class Match(Generic[RegexString]): +class Match(Generic[StrOrBuffer]): pos: int endpos: int lastindex: int | None lastgroup: str | None - string: RegexString + string: StrOrBuffer # The regular expression object whose match() or search() method produced # this match instance. - re: Pattern[RegexString] - def expand(self, template: RegexString) -> RegexString: ... - # group() returns "RegexString" or "RegexString | None", depending on the pattern. + re: Pattern[StrOrBuffer] + def expand(self, template: StrOrBuffer) -> StrOrBuffer: ... + # group() returns "StrOrBuffer" or "StrOrBuffer | None", depending on the pattern. @overload - def group(self, __group: _Literal[0] = ...) -> RegexString: ... + def group(self, __group: _Literal[0] = ...) -> StrOrBuffer: ... @overload - def group(self, __group: str | int) -> RegexString | Any: ... + def group(self, __group: str | int) -> StrOrBuffer | Any: ... @overload - def group(self, __group1: str | int, __group2: str | int, *groups: str | int) -> tuple[RegexString | Any, ...]: ... - # Each item of groups()'s return tuple is either "RegexString" or - # "RegexString | None", depending on the pattern. + def group(self, __group1: str | int, __group2: str | int, *groups: str | int) -> tuple[StrOrBuffer | Any, ...]: ... + # Each item of groups()'s return tuple is either "StrOrBuffer" or + # "StrOrBuffer | None", depending on the pattern. @overload - def groups(self) -> tuple[RegexString | Any, ...]: ... + def groups(self) -> tuple[StrOrBuffer | Any, ...]: ... @overload - def groups(self, default: _T) -> tuple[RegexString | _T, ...]: ... - # Each value in groupdict()'s return dict is either "RegexString" or - # "RegexString | None", depending on the pattern. + def groups(self, default: _T) -> tuple[StrOrBuffer | _T, ...]: ... + # Each value in groupdict()'s return dict is either "StrOrBuffer" or + # "StrOrBuffer | None", depending on the pattern. @overload - def groupdict(self) -> dict[str, RegexString | Any]: ... + def groupdict(self) -> dict[str, StrOrBuffer | Any]: ... @overload - def groupdict(self, default: _T) -> dict[str, RegexString | _T]: ... + def groupdict(self, default: _T) -> dict[str, StrOrBuffer | _T]: ... def start(self, __group: int | str = ...) -> int: ... def end(self, __group: int | str = ...) -> int: ... def span(self, __group: int | str = ...) -> tuple[int, int]: ... @property def regs(self) -> tuple[tuple[int, int], ...]: ... # undocumented - # __getitem__() returns "RegexString" or "RegexString | None", depending on the pattern. + # __getitem__() returns "StrOrBuffer" or "StrOrBuffer | None", depending on the pattern. @overload - def __getitem__(self, __key: _Literal[0]) -> RegexString: ... + def __getitem__(self, __key: _Literal[0]) -> StrOrBuffer: ... @overload - def __getitem__(self, __key: int | str) -> RegexString | Any: ... + def __getitem__(self, __key: int | str) -> StrOrBuffer | Any: ... if sys.version_info >= (3, 9): def __class_getitem__(cls, item: Any) -> GenericAlias: ... @_final -class Pattern(Generic[RegexString]): +class Pattern(Generic[StrOrBuffer]): flags: int groupindex: Mapping[str, int] groups: int - pattern: RegexString - def search(self, string: RegexString, pos: int = ..., endpos: int = ...) -> Match[RegexString] | None: ... - def match(self, string: ByteString, pos: int = ..., endpos: int = ...) -> Match[RegexString] | None: ... - def fullmatch(self, string: RegexString, pos: int = ..., endpos: int = ...) -> Match[RegexString] | None: ... - def split(self, string: RegexString, maxsplit: int = ...) -> list[RegexString | Any]: ... - def findall(self, string: RegexString, pos: int = ..., endpos: int = ...) -> list[Any]: ... - def finditer(self, string: RegexString, pos: int = ..., endpos: int = ...) -> Iterator[Match[RegexString]]: ... + pattern: StrOrBuffer + def search(self, string: StrOrBuffer, pos: int = ..., endpos: int = ...) -> Match[StrOrBuffer] | None: ... + def match(self, string: ByteString, pos: int = ..., endpos: int = ...) -> Match[StrOrBuffer] | None: ... + def fullmatch(self, string: StrOrBuffer, pos: int = ..., endpos: int = ...) -> Match[StrOrBuffer] | None: ... + def split(self, string: StrOrBuffer, maxsplit: int = ...) -> list[StrOrBuffer | Any]: ... + def findall(self, string: StrOrBuffer, pos: int = ..., endpos: int = ...) -> list[Any]: ... + def finditer(self, string: StrOrBuffer, pos: int = ..., endpos: int = ...) -> Iterator[Match[StrOrBuffer]]: ... @overload - def sub(self, repl: RegexString, string: RegexString, count: int = ...) -> RegexString: ... + def sub(self, repl: StrOrBuffer, string: StrOrBuffer, count: int = ...) -> StrOrBuffer: ... @overload - def sub(self, repl: Callable[[Match[RegexString]], RegexString], string: RegexString, count: int = ...) -> RegexString: ... + def sub(self, repl: Callable[[Match[StrOrBuffer]], StrOrBuffer], string: StrOrBuffer, count: int = ...) -> StrOrBuffer: ... @overload - def subn(self, repl: RegexString, string: RegexString, count: int = ...) -> tuple[RegexString, int]: ... + def subn(self, repl: StrOrBuffer, string: StrOrBuffer, count: int = ...) -> tuple[StrOrBuffer, int]: ... @overload def subn( - self, repl: Callable[[Match[RegexString]], RegexString], string: RegexString, count: int = ... - ) -> tuple[RegexString, int]: ... + self, repl: Callable[[Match[StrOrBuffer]], StrOrBuffer], string: StrOrBuffer, count: int = ... + ) -> tuple[StrOrBuffer, int]: ... if sys.version_info >= (3, 9): def __class_getitem__(cls, item: Any) -> GenericAlias: ... From 85a5004e1aa5cfca46ced83745b9b46959bc1861 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Feb 2022 22:14:20 +0000 Subject: [PATCH 22/29] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/re.pyi | 2 +- stdlib/typing.pyi | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/re.pyi b/stdlib/re.pyi index ce134e7215cc..93b94882f29f 100644 --- a/stdlib/re.pyi +++ b/stdlib/re.pyi @@ -1,8 +1,8 @@ import enum import sys +from _typeshed import StrOrBuffer from sre_constants import error as error from typing import Any, Callable, Iterator, Union, overload -from _typeshed import StrOrBuffer # ----- re variables and constants ----- if sys.version_info >= (3, 7): diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 2b32a7238db5..260c14c1c3af 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, StrOrBuffer +from _typeshed import Self, StrOrBuffer, SupportsKeysAndGetItem 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 From 46af759487cfec156a429f3c7cda33f96318e0b1 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Tue, 15 Feb 2022 00:20:08 +0200 Subject: [PATCH 23/29] Ignore linting error --- stdlib/_typeshed/__init__.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index b823398616e2..12e1c4066a2f 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -190,7 +190,7 @@ 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 -StrOrBuffer = TypeVar("StrOrBuffer", str, ReadOnlyBuffer, WriteableBuffer) +StrOrBuffer = TypeVar("StrOrBuffer", str, ReadOnlyBuffer, WriteableBuffer) # noqa Y001 # stable if sys.version_info >= (3, 10): From d9e68ac69118dce24361131ea4b96e6b4767d8e3 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Tue, 15 Feb 2022 00:22:44 +0200 Subject: [PATCH 24/29] Fixup old changes --- stdlib/mmap.pyi | 4 ++-- stdlib/typing.pyi | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/mmap.pyi b/stdlib/mmap.pyi index bc5b870e476f..1f44651dda00 100644 --- a/stdlib/mmap.pyi +++ b/stdlib/mmap.pyi @@ -1,7 +1,7 @@ import sys from _typeshed import ReadableBuffer from contextlib import AbstractContextManager -from typing import ByteString, Iterator, NoReturn, Sized, overload +from typing import Iterable, Iterator, NoReturn, Sized, overload ACCESS_DEFAULT: int ACCESS_READ: int @@ -27,7 +27,7 @@ if sys.platform != "win32": PAGESIZE: int -class mmap(AbstractContextManager[mmap], ByteString, Sized): +class mmap(AbstractContextManager[mmap], Iterable[int], Sized): if sys.platform == "win32": def __init__(self, fileno: int, length: int, tagname: str | None = ..., access: int = ..., offset: int = ...) -> None: ... else: diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 260c14c1c3af..d7d23c43f3ab 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -637,7 +637,7 @@ class Pattern(Generic[StrOrBuffer]): groups: int pattern: StrOrBuffer def search(self, string: StrOrBuffer, pos: int = ..., endpos: int = ...) -> Match[StrOrBuffer] | None: ... - def match(self, string: ByteString, pos: int = ..., endpos: int = ...) -> Match[StrOrBuffer] | None: ... + def match(self, string: StrOrBuffer, pos: int = ..., endpos: int = ...) -> Match[StrOrBuffer] | None: ... def fullmatch(self, string: StrOrBuffer, pos: int = ..., endpos: int = ...) -> Match[StrOrBuffer] | None: ... def split(self, string: StrOrBuffer, maxsplit: int = ...) -> list[StrOrBuffer | Any]: ... def findall(self, string: StrOrBuffer, pos: int = ..., endpos: int = ...) -> list[Any]: ... From 0fe7ca31a4dceb5a21fe05d515ba68a536e433d3 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Fri, 18 Feb 2022 18:01:49 +0200 Subject: [PATCH 25/29] Revert last changes --- stdlib/_typeshed/__init__.pyi | 2 - stdlib/re.pyi | 85 +++++++++++++---------------------- stdlib/typing.pyi | 66 +++++++++++++-------------- 3 files changed, 62 insertions(+), 91 deletions(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index 12e1c4066a2f..103af47c7524 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -190,8 +190,6 @@ 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 -StrOrBuffer = TypeVar("StrOrBuffer", str, ReadOnlyBuffer, WriteableBuffer) # noqa Y001 - # stable if sys.version_info >= (3, 10): from types import NoneType as NoneType diff --git a/stdlib/re.pyi b/stdlib/re.pyi index 93b94882f29f..01a60d170c50 100644 --- a/stdlib/re.pyi +++ b/stdlib/re.pyi @@ -1,8 +1,7 @@ import enum import sys -from _typeshed import StrOrBuffer from sre_constants import error as error -from typing import Any, Callable, Iterator, Union, overload +from typing import Any, AnyStr, Callable, Iterator, Union, overload # ----- re variables and constants ----- if sys.version_info >= (3, 7): @@ -53,90 +52,66 @@ if sys.version_info < (3, 7): _pattern_type: type @overload -def compile(pattern: StrOrBuffer, flags: _FlagsType = ...) -> Pattern[StrOrBuffer]: ... +def compile(pattern: AnyStr, flags: _FlagsType = ...) -> Pattern[AnyStr]: ... @overload -def compile(pattern: Pattern[StrOrBuffer], flags: _FlagsType = ...) -> Pattern[StrOrBuffer]: ... +def compile(pattern: Pattern[AnyStr], flags: _FlagsType = ...) -> Pattern[AnyStr]: ... @overload -def search(pattern: StrOrBuffer, string: StrOrBuffer, flags: _FlagsType = ...) -> Match[StrOrBuffer] | None: ... +def search(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... @overload -def search(pattern: Pattern[StrOrBuffer], string: StrOrBuffer, flags: _FlagsType = ...) -> Match[StrOrBuffer] | None: ... +def search(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... @overload -def match(pattern: StrOrBuffer, string: StrOrBuffer, flags: _FlagsType = ...) -> Match[StrOrBuffer] | None: ... +def match(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... @overload -def match(pattern: Pattern[StrOrBuffer], string: StrOrBuffer, flags: _FlagsType = ...) -> Match[StrOrBuffer] | None: ... +def match(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... # New in Python 3.4 @overload -def fullmatch(pattern: StrOrBuffer, string: StrOrBuffer, flags: _FlagsType = ...) -> Match[StrOrBuffer] | None: ... +def fullmatch(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... @overload -def fullmatch(pattern: Pattern[StrOrBuffer], string: StrOrBuffer, flags: _FlagsType = ...) -> Match[StrOrBuffer] | None: ... +def fullmatch(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... @overload -def split(pattern: StrOrBuffer, string: StrOrBuffer, maxsplit: int = ..., flags: _FlagsType = ...) -> list[StrOrBuffer | Any]: ... +def split(pattern: AnyStr, string: AnyStr, maxsplit: int = ..., flags: _FlagsType = ...) -> list[AnyStr | Any]: ... @overload -def split( - pattern: Pattern[StrOrBuffer], string: StrOrBuffer, maxsplit: int = ..., flags: _FlagsType = ... -) -> list[StrOrBuffer | Any]: ... +def split(pattern: Pattern[AnyStr], string: AnyStr, maxsplit: int = ..., flags: _FlagsType = ...) -> list[AnyStr | Any]: ... @overload -def findall(pattern: StrOrBuffer, string: StrOrBuffer, flags: _FlagsType = ...) -> list[Any]: ... +def findall(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> list[Any]: ... @overload -def findall(pattern: Pattern[StrOrBuffer], string: StrOrBuffer, flags: _FlagsType = ...) -> list[Any]: ... +def findall(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> list[Any]: ... # Return an iterator yielding match objects over all non-overlapping matches # for the RE pattern in string. The string is scanned left-to-right, and # matches are returned in the order found. Empty matches are included in the # result unless they touch the beginning of another match. @overload -def finditer(pattern: StrOrBuffer, string: StrOrBuffer, flags: _FlagsType = ...) -> Iterator[Match[StrOrBuffer]]: ... +def finditer(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Iterator[Match[AnyStr]]: ... @overload -def finditer(pattern: Pattern[StrOrBuffer], string: StrOrBuffer, flags: _FlagsType = ...) -> Iterator[Match[StrOrBuffer]]: ... +def finditer(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Iterator[Match[AnyStr]]: ... @overload -def sub( - pattern: StrOrBuffer, repl: StrOrBuffer, string: StrOrBuffer, count: int = ..., flags: _FlagsType = ... -) -> StrOrBuffer: ... +def sub(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> AnyStr: ... @overload def sub( - pattern: StrOrBuffer, - repl: Callable[[Match[StrOrBuffer]], StrOrBuffer], - string: StrOrBuffer, - count: int = ..., - flags: _FlagsType = ..., -) -> StrOrBuffer: ... + pattern: AnyStr, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ... +) -> AnyStr: ... @overload -def sub( - pattern: Pattern[StrOrBuffer], repl: StrOrBuffer, string: StrOrBuffer, count: int = ..., flags: _FlagsType = ... -) -> StrOrBuffer: ... +def sub(pattern: Pattern[AnyStr], repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> AnyStr: ... @overload def sub( - pattern: Pattern[StrOrBuffer], - repl: Callable[[Match[StrOrBuffer]], StrOrBuffer], - string: StrOrBuffer, - count: int = ..., - flags: _FlagsType = ..., -) -> StrOrBuffer: ... + pattern: Pattern[AnyStr], repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ... +) -> AnyStr: ... @overload -def subn( - pattern: StrOrBuffer, repl: StrOrBuffer, string: StrOrBuffer, count: int = ..., flags: _FlagsType = ... -) -> tuple[StrOrBuffer, int]: ... +def subn(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> tuple[AnyStr, int]: ... @overload def subn( - pattern: StrOrBuffer, - repl: Callable[[Match[StrOrBuffer]], StrOrBuffer], - string: StrOrBuffer, - count: int = ..., - flags: _FlagsType = ..., -) -> tuple[StrOrBuffer, int]: ... + pattern: AnyStr, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ... +) -> tuple[AnyStr, int]: ... @overload def subn( - pattern: Pattern[StrOrBuffer], repl: StrOrBuffer, string: StrOrBuffer, count: int = ..., flags: _FlagsType = ... -) -> tuple[StrOrBuffer, int]: ... + pattern: Pattern[AnyStr], repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ... +) -> tuple[AnyStr, int]: ... @overload def subn( - pattern: Pattern[StrOrBuffer], - repl: Callable[[Match[StrOrBuffer]], StrOrBuffer], - string: StrOrBuffer, - count: int = ..., - flags: _FlagsType = ..., -) -> tuple[StrOrBuffer, int]: ... -def escape(pattern: StrOrBuffer) -> StrOrBuffer: ... + pattern: Pattern[AnyStr], repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ... +) -> tuple[AnyStr, int]: ... +def escape(pattern: AnyStr) -> AnyStr: ... def purge() -> None: ... -def template(pattern: StrOrBuffer | Pattern[StrOrBuffer], flags: _FlagsType = ...) -> Pattern[StrOrBuffer]: ... +def template(pattern: AnyStr | Pattern[AnyStr], flags: _FlagsType = ...) -> Pattern[AnyStr]: ... diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index d7d23c43f3ab..5a8557f909e5 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, StrOrBuffer, SupportsKeysAndGetItem +from _typeshed import Self, SupportsKeysAndGetItem 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,71 +587,69 @@ class TextIO(IO[str]): class ByteString(Sequence[int], metaclass=ABCMeta): ... @_final -class Match(Generic[StrOrBuffer]): +class Match(Generic[AnyStr]): pos: int endpos: int lastindex: int | None lastgroup: str | None - string: StrOrBuffer + string: AnyStr # The regular expression object whose match() or search() method produced # this match instance. - re: Pattern[StrOrBuffer] - def expand(self, template: StrOrBuffer) -> StrOrBuffer: ... - # group() returns "StrOrBuffer" or "StrOrBuffer | None", depending on the pattern. + re: Pattern[AnyStr] + def expand(self, template: AnyStr) -> AnyStr: ... + # group() returns "AnyStr" or "AnyStr | None", depending on the pattern. @overload - def group(self, __group: _Literal[0] = ...) -> StrOrBuffer: ... + def group(self, __group: _Literal[0] = ...) -> AnyStr: ... @overload - def group(self, __group: str | int) -> StrOrBuffer | Any: ... + def group(self, __group: str | int) -> AnyStr | Any: ... @overload - def group(self, __group1: str | int, __group2: str | int, *groups: str | int) -> tuple[StrOrBuffer | Any, ...]: ... - # Each item of groups()'s return tuple is either "StrOrBuffer" or - # "StrOrBuffer | None", depending on the pattern. + def group(self, __group1: str | int, __group2: str | int, *groups: str | int) -> tuple[AnyStr | Any, ...]: ... + # Each item of groups()'s return tuple is either "AnyStr" or + # "AnyStr | None", depending on the pattern. @overload - def groups(self) -> tuple[StrOrBuffer | Any, ...]: ... + def groups(self) -> tuple[AnyStr | Any, ...]: ... @overload - def groups(self, default: _T) -> tuple[StrOrBuffer | _T, ...]: ... - # Each value in groupdict()'s return dict is either "StrOrBuffer" or - # "StrOrBuffer | None", depending on the pattern. + def groups(self, default: _T) -> tuple[AnyStr | _T, ...]: ... + # Each value in groupdict()'s return dict is either "AnyStr" or + # "AnyStr | None", depending on the pattern. @overload - def groupdict(self) -> dict[str, StrOrBuffer | Any]: ... + def groupdict(self) -> dict[str, AnyStr | Any]: ... @overload - def groupdict(self, default: _T) -> dict[str, StrOrBuffer | _T]: ... + def groupdict(self, default: _T) -> dict[str, AnyStr | _T]: ... def start(self, __group: int | str = ...) -> int: ... def end(self, __group: int | str = ...) -> int: ... def span(self, __group: int | str = ...) -> tuple[int, int]: ... @property def regs(self) -> tuple[tuple[int, int], ...]: ... # undocumented - # __getitem__() returns "StrOrBuffer" or "StrOrBuffer | None", depending on the pattern. + # __getitem__() returns "AnyStr" or "AnyStr | None", depending on the pattern. @overload - def __getitem__(self, __key: _Literal[0]) -> StrOrBuffer: ... + def __getitem__(self, __key: _Literal[0]) -> AnyStr: ... @overload - def __getitem__(self, __key: int | str) -> StrOrBuffer | Any: ... + def __getitem__(self, __key: int | str) -> AnyStr | Any: ... if sys.version_info >= (3, 9): def __class_getitem__(cls, item: Any) -> GenericAlias: ... @_final -class Pattern(Generic[StrOrBuffer]): +class Pattern(Generic[AnyStr]): flags: int groupindex: Mapping[str, int] groups: int - pattern: StrOrBuffer - def search(self, string: StrOrBuffer, pos: int = ..., endpos: int = ...) -> Match[StrOrBuffer] | None: ... - def match(self, string: StrOrBuffer, pos: int = ..., endpos: int = ...) -> Match[StrOrBuffer] | None: ... - def fullmatch(self, string: StrOrBuffer, pos: int = ..., endpos: int = ...) -> Match[StrOrBuffer] | None: ... - def split(self, string: StrOrBuffer, maxsplit: int = ...) -> list[StrOrBuffer | Any]: ... - def findall(self, string: StrOrBuffer, pos: int = ..., endpos: int = ...) -> list[Any]: ... - def finditer(self, string: StrOrBuffer, pos: int = ..., endpos: int = ...) -> Iterator[Match[StrOrBuffer]]: ... + 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 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]: ... + def finditer(self, string: AnyStr, pos: int = ..., endpos: int = ...) -> Iterator[Match[AnyStr]]: ... @overload - def sub(self, repl: StrOrBuffer, string: StrOrBuffer, count: int = ...) -> StrOrBuffer: ... + def sub(self, repl: AnyStr, string: AnyStr, count: int = ...) -> AnyStr: ... @overload - def sub(self, repl: Callable[[Match[StrOrBuffer]], StrOrBuffer], string: StrOrBuffer, count: int = ...) -> StrOrBuffer: ... + def sub(self, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ...) -> AnyStr: ... @overload - def subn(self, repl: StrOrBuffer, string: StrOrBuffer, count: int = ...) -> tuple[StrOrBuffer, int]: ... + def subn(self, repl: AnyStr, string: AnyStr, count: int = ...) -> tuple[AnyStr, int]: ... @overload - def subn( - self, repl: Callable[[Match[StrOrBuffer]], StrOrBuffer], string: StrOrBuffer, count: int = ... - ) -> tuple[StrOrBuffer, int]: ... + def subn(self, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ...) -> tuple[AnyStr, int]: ... if sys.version_info >= (3, 9): def __class_getitem__(cls, item: Any) -> GenericAlias: ... From c4f23f7087c802f78e656a9cb1a3905bbb298243 Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Sat, 19 Feb 2022 12:02:57 +0200 Subject: [PATCH 26/29] Add second typevar argument to `Match` --- stdlib/typing.pyi | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 5a8557f909e5..129e3067ebab 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 Self, SupportsKeysAndGetItem, ReadableBuffer, 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 @@ -586,13 +586,15 @@ class TextIO(IO[str]): class ByteString(Sequence[int], metaclass=ABCMeta): ... +MatchString = TypeVar("MatchString", bound=str | bytes | ReadableBuffer) + @_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 +638,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]: ... From e26ab0c8486a4b6d4c1971e6603cfa1ee683dc2e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 19 Feb 2022 10:04:30 +0000 Subject: [PATCH 27/29] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/typing.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 129e3067ebab..d9609d15aca6 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, ReadableBuffer, T_ReadableBuffer +from _typeshed import 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 From a410b160da37fd01efcd4cc9ecd3c78e2c5fef8c Mon Sep 17 00:00:00 2001 From: Itai Steinherz Date: Sat, 19 Feb 2022 14:48:24 +0200 Subject: [PATCH 28/29] `re.match` with `mmap.mmap` PoC --- stdlib/_typeshed/__init__.pyi | 2 ++ stdlib/re.pyi | 5 +++-- stdlib/typing.pyi | 4 +--- 3 files changed, 6 insertions(+), 5 deletions(-) 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..357b6d0406a1 100644 --- a/stdlib/re.pyi +++ b/stdlib/re.pyi @@ -1,6 +1,7 @@ import enum import sys from sre_constants import error as error +from stdlib._typeshed import MatchString from typing import Any, AnyStr, Callable, Iterator, Union, overload # ----- re variables and constants ----- @@ -60,9 +61,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 d9609d15aca6..be65042b1878 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 ReadableBuffer, Self, SupportsKeysAndGetItem, T_ReadableBuffer +from _typeshed import Self, SupportsKeysAndGetItem, ReadableBuffer, T_ReadableBuffer, MatchString 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 @@ -586,8 +586,6 @@ class TextIO(IO[str]): class ByteString(Sequence[int], metaclass=ABCMeta): ... -MatchString = TypeVar("MatchString", bound=str | bytes | ReadableBuffer) - @_final class Match(Generic[AnyStr, MatchString]): pos: int From 7314338106c59d8b9935786f4cee5be6da94a932 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 19 Feb 2022 12:49:50 +0000 Subject: [PATCH 29/29] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/re.pyi | 3 ++- stdlib/typing.pyi | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/stdlib/re.pyi b/stdlib/re.pyi index 357b6d0406a1..e0468f73e08b 100644 --- a/stdlib/re.pyi +++ b/stdlib/re.pyi @@ -1,9 +1,10 @@ import enum import sys from sre_constants import error as error -from stdlib._typeshed import MatchString 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 diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index be65042b1878..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, ReadableBuffer, T_ReadableBuffer, MatchString +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