From ac43ae5bdbdba190ede648144b0d8928a8d673db Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Tue, 22 Feb 2022 10:53:56 +0100 Subject: [PATCH 1/2] re: Merge overloaded functions by using unions The signatures of these functions can also be modeled without overloads using unions as parameter types. While this shouldn't make any difference for type checking, it improves the UX for people using IDEs because having to cycle between subtly different function signatures isn't fun. --- stdlib/re.pyi | 66 ++++++++--------------------------------------- stdlib/typing.pyi | 10 ++----- 2 files changed, 13 insertions(+), 63 deletions(-) diff --git a/stdlib/re.pyi b/stdlib/re.pyi index 01a60d170c50..486f7f7f221b 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 # ----- re variables and constants ----- if sys.version_info >= (3, 7): @@ -51,67 +51,23 @@ if sys.version_info < (3, 7): # undocumented _pattern_type: type -@overload -def compile(pattern: AnyStr, flags: _FlagsType = ...) -> Pattern[AnyStr]: ... -@overload -def compile(pattern: Pattern[AnyStr], flags: _FlagsType = ...) -> Pattern[AnyStr]: ... -@overload -def search(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... -@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: ... -@overload -def match(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... +def compile(pattern: AnyStr | Pattern[AnyStr], flags: _FlagsType = ...) -> Pattern[AnyStr]: ... +def search(pattern: AnyStr | Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... +def match(pattern: AnyStr | Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... # New in Python 3.4 -@overload -def fullmatch(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... -@overload -def fullmatch(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... -@overload -def split(pattern: AnyStr, string: AnyStr, maxsplit: int = ..., flags: _FlagsType = ...) -> list[AnyStr | Any]: ... -@overload -def split(pattern: Pattern[AnyStr], string: AnyStr, maxsplit: int = ..., flags: _FlagsType = ...) -> list[AnyStr | Any]: ... -@overload -def findall(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> list[Any]: ... -@overload -def findall(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> list[Any]: ... +def fullmatch(pattern: AnyStr | Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... +def split(pattern: AnyStr | Pattern[AnyStr], string: AnyStr, maxsplit: int = ..., flags: _FlagsType = ...) -> list[AnyStr | Any]: ... +def findall(pattern: AnyStr | 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: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Iterator[Match[AnyStr]]: ... -@overload -def finditer(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Iterator[Match[AnyStr]]: ... -@overload -def sub(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> AnyStr: ... -@overload -def sub( - pattern: AnyStr, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ... -) -> AnyStr: ... -@overload -def sub(pattern: Pattern[AnyStr], repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> AnyStr: ... -@overload -def sub( - pattern: Pattern[AnyStr], repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ... -) -> AnyStr: ... -@overload -def subn(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> tuple[AnyStr, int]: ... -@overload -def subn( - pattern: AnyStr, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ... -) -> tuple[AnyStr, int]: ... -@overload -def subn( - pattern: Pattern[AnyStr], repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ... -) -> tuple[AnyStr, int]: ... -@overload -def subn( - pattern: Pattern[AnyStr], repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ... -) -> tuple[AnyStr, int]: ... +def finditer(pattern: AnyStr | Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Iterator[Match[AnyStr]]: ... +def sub(pattern: AnyStr | Pattern[AnyStr], repl: AnyStr | Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> AnyStr: ... +def subn(pattern: AnyStr | Pattern[AnyStr], repl: AnyStr | Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> tuple[AnyStr, int]: ... + def escape(pattern: AnyStr) -> AnyStr: ... def purge() -> None: ... def template(pattern: AnyStr | Pattern[AnyStr], flags: _FlagsType = ...) -> Pattern[AnyStr]: ... diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 202d4f287c43..bc1606be1f71 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -652,14 +652,8 @@ class Pattern(Generic[AnyStr]): 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: AnyStr, string: AnyStr, count: int = ...) -> AnyStr: ... - @overload - def sub(self, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ...) -> AnyStr: ... - @overload - def subn(self, repl: AnyStr, string: AnyStr, count: int = ...) -> tuple[AnyStr, int]: ... - @overload - def subn(self, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ...) -> tuple[AnyStr, int]: ... + def sub(self, repl: AnyStr | Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ...) -> AnyStr: ... + def subn(self, repl: AnyStr | Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ...) -> tuple[AnyStr, int]: ... def __copy__(self) -> Pattern[AnyStr]: ... def __deepcopy__(self, __memo: Any) -> Pattern[AnyStr]: ... if sys.version_info >= (3, 9): From c2a403eb99d3f6ca9306c8fd49ec015adca140f3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 22 Feb 2022 10:23:45 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/re.pyi | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/stdlib/re.pyi b/stdlib/re.pyi index 486f7f7f221b..a0be723c5303 100644 --- a/stdlib/re.pyi +++ b/stdlib/re.pyi @@ -57,7 +57,9 @@ def match(pattern: AnyStr | Pattern[AnyStr], string: AnyStr, flags: _FlagsType = # New in Python 3.4 def fullmatch(pattern: AnyStr | Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... -def split(pattern: AnyStr | Pattern[AnyStr], string: AnyStr, maxsplit: int = ..., flags: _FlagsType = ...) -> list[AnyStr | Any]: ... +def split( + pattern: AnyStr | Pattern[AnyStr], string: AnyStr, maxsplit: int = ..., flags: _FlagsType = ... +) -> list[AnyStr | Any]: ... def findall(pattern: AnyStr | Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> list[Any]: ... # Return an iterator yielding match objects over all non-overlapping matches @@ -65,9 +67,20 @@ def findall(pattern: AnyStr | Pattern[AnyStr], string: AnyStr, flags: _FlagsType # matches are returned in the order found. Empty matches are included in the # result unless they touch the beginning of another match. def finditer(pattern: AnyStr | Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Iterator[Match[AnyStr]]: ... -def sub(pattern: AnyStr | Pattern[AnyStr], repl: AnyStr | Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> AnyStr: ... -def subn(pattern: AnyStr | Pattern[AnyStr], repl: AnyStr | Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> tuple[AnyStr, int]: ... - +def sub( + pattern: AnyStr | Pattern[AnyStr], + repl: AnyStr | Callable[[Match[AnyStr]], AnyStr], + string: AnyStr, + count: int = ..., + flags: _FlagsType = ..., +) -> AnyStr: ... +def subn( + pattern: AnyStr | Pattern[AnyStr], + repl: AnyStr | Callable[[Match[AnyStr]], AnyStr], + string: AnyStr, + count: int = ..., + flags: _FlagsType = ..., +) -> tuple[AnyStr, int]: ... def escape(pattern: AnyStr) -> AnyStr: ... def purge() -> None: ... def template(pattern: AnyStr | Pattern[AnyStr], flags: _FlagsType = ...) -> Pattern[AnyStr]: ...