Skip to content

Commit ac43ae5

Browse files
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.
1 parent d6ce3ab commit ac43ae5

File tree

2 files changed

+13
-63
lines changed

2 files changed

+13
-63
lines changed

stdlib/re.pyi

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import enum
22
import sys
33
from sre_constants import error as error
4-
from typing import Any, AnyStr, Callable, Iterator, Union, overload
4+
from typing import Any, AnyStr, Callable, Iterator, Union
55

66
# ----- re variables and constants -----
77
if sys.version_info >= (3, 7):
@@ -51,67 +51,23 @@ if sys.version_info < (3, 7):
5151
# undocumented
5252
_pattern_type: type
5353

54-
@overload
55-
def compile(pattern: AnyStr, flags: _FlagsType = ...) -> Pattern[AnyStr]: ...
56-
@overload
57-
def compile(pattern: Pattern[AnyStr], flags: _FlagsType = ...) -> Pattern[AnyStr]: ...
58-
@overload
59-
def search(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ...
60-
@overload
61-
def search(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ...
62-
@overload
63-
def match(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ...
64-
@overload
65-
def match(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ...
54+
def compile(pattern: AnyStr | Pattern[AnyStr], flags: _FlagsType = ...) -> Pattern[AnyStr]: ...
55+
def search(pattern: AnyStr | Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ...
56+
def match(pattern: AnyStr | Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ...
6657

6758
# New in Python 3.4
68-
@overload
69-
def fullmatch(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ...
70-
@overload
71-
def fullmatch(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ...
72-
@overload
73-
def split(pattern: AnyStr, string: AnyStr, maxsplit: int = ..., flags: _FlagsType = ...) -> list[AnyStr | Any]: ...
74-
@overload
75-
def split(pattern: Pattern[AnyStr], string: AnyStr, maxsplit: int = ..., flags: _FlagsType = ...) -> list[AnyStr | Any]: ...
76-
@overload
77-
def findall(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> list[Any]: ...
78-
@overload
79-
def findall(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> list[Any]: ...
59+
def fullmatch(pattern: AnyStr | Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ...
60+
def split(pattern: AnyStr | Pattern[AnyStr], string: AnyStr, maxsplit: int = ..., flags: _FlagsType = ...) -> list[AnyStr | Any]: ...
61+
def findall(pattern: AnyStr | Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> list[Any]: ...
8062

8163
# Return an iterator yielding match objects over all non-overlapping matches
8264
# for the RE pattern in string. The string is scanned left-to-right, and
8365
# matches are returned in the order found. Empty matches are included in the
8466
# result unless they touch the beginning of another match.
85-
@overload
86-
def finditer(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Iterator[Match[AnyStr]]: ...
87-
@overload
88-
def finditer(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Iterator[Match[AnyStr]]: ...
89-
@overload
90-
def sub(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> AnyStr: ...
91-
@overload
92-
def sub(
93-
pattern: AnyStr, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ...
94-
) -> AnyStr: ...
95-
@overload
96-
def sub(pattern: Pattern[AnyStr], repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> AnyStr: ...
97-
@overload
98-
def sub(
99-
pattern: Pattern[AnyStr], repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ...
100-
) -> AnyStr: ...
101-
@overload
102-
def subn(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> tuple[AnyStr, int]: ...
103-
@overload
104-
def subn(
105-
pattern: AnyStr, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ...
106-
) -> tuple[AnyStr, int]: ...
107-
@overload
108-
def subn(
109-
pattern: Pattern[AnyStr], repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ...
110-
) -> tuple[AnyStr, int]: ...
111-
@overload
112-
def subn(
113-
pattern: Pattern[AnyStr], repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ...
114-
) -> tuple[AnyStr, int]: ...
67+
def finditer(pattern: AnyStr | Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Iterator[Match[AnyStr]]: ...
68+
def sub(pattern: AnyStr | Pattern[AnyStr], repl: AnyStr | Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> AnyStr: ...
69+
def subn(pattern: AnyStr | Pattern[AnyStr], repl: AnyStr | Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> tuple[AnyStr, int]: ...
70+
11571
def escape(pattern: AnyStr) -> AnyStr: ...
11672
def purge() -> None: ...
11773
def template(pattern: AnyStr | Pattern[AnyStr], flags: _FlagsType = ...) -> Pattern[AnyStr]: ...

stdlib/typing.pyi

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -652,14 +652,8 @@ class Pattern(Generic[AnyStr]):
652652
def split(self, string: AnyStr, maxsplit: int = ...) -> list[AnyStr | Any]: ...
653653
def findall(self, string: AnyStr, pos: int = ..., endpos: int = ...) -> list[Any]: ...
654654
def finditer(self, string: AnyStr, pos: int = ..., endpos: int = ...) -> Iterator[Match[AnyStr]]: ...
655-
@overload
656-
def sub(self, repl: AnyStr, string: AnyStr, count: int = ...) -> AnyStr: ...
657-
@overload
658-
def sub(self, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ...) -> AnyStr: ...
659-
@overload
660-
def subn(self, repl: AnyStr, string: AnyStr, count: int = ...) -> tuple[AnyStr, int]: ...
661-
@overload
662-
def subn(self, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ...) -> tuple[AnyStr, int]: ...
655+
def sub(self, repl: AnyStr | Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ...) -> AnyStr: ...
656+
def subn(self, repl: AnyStr | Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ...) -> tuple[AnyStr, int]: ...
663657
def __copy__(self) -> Pattern[AnyStr]: ...
664658
def __deepcopy__(self, __memo: Any) -> Pattern[AnyStr]: ...
665659
if sys.version_info >= (3, 9):

0 commit comments

Comments
 (0)