Skip to content

Commit 3e37029

Browse files
committed
Almost all re functions take a compiled pattern. (Even re.compile()!) (#203)
* Almost all re functions take a compiled pattern. (Even re.compile()!) Fixes #188 Note: I'm using AnyStr so that the type of string used for pattern and for the rest of the arguments must match. This is not 100% correct, since Python 2 sometimes allows mixed types. But sometimes it doesn't, depending on the values (e.g. non-ASCII bytes), and Python 3 always insists on matching, so I think this is actually a good idea. * Same treatment for stdlib/3/re.pyi.
1 parent d09cc05 commit 3e37029

File tree

2 files changed

+97
-21
lines changed

2 files changed

+97
-21
lines changed

stdlib/2.7/re.pyi

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,56 @@ TEMPLATE = 0
2828

2929
class error(Exception): ...
3030

31+
@overload
3132
def compile(pattern: AnyStr, flags: int = ...) -> Pattern[AnyStr]: ...
32-
def search(pattern: AnyStr, string: AnyStr,
33-
flags: int = ...) -> Match[AnyStr]: ...
34-
def match(pattern: AnyStr, string: AnyStr,
35-
flags: int = ...) -> Match[AnyStr]: ...
36-
def split(pattern: AnyStr, string: AnyStr, maxsplit: int = ...,
37-
flags: int = ...) -> List[AnyStr]: ...
38-
def findall(pattern: AnyStr, string: AnyStr,
39-
flags: int = ...) -> List[AnyStr]: ...
33+
@overload
34+
def compile(pattern: Pattern[AnyStr], flags: int = ...) -> Pattern[AnyStr]: ...
35+
36+
@overload
37+
def search(pattern: AnyStr, string: AnyStr, flags: int = ...) -> Match[AnyStr]: ...
38+
@overload
39+
def search(pattern: Pattern[AnyStr], string: AnyStr, flags: int = ...) -> Match[AnyStr]: ...
40+
41+
@overload
42+
def match(pattern: AnyStr, string: AnyStr, flags: int = ...) -> Match[AnyStr]: ...
43+
@overload
44+
def match(pattern: Pattern[AnyStr], string: AnyStr, flags: int = ...) -> Match[AnyStr]: ...
45+
46+
@overload
47+
def split(pattern: AnyStr, string: AnyStr,
48+
maxsplit: int = ..., flags: int = ...) -> List[AnyStr]: ...
49+
@overload
50+
def split(pattern: Pattern[AnyStr], string: AnyStr,
51+
maxsplit: int = ..., flags: int = ...) -> List[AnyStr]: ...
52+
53+
@overload
54+
def findall(pattern: AnyStr, string: AnyStr, flags: int = ...) -> List[AnyStr]: ...
55+
@overload
56+
def findall(pattern: Pattern[AnyStr], string: AnyStr, flags: int = ...) -> List[AnyStr]: ...
4057

4158
# Return an iterator yielding match objects over all non-overlapping matches
4259
# for the RE pattern in string. The string is scanned left-to-right, and
4360
# matches are returned in the order found. Empty matches are included in the
4461
# result unless they touch the beginning of another match.
62+
@overload
4563
def finditer(pattern: AnyStr, string: AnyStr,
4664
flags: int = ...) -> Iterator[Match[AnyStr]]: ...
65+
@overload
66+
def finditer(pattern: Pattern[AnyStr], string: AnyStr,
67+
flags: int = ...) -> Iterator[Match[AnyStr]]: ...
4768

4869
@overload
4970
def sub(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ...,
5071
flags: int = ...) -> AnyStr: ...
5172
@overload
5273
def sub(pattern: AnyStr, repl: Callable[[Match[AnyStr]], AnyStr],
5374
string: AnyStr, count: int = ..., flags: int = ...) -> AnyStr: ...
75+
@overload
76+
def sub(pattern: Pattern[AnyStr], repl: AnyStr, string: AnyStr, count: int = ...,
77+
flags: int = ...) -> AnyStr: ...
78+
@overload
79+
def sub(pattern: Pattern[AnyStr], repl: Callable[[Match[AnyStr]], AnyStr],
80+
string: AnyStr, count: int = ..., flags: int = ...) -> AnyStr: ...
5481

5582
@overload
5683
def subn(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ...,
@@ -59,6 +86,13 @@ def subn(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ...,
5986
def subn(pattern: AnyStr, repl: Callable[[Match[AnyStr]], AnyStr],
6087
string: AnyStr, count: int = ...,
6188
flags: int = ...) -> Tuple[AnyStr, int]: ...
89+
@overload
90+
def subn(pattern: Pattern[AnyStr], repl: AnyStr, string: AnyStr, count: int = ...,
91+
flags: int = ...) -> Tuple[AnyStr, int]: ...
92+
@overload
93+
def subn(pattern: Pattern[AnyStr], repl: Callable[[Match[AnyStr]], AnyStr],
94+
string: AnyStr, count: int = ...,
95+
flags: int = ...) -> Tuple[AnyStr, int]: ...
6296

6397
def escape(string: AnyStr) -> AnyStr: ...
6498

stdlib/3/re.pyi

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# and http://hg.python.org/cpython/file/618ea5612e83/Lib/re.py
77

88
from typing import (
9-
List, Iterator, Callable, Tuple, Sequence, Dict, Union,
9+
List, Iterator, overload, Callable, Tuple, Sequence, Dict,
1010
Generic, AnyStr, Match, Pattern
1111
)
1212

@@ -29,29 +29,71 @@ UNICODE = 0
2929

3030
class error(Exception): ...
3131

32+
@overload
3233
def compile(pattern: AnyStr, flags: int = ...) -> Pattern[AnyStr]: ...
33-
def search(pattern: AnyStr, string: AnyStr,
34-
flags: int = ...) -> Match[AnyStr]: ...
35-
def match(pattern: AnyStr, string: AnyStr,
36-
flags: int = ...) -> Match[AnyStr]: ...
37-
def split(pattern: AnyStr, string: AnyStr, maxsplit: int = ...,
38-
flags: int = ...) -> List[AnyStr]: ...
39-
def findall(pattern: AnyStr, string: AnyStr,
40-
flags: int = ...) -> List[AnyStr]: ...
34+
@overload
35+
def compile(pattern: Pattern[AnyStr], flags: int = ...) -> Pattern[AnyStr]: ...
36+
37+
@overload
38+
def search(pattern: AnyStr, string: AnyStr, flags: int = ...) -> Match[AnyStr]: ...
39+
@overload
40+
def search(pattern: Pattern[AnyStr], string: AnyStr, flags: int = ...) -> Match[AnyStr]: ...
41+
42+
@overload
43+
def match(pattern: AnyStr, string: AnyStr, flags: int = ...) -> Match[AnyStr]: ...
44+
@overload
45+
def match(pattern: Pattern[AnyStr], string: AnyStr, flags: int = ...) -> Match[AnyStr]: ...
46+
47+
@overload
48+
def split(pattern: AnyStr, string: AnyStr,
49+
maxsplit: int = ..., flags: int = ...) -> List[AnyStr]: ...
50+
@overload
51+
def split(pattern: Pattern[AnyStr], string: AnyStr,
52+
maxsplit: int = ..., flags: int = ...) -> List[AnyStr]: ...
53+
54+
@overload
55+
def findall(pattern: AnyStr, string: AnyStr, flags: int = ...) -> List[AnyStr]: ...
56+
@overload
57+
def findall(pattern: Pattern[AnyStr], string: AnyStr, flags: int = ...) -> List[AnyStr]: ...
4158

4259
# Return an iterator yielding match objects over all non-overlapping matches
4360
# for the RE pattern in string. The string is scanned left-to-right, and
4461
# matches are returned in the order found. Empty matches are included in the
4562
# result unless they touch the beginning of another match.
63+
@overload
4664
def finditer(pattern: AnyStr, string: AnyStr,
4765
flags: int = ...) -> Iterator[Match[AnyStr]]: ...
66+
@overload
67+
def finditer(pattern: Pattern[AnyStr], string: AnyStr,
68+
flags: int = ...) -> Iterator[Match[AnyStr]]: ...
4869

49-
def sub(pattern: AnyStr, repl: Union[AnyStr, Callable[[Match[AnyStr]], AnyStr]],
70+
@overload
71+
def sub(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ...,
72+
flags: int = ...) -> AnyStr: ...
73+
@overload
74+
def sub(pattern: AnyStr, repl: Callable[[Match[AnyStr]], AnyStr],
75+
string: AnyStr, count: int = ..., flags: int = ...) -> AnyStr: ...
76+
@overload
77+
def sub(pattern: Pattern[AnyStr], repl: AnyStr, string: AnyStr, count: int = ...,
78+
flags: int = ...) -> AnyStr: ...
79+
@overload
80+
def sub(pattern: Pattern[AnyStr], repl: Callable[[Match[AnyStr]], AnyStr],
5081
string: AnyStr, count: int = ..., flags: int = ...) -> AnyStr: ...
5182

52-
def subn(pattern: AnyStr, repl: Union[AnyStr, Callable[[Match[AnyStr]], AnyStr]],
53-
string: AnyStr, count: int = ..., flags: int = ...) -> Tuple[AnyStr, int]:
54-
...
83+
@overload
84+
def subn(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ...,
85+
flags: int = ...) -> Tuple[AnyStr, int]: ...
86+
@overload
87+
def subn(pattern: AnyStr, repl: Callable[[Match[AnyStr]], AnyStr],
88+
string: AnyStr, count: int = ...,
89+
flags: int = ...) -> Tuple[AnyStr, int]: ...
90+
@overload
91+
def subn(pattern: Pattern[AnyStr], repl: AnyStr, string: AnyStr, count: int = ...,
92+
flags: int = ...) -> Tuple[AnyStr, int]: ...
93+
@overload
94+
def subn(pattern: Pattern[AnyStr], repl: Callable[[Match[AnyStr]], AnyStr],
95+
string: AnyStr, count: int = ...,
96+
flags: int = ...) -> Tuple[AnyStr, int]: ...
5597

5698
def escape(string: AnyStr) -> AnyStr: ...
5799

0 commit comments

Comments
 (0)