|
1 | 1 | import enum
|
2 | 2 | import sre_compile
|
3 | 3 | import sys
|
| 4 | +from _typeshed import ReadableBuffer |
4 | 5 | from collections.abc import Callable, Iterator
|
5 | 6 | from sre_constants import error as error
|
6 | 7 | from typing import Any, AnyStr, overload
|
@@ -155,70 +156,67 @@ if sys.version_info < (3, 7):
|
155 | 156 | # undocumented
|
156 | 157 | _pattern_type: type
|
157 | 158 |
|
158 |
| -# Type-wise these overloads are unnecessary, they could also be modeled using |
| 159 | +# Type-wise the compile() overloads are unnecessary, they could also be modeled using |
159 | 160 | # unions in the parameter types. However mypy has a bug regarding TypeVar
|
160 | 161 | # constraints (https://github.com/python/mypy/issues/11880),
|
161 | 162 | # which limits us here because AnyStr is a constrained TypeVar.
|
162 | 163 |
|
| 164 | +# pattern arguments do *not* accept arbitrary buffers such as bytearray, |
| 165 | +# because the pattern must be hashable. |
163 | 166 | @overload
|
164 | 167 | def compile(pattern: AnyStr, flags: _FlagsType = ...) -> Pattern[AnyStr]: ...
|
165 | 168 | @overload
|
166 | 169 | def compile(pattern: Pattern[AnyStr], flags: _FlagsType = ...) -> Pattern[AnyStr]: ...
|
167 | 170 | @overload
|
168 |
| -def search(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... |
| 171 | +def search(pattern: str | Pattern[str], string: str, flags: _FlagsType = ...) -> Match[str] | None: ... |
169 | 172 | @overload
|
170 |
| -def search(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... |
| 173 | +def search(pattern: bytes | Pattern[bytes], string: ReadableBuffer, flags: _FlagsType = ...) -> Match[bytes] | None: ... |
171 | 174 | @overload
|
172 |
| -def match(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... |
| 175 | +def match(pattern: str | Pattern[str], string: str, flags: _FlagsType = ...) -> Match[str] | None: ... |
173 | 176 | @overload
|
174 |
| -def match(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... |
| 177 | +def match(pattern: bytes | Pattern[bytes], string: ReadableBuffer, flags: _FlagsType = ...) -> Match[bytes] | None: ... |
175 | 178 | @overload
|
176 |
| -def fullmatch(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... |
| 179 | +def fullmatch(pattern: str | Pattern[str], string: str, flags: _FlagsType = ...) -> Match[str] | None: ... |
177 | 180 | @overload
|
178 |
| -def fullmatch(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Match[AnyStr] | None: ... |
| 181 | +def fullmatch(pattern: bytes | Pattern[bytes], string: ReadableBuffer, flags: _FlagsType = ...) -> Match[bytes] | None: ... |
179 | 182 | @overload
|
180 |
| -def split(pattern: AnyStr, string: AnyStr, maxsplit: int = ..., flags: _FlagsType = ...) -> list[AnyStr | Any]: ... |
| 183 | +def split(pattern: str | Pattern[str], string: str, maxsplit: int = ..., flags: _FlagsType = ...) -> list[str | Any]: ... |
181 | 184 | @overload
|
182 |
| -def split(pattern: Pattern[AnyStr], string: AnyStr, maxsplit: int = ..., flags: _FlagsType = ...) -> list[AnyStr | Any]: ... |
| 185 | +def split( |
| 186 | + pattern: bytes | Pattern[bytes], string: ReadableBuffer, maxsplit: int = ..., flags: _FlagsType = ... |
| 187 | +) -> list[bytes | Any]: ... |
183 | 188 | @overload
|
184 |
| -def findall(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> list[Any]: ... |
| 189 | +def findall(pattern: str | Pattern[str], string: str, flags: _FlagsType = ...) -> list[Any]: ... |
185 | 190 | @overload
|
186 |
| -def findall(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> list[Any]: ... |
187 |
| - |
188 |
| -# Return an iterator yielding match objects over all non-overlapping matches |
189 |
| -# for the RE pattern in string. The string is scanned left-to-right, and |
190 |
| -# matches are returned in the order found. Empty matches are included in the |
191 |
| -# result unless they touch the beginning of another match. |
192 |
| -@overload |
193 |
| -def finditer(pattern: AnyStr, string: AnyStr, flags: _FlagsType = ...) -> Iterator[Match[AnyStr]]: ... |
| 191 | +def findall(pattern: bytes | Pattern[bytes], string: ReadableBuffer, flags: _FlagsType = ...) -> list[Any]: ... |
194 | 192 | @overload
|
195 |
| -def finditer(pattern: Pattern[AnyStr], string: AnyStr, flags: _FlagsType = ...) -> Iterator[Match[AnyStr]]: ... |
| 193 | +def finditer(pattern: str | Pattern[str], string: str, flags: _FlagsType = ...) -> Iterator[Match[str]]: ... |
196 | 194 | @overload
|
197 |
| -def sub(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> AnyStr: ... |
| 195 | +def finditer(pattern: bytes | Pattern[bytes], string: ReadableBuffer, flags: _FlagsType = ...) -> Iterator[Match[bytes]]: ... |
198 | 196 | @overload
|
199 | 197 | def sub(
|
200 |
| - pattern: AnyStr, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ... |
201 |
| -) -> AnyStr: ... |
202 |
| -@overload |
203 |
| -def sub(pattern: Pattern[AnyStr], repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> AnyStr: ... |
| 198 | + pattern: str | Pattern[str], repl: str | Callable[[Match[str]], str], string: str, count: int = ..., flags: _FlagsType = ... |
| 199 | +) -> str: ... |
204 | 200 | @overload
|
205 | 201 | def sub(
|
206 |
| - pattern: Pattern[AnyStr], repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ... |
207 |
| -) -> AnyStr: ... |
208 |
| -@overload |
209 |
| -def subn(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ...) -> tuple[AnyStr, int]: ... |
210 |
| -@overload |
211 |
| -def subn( |
212 |
| - pattern: AnyStr, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ... |
213 |
| -) -> tuple[AnyStr, int]: ... |
| 202 | + pattern: bytes | Pattern[bytes], |
| 203 | + repl: ReadableBuffer | Callable[[Match[bytes]], ReadableBuffer], |
| 204 | + string: ReadableBuffer, |
| 205 | + count: int = ..., |
| 206 | + flags: _FlagsType = ..., |
| 207 | +) -> bytes: ... |
214 | 208 | @overload
|
215 | 209 | def subn(
|
216 |
| - pattern: Pattern[AnyStr], repl: AnyStr, string: AnyStr, count: int = ..., flags: _FlagsType = ... |
217 |
| -) -> tuple[AnyStr, int]: ... |
| 210 | + pattern: str | Pattern[str], repl: str | Callable[[Match[str]], str], string: str, count: int = ..., flags: _FlagsType = ... |
| 211 | +) -> tuple[str, int]: ... |
218 | 212 | @overload
|
219 | 213 | def subn(
|
220 |
| - pattern: Pattern[AnyStr], repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: _FlagsType = ... |
221 |
| -) -> tuple[AnyStr, int]: ... |
| 214 | + pattern: bytes | Pattern[bytes], |
| 215 | + repl: ReadableBuffer | Callable[[Match[bytes]], ReadableBuffer], |
| 216 | + string: ReadableBuffer, |
| 217 | + count: int = ..., |
| 218 | + flags: _FlagsType = ..., |
| 219 | +) -> tuple[bytes, int]: ... |
222 | 220 | def escape(pattern: AnyStr) -> AnyStr: ...
|
223 | 221 | def purge() -> None: ...
|
224 | 222 | def template(pattern: AnyStr | Pattern[AnyStr], flags: _FlagsType = ...) -> Pattern[AnyStr]: ...
|
0 commit comments