Skip to content

Commit b00b4f3

Browse files
JelleZijlstraAlexWaygoodAkuli
authoredMay 10, 2022
regex: accept buffers (#7680)
Similar to #7679 Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> Co-authored-by: Akuli <akuviljanen17@gmail.com>
1 parent 68a781d commit b00b4f3

File tree

2 files changed

+406
-103
lines changed

2 files changed

+406
-103
lines changed
 

‎stubs/regex/regex/_regex.pyi

+214-67
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from _typeshed import Self
1+
from _typeshed import ReadableBuffer, Self
22
from collections.abc import Callable, Mapping
33
from typing import Any, AnyStr, Generic, TypeVar, overload
44
from typing_extensions import Literal, final
@@ -7,126 +7,273 @@ _T = TypeVar("_T")
77

88
@final
99
class Pattern(Generic[AnyStr]):
10-
pattern: AnyStr
11-
flags: int
12-
groups: int
13-
groupindex: Mapping[str, int]
14-
named_lists: Mapping[str, frozenset[AnyStr]]
10+
@property
11+
def flags(self) -> int: ...
12+
@property
13+
def groupindex(self) -> Mapping[str, int]: ...
14+
@property
15+
def groups(self) -> int: ...
16+
@property
17+
def pattern(self) -> AnyStr: ...
18+
@property
19+
def named_lists(self) -> Mapping[str, frozenset[AnyStr]]: ...
20+
@overload
1521
def search(
16-
self,
17-
string: AnyStr,
18-
pos: int | None = ...,
19-
endpos: int | None = ...,
22+
self: Pattern[str],
23+
string: str,
24+
pos: int = ...,
25+
endpos: int = ...,
26+
concurrent: bool | None = ...,
27+
timeout: float | None = ...,
28+
) -> Match[str] | None: ...
29+
@overload
30+
def search(
31+
self: Pattern[bytes],
32+
string: ReadableBuffer,
33+
pos: int = ...,
34+
endpos: int = ...,
2035
concurrent: bool | None = ...,
2136
timeout: float | None = ...,
22-
) -> Match[AnyStr] | None: ...
37+
) -> Match[bytes] | None: ...
38+
@overload
2339
def match(
24-
self,
25-
string: AnyStr,
26-
pos: int | None = ...,
27-
endpos: int | None = ...,
40+
self: Pattern[str],
41+
string: str,
42+
pos: int = ...,
43+
endpos: int = ...,
44+
concurrent: bool | None = ...,
45+
timeout: float | None = ...,
46+
) -> Match[str] | None: ...
47+
@overload
48+
def match(
49+
self: Pattern[bytes],
50+
string: ReadableBuffer,
51+
pos: int = ...,
52+
endpos: int = ...,
2853
concurrent: bool | None = ...,
2954
timeout: float | None = ...,
30-
) -> Match[AnyStr] | None: ...
55+
) -> Match[bytes] | None: ...
56+
@overload
3157
def fullmatch(
32-
self,
33-
string: AnyStr,
34-
pos: int | None = ...,
35-
endpos: int | None = ...,
58+
self: Pattern[str],
59+
string: str,
60+
pos: int = ...,
61+
endpos: int = ...,
62+
concurrent: bool | None = ...,
63+
timeout: float | None = ...,
64+
) -> Match[str] | None: ...
65+
@overload
66+
def fullmatch(
67+
self: Pattern[bytes],
68+
string: ReadableBuffer,
69+
pos: int = ...,
70+
endpos: int = ...,
3671
concurrent: bool | None = ...,
3772
timeout: float | None = ...,
38-
) -> Match[AnyStr] | None: ...
73+
) -> Match[bytes] | None: ...
74+
@overload
75+
def split(
76+
self: Pattern[str], string: str, maxsplit: int = ..., concurrent: bool | None = ..., timeout: float | None = ...
77+
) -> list[str | Any]: ...
78+
@overload
3979
def split(
40-
self, string: AnyStr, maxsplit: int = ..., concurrent: bool | None = ..., timeout: float | None = ...
41-
) -> list[AnyStr | Any]: ...
80+
self: Pattern[bytes],
81+
string: ReadableBuffer,
82+
maxsplit: int = ...,
83+
concurrent: bool | None = ...,
84+
timeout: float | None = ...,
85+
) -> list[bytes | Any]: ...
86+
@overload
4287
def splititer(
43-
self, string: AnyStr, maxsplit: int = ..., concurrent: bool | None = ..., timeout: float | None = ...
44-
) -> Splitter[AnyStr]: ...
88+
self: Pattern[str], string: str, maxsplit: int = ..., concurrent: bool | None = ..., timeout: float | None = ...
89+
) -> Splitter[str]: ...
90+
@overload
91+
def splititer(
92+
self: Pattern[bytes],
93+
string: ReadableBuffer,
94+
maxsplit: int = ...,
95+
concurrent: bool | None = ...,
96+
timeout: float | None = ...,
97+
) -> Splitter[bytes]: ...
98+
@overload
4599
def findall(
46-
self,
47-
string: AnyStr,
48-
pos: int | None = ...,
49-
endpos: int | None = ...,
100+
self: Pattern[str],
101+
string: str,
102+
pos: int = ...,
103+
endpos: int = ...,
50104
overlapped: bool = ...,
51105
concurrent: bool | None = ...,
52106
timeout: float | None = ...,
53107
) -> list[Any]: ...
108+
@overload
109+
def findall(
110+
self: Pattern[bytes],
111+
string: ReadableBuffer,
112+
pos: int = ...,
113+
endpos: int = ...,
114+
overlapped: bool = ...,
115+
concurrent: bool | None = ...,
116+
timeout: float | None = ...,
117+
) -> list[Any]: ...
118+
@overload
54119
def finditer(
55-
self,
56-
string: AnyStr,
120+
self: Pattern[str],
121+
string: str,
122+
pos: int = ...,
123+
endpos: int = ...,
124+
overlapped: bool = ...,
125+
concurrent: bool | None = ...,
126+
timeout: float | None = ...,
127+
) -> Scanner[str]: ...
128+
@overload
129+
def finditer(
130+
self: Pattern[bytes],
131+
string: ReadableBuffer,
132+
pos: int = ...,
133+
endpos: int = ...,
134+
overlapped: bool = ...,
135+
concurrent: bool | None = ...,
136+
timeout: float | None = ...,
137+
) -> Scanner[bytes]: ...
138+
@overload
139+
def sub(
140+
self: Pattern[str],
141+
repl: str | Callable[[Match[str]], str],
142+
string: str,
143+
count: int = ...,
144+
flags: int = ...,
57145
pos: int | None = ...,
58146
endpos: int | None = ...,
59-
overlapped: bool = ...,
60147
concurrent: bool | None = ...,
61148
timeout: float | None = ...,
62-
) -> Scanner[AnyStr]: ...
149+
) -> str: ...
150+
@overload
63151
def sub(
64-
self,
65-
repl: AnyStr | Callable[[Match[AnyStr]], AnyStr],
66-
string: AnyStr,
152+
self: Pattern[bytes],
153+
repl: ReadableBuffer | Callable[[Match[bytes]], ReadableBuffer],
154+
string: ReadableBuffer,
67155
count: int = ...,
68156
flags: int = ...,
69157
pos: int | None = ...,
70158
endpos: int | None = ...,
71159
concurrent: bool | None = ...,
72160
timeout: float | None = ...,
73-
) -> AnyStr: ...
161+
) -> bytes: ...
162+
@overload
74163
def subf(
75-
self,
76-
format: AnyStr | Callable[[Match[AnyStr]], AnyStr],
77-
string: AnyStr,
164+
self: Pattern[str],
165+
format: str | Callable[[Match[str]], str],
166+
string: str,
167+
count: int = ...,
168+
flags: int = ...,
169+
pos: int | None = ...,
170+
endpos: int | None = ...,
171+
concurrent: bool | None = ...,
172+
timeout: float | None = ...,
173+
) -> str: ...
174+
@overload
175+
def subf(
176+
self: Pattern[bytes],
177+
format: ReadableBuffer | Callable[[Match[bytes]], ReadableBuffer],
178+
string: ReadableBuffer,
179+
count: int = ...,
180+
flags: int = ...,
181+
pos: int | None = ...,
182+
endpos: int | None = ...,
183+
concurrent: bool | None = ...,
184+
timeout: float | None = ...,
185+
) -> bytes: ...
186+
@overload
187+
def subn(
188+
self: Pattern[str],
189+
repl: str | Callable[[Match[str]], str],
190+
string: str,
78191
count: int = ...,
79192
flags: int = ...,
80193
pos: int | None = ...,
81194
endpos: int | None = ...,
82195
concurrent: bool | None = ...,
83196
timeout: float | None = ...,
84-
) -> AnyStr: ...
197+
) -> tuple[str, int]: ...
198+
@overload
85199
def subn(
86-
self,
87-
repl: AnyStr | Callable[[Match[AnyStr]], AnyStr],
88-
string: AnyStr,
200+
self: Pattern[bytes],
201+
repl: ReadableBuffer | Callable[[Match[bytes]], ReadableBuffer],
202+
string: ReadableBuffer,
203+
count: int = ...,
204+
flags: int = ...,
205+
pos: int | None = ...,
206+
endpos: int | None = ...,
207+
concurrent: bool | None = ...,
208+
timeout: float | None = ...,
209+
) -> tuple[bytes, int]: ...
210+
@overload
211+
def subfn(
212+
self: Pattern[str],
213+
format: str | Callable[[Match[str]], str],
214+
string: str,
89215
count: int = ...,
90216
flags: int = ...,
91217
pos: int | None = ...,
92218
endpos: int | None = ...,
93219
concurrent: bool | None = ...,
94220
timeout: float | None = ...,
95-
) -> tuple[AnyStr, int]: ...
221+
) -> tuple[str, int]: ...
222+
@overload
96223
def subfn(
97-
self,
98-
format: AnyStr | Callable[[Match[AnyStr]], AnyStr],
99-
string: AnyStr,
224+
self: Pattern[bytes],
225+
format: ReadableBuffer | Callable[[Match[bytes]], ReadableBuffer],
226+
string: ReadableBuffer,
100227
count: int = ...,
101228
flags: int = ...,
102229
pos: int | None = ...,
103230
endpos: int | None = ...,
104231
concurrent: bool | None = ...,
105232
timeout: float | None = ...,
106-
) -> tuple[AnyStr, int]: ...
233+
) -> tuple[bytes, int]: ...
234+
@overload
107235
def scanner(
108-
self,
109-
string: AnyStr,
236+
self: Pattern[str],
237+
string: str,
110238
pos: int | None = ...,
111239
endpos: int | None = ...,
112240
overlapped: bool = ...,
113241
concurrent: bool | None = ...,
114242
timeout: float | None = ...,
115-
) -> Scanner[AnyStr]: ...
243+
) -> Scanner[str]: ...
244+
@overload
245+
def scanner(
246+
self: Pattern[bytes],
247+
string: bytes,
248+
pos: int | None = ...,
249+
endpos: int | None = ...,
250+
overlapped: bool = ...,
251+
concurrent: bool | None = ...,
252+
timeout: float | None = ...,
253+
) -> Scanner[bytes]: ...
116254

117255
@final
118256
class Match(Generic[AnyStr]):
119-
120-
re: Pattern[AnyStr]
121-
string: AnyStr
122-
pos: int
123-
endpos: int
124-
partial: bool
125-
regs: tuple[tuple[int, int], ...]
126-
fuzzy_counts: tuple[int, int, int]
127-
fuzzy_changes: tuple[list[int], list[int], list[int]]
128-
lastgroup: str | None
129-
lastindex: int | None
257+
@property
258+
def pos(self) -> int: ...
259+
@property
260+
def endpos(self) -> int: ...
261+
@property
262+
def lastindex(self) -> int | None: ...
263+
@property
264+
def lastgroup(self) -> str | None: ...
265+
@property
266+
def string(self) -> AnyStr: ...
267+
@property
268+
def re(self) -> Pattern[AnyStr]: ...
269+
@property
270+
def partial(self) -> bool: ...
271+
@property
272+
def regs(self) -> tuple[tuple[int, int], ...]: ...
273+
@property
274+
def fuzzy_counts(self) -> tuple[int, int, int]: ...
275+
@property
276+
def fuzzy_changes(self) -> tuple[list[int], list[int], list[int]]: ...
130277
@overload
131278
def group(self, __group: Literal[0] = ...) -> AnyStr: ...
132279
@overload
@@ -180,16 +327,16 @@ class Match(Generic[AnyStr]):
180327

181328
@final
182329
class Splitter(Generic[AnyStr]):
183-
184-
pattern: Pattern[AnyStr]
330+
@property
331+
def pattern(self) -> Pattern[AnyStr]: ...
185332
def __iter__(self: Self) -> Self: ...
186333
def __next__(self) -> AnyStr | Any: ...
187334
def split(self) -> AnyStr | Any: ...
188335

189336
@final
190337
class Scanner(Generic[AnyStr]):
191-
192-
pattern: Pattern[AnyStr]
338+
@property
339+
def pattern(self) -> Pattern[AnyStr]: ...
193340
def __iter__(self: Self) -> Self: ...
194341
def __next__(self) -> Match[AnyStr]: ...
195342
def match(self) -> Match[AnyStr] | None: ...

0 commit comments

Comments
 (0)
Please sign in to comment.