Debian Codegraph @

jedi/third_party/typeshed/stdlib/re.pyi

raw symbols SCIP
1 import enum
2 import sre_compile
3 import sre_constants
4 import sys
5 from _typeshed import MaybeNone, ReadableBuffer
6 from collections.abc import Callable, Iterator, Mapping
7 from types import GenericAlias
8 from typing import Any, AnyStr, Final, Generic, Literal, TypeVar, final, overload
9 from typing_extensions import TypeAlias, deprecated
10
11 __all__ = [
12 "match",
13 "fullmatch",
14 "search",
15 "sub",
16 "subn",
17 "split",
18 "findall",
19 "finditer",
20 "compile",
21 "purge",
22 "escape",
23 "error",
24 "A",
25 "I",
26 "L",
27 "M",
28 "S",
29 "X",
30 "U",
31 "ASCII",
32 "IGNORECASE",
33 "LOCALE",
34 "MULTILINE",
35 "DOTALL",
36 "VERBOSE",
37 "UNICODE",
38 "Match",
39 "Pattern",
40 ]
41 if sys.version_info < (3, 13):
42 __all__ += ["template"]
43
44 if sys.version_info >= (3, 11):
45 __all__ += ["NOFLAG", "RegexFlag"]
46
47 if sys.version_info >= (3, 13):
48 __all__ += ["PatternError"]
49
50 PatternError = sre_constants.error
51
52 _T = TypeVar("_T")
53
54 # The implementation defines this in re._constants (version_info >= 3, 11) or
55 # sre_constants. Typeshed has it here because its __module__ attribute is set to "re".
56 class error(Exception):
57 msg: str
58 pattern: str | bytes | None
59 pos: int | None
60 lineno: int
61 colno: int
62 def __init__(self, msg: str, pattern: str | bytes | None = None, pos: int | None = None) -> None: ...
63
64 @final
65 class Match(Generic[AnyStr]):
66 @property
67 def pos(self) -> int: ...
68 @property
69 def endpos(self) -> int: ...
70 @property
71 def lastindex(self) -> int | None: ...
72 @property
73 def lastgroup(self) -> str | None: ...
74 @property
75 def string(self) -> AnyStr: ...
76
77 # The regular expression object whose match() or search() method produced
78 # this match instance.
79 @property
80 def re(self) -> Pattern[AnyStr]: ...
81 @overload
82 def expand(self: Match[str], template: str) -> str: ...
83 @overload
84 def expand(self: Match[bytes], template: ReadableBuffer) -> bytes: ...
85 @overload
86 def expand(self, template: AnyStr) -> AnyStr: ...
87 # group() returns "AnyStr" or "AnyStr | None", depending on the pattern.
88 @overload
89 def group(self, group: Literal[0] = 0, /) -> AnyStr: ...
90 @overload
91 def group(self, group: str | int, /) -> AnyStr | MaybeNone: ...
92 @overload
93 def group(self, group1: str | int, group2: str | int, /, *groups: str | int) -> tuple[AnyStr | MaybeNone, ...]: ...
94 # Each item of groups()'s return tuple is either "AnyStr" or
95 # "AnyStr | None", depending on the pattern.
96 @overload
97 def groups(self) -> tuple[AnyStr | MaybeNone, ...]: ...
98 @overload
99 def groups(self, default: _T) -> tuple[AnyStr | _T, ...]: ...
100 # Each value in groupdict()'s return dict is either "AnyStr" or
101 # "AnyStr | None", depending on the pattern.
102 @overload
103 def groupdict(self) -> dict[str, AnyStr | MaybeNone]: ...
104 @overload
105 def groupdict(self, default: _T) -> dict[str, AnyStr | _T]: ...
106 def start(self, group: int | str = 0, /) -> int: ...
107 def end(self, group: int | str = 0, /) -> int: ...
108 def span(self, group: int | str = 0, /) -> tuple[int, int]: ...
109 @property
110 def regs(self) -> tuple[tuple[int, int], ...]: ... # undocumented
111 # __getitem__() returns "AnyStr" or "AnyStr | None", depending on the pattern.
112 @overload
113 def __getitem__(self, key: Literal[0], /) -> AnyStr: ...
114 @overload
115 def __getitem__(self, key: int | str, /) -> AnyStr | MaybeNone: ...
116 def __copy__(self) -> Match[AnyStr]: ...
117 def __deepcopy__(self, memo: Any, /) -> Match[AnyStr]: ...
118 def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
119
120 @final
121 class Pattern(Generic[AnyStr]):
122 @property
123 def flags(self) -> int: ...
124 @property
125 def groupindex(self) -> Mapping[str, int]: ...
126 @property
127 def groups(self) -> int: ...
128 @property
129 def pattern(self) -> AnyStr: ...
130 @overload
131 def search(self: Pattern[str], string: str, pos: int = 0, endpos: int = sys.maxsize) -> Match[str] | None: ...
132 @overload
133 def search(self: Pattern[bytes], string: ReadableBuffer, pos: int = 0, endpos: int = sys.maxsize) -> Match[bytes] | None: ...
134 @overload
135 def search(self, string: AnyStr, pos: int = 0, endpos: int = sys.maxsize) -> Match[AnyStr] | None: ...
136 @overload
137 def match(self: Pattern[str], string: str, pos: int = 0, endpos: int = sys.maxsize) -> Match[str] | None: ...
138 @overload
139 def match(self: Pattern[bytes], string: ReadableBuffer, pos: int = 0, endpos: int = sys.maxsize) -> Match[bytes] | None: ...
140 @overload
141 def match(self, string: AnyStr, pos: int = 0, endpos: int = sys.maxsize) -> Match[AnyStr] | None: ...
142 @overload
143 def fullmatch(self: Pattern[str], string: str, pos: int = 0, endpos: int = sys.maxsize) -> Match[str] | None: ...
144 @overload
145 def fullmatch(
146 self: Pattern[bytes], string: ReadableBuffer, pos: int = 0, endpos: int = sys.maxsize
147 ) -> Match[bytes] | None: ...
148 @overload
149 def fullmatch(self, string: AnyStr, pos: int = 0, endpos: int = sys.maxsize) -> Match[AnyStr] | None: ...
150 @overload
151 def split(self: Pattern[str], string: str, maxsplit: int = 0) -> list[str | MaybeNone]: ...
152 @overload
153 def split(self: Pattern[bytes], string: ReadableBuffer, maxsplit: int = 0) -> list[bytes | MaybeNone]: ...
154 @overload
155 def split(self, string: AnyStr, maxsplit: int = 0) -> list[AnyStr | MaybeNone]: ...
156 # return type depends on the number of groups in the pattern
157 @overload
158 def findall(self: Pattern[str], string: str, pos: int = 0, endpos: int = sys.maxsize) -> list[Any]: ...
159 @overload
160 def findall(self: Pattern[bytes], string: ReadableBuffer, pos: int = 0, endpos: int = sys.maxsize) -> list[Any]: ...
161 @overload
162 def findall(self, string: AnyStr, pos: int = 0, endpos: int = sys.maxsize) -> list[AnyStr]: ...
163 @overload
164 def finditer(self: Pattern[str], string: str, pos: int = 0, endpos: int = sys.maxsize) -> Iterator[Match[str]]: ...
165 @overload
166 def finditer(
167 self: Pattern[bytes], string: ReadableBuffer, pos: int = 0, endpos: int = sys.maxsize
168 ) -> Iterator[Match[bytes]]: ...
169 @overload
170 def finditer(self, string: AnyStr, pos: int = 0, endpos: int = sys.maxsize) -> Iterator[Match[AnyStr]]: ...
171 @overload
172 def sub(self: Pattern[str], repl: str | Callable[[Match[str]], str], string: str, count: int = 0) -> str: ...
173 @overload
174 def sub(
175 self: Pattern[bytes],
176 repl: ReadableBuffer | Callable[[Match[bytes]], ReadableBuffer],
177 string: ReadableBuffer,
178 count: int = 0,
179 ) -> bytes: ...
180 @overload
181 def sub(self, repl: AnyStr | Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = 0) -> AnyStr: ...
182 @overload
183 def subn(self: Pattern[str], repl: str | Callable[[Match[str]], str], string: str, count: int = 0) -> tuple[str, int]: ...
184 @overload
185 def subn(
186 self: Pattern[bytes],
187 repl: ReadableBuffer | Callable[[Match[bytes]], ReadableBuffer],
188 string: ReadableBuffer,
189 count: int = 0,
190 ) -> tuple[bytes, int]: ...
191 @overload
192 def subn(self, repl: AnyStr | Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = 0) -> tuple[AnyStr, int]: ...
193 def __copy__(self) -> Pattern[AnyStr]: ...
194 def __deepcopy__(self, memo: Any, /) -> Pattern[AnyStr]: ...
195 def __eq__(self, value: object, /) -> bool: ...
196 def __hash__(self) -> int: ...
197 def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
198
199 # ----- re variables and constants -----
200
201 class RegexFlag(enum.IntFlag):
202 A = sre_compile.SRE_FLAG_ASCII
203 ASCII = A
204 DEBUG = sre_compile.SRE_FLAG_DEBUG
205 I = sre_compile.SRE_FLAG_IGNORECASE
206 IGNORECASE = I
207 L = sre_compile.SRE_FLAG_LOCALE
208 LOCALE = L
209 M = sre_compile.SRE_FLAG_MULTILINE
210 MULTILINE = M
211 S = sre_compile.SRE_FLAG_DOTALL
212 DOTALL = S
213 X = sre_compile.SRE_FLAG_VERBOSE
214 VERBOSE = X
215 U = sre_compile.SRE_FLAG_UNICODE
216 UNICODE = U
217 if sys.version_info < (3, 13):
218 T = sre_compile.SRE_FLAG_TEMPLATE
219 TEMPLATE = T
220 if sys.version_info >= (3, 11):
221 NOFLAG = 0
222
223 A: Final = RegexFlag.A
224 ASCII: Final = RegexFlag.ASCII
225 DEBUG: Final = RegexFlag.DEBUG
226 I: Final = RegexFlag.I
227 IGNORECASE: Final = RegexFlag.IGNORECASE
228 L: Final = RegexFlag.L
229 LOCALE: Final = RegexFlag.LOCALE
230 M: Final = RegexFlag.M
231 MULTILINE: Final = RegexFlag.MULTILINE
232 S: Final = RegexFlag.S
233 DOTALL: Final = RegexFlag.DOTALL
234 X: Final = RegexFlag.X
235 VERBOSE: Final = RegexFlag.VERBOSE
236 U: Final = RegexFlag.U
237 UNICODE: Final = RegexFlag.UNICODE
238 if sys.version_info < (3, 13):
239 T: Final = RegexFlag.T
240 TEMPLATE: Final = RegexFlag.TEMPLATE
241 if sys.version_info >= (3, 11):
242 NOFLAG: Final = RegexFlag.NOFLAG
243 _FlagsType: TypeAlias = int | RegexFlag
244
245 # Type-wise the compile() overloads are unnecessary, they could also be modeled using
246 # unions in the parameter types. However mypy has a bug regarding TypeVar
247 # constraints (https://github.com/python/mypy/issues/11880),
248 # which limits us here because AnyStr is a constrained TypeVar.
249
250 # pattern arguments do *not* accept arbitrary buffers such as bytearray,
251 # because the pattern must be hashable.
252 @overload
253 def compile(pattern: AnyStr, flags: _FlagsType = 0) -> Pattern[AnyStr]: ...
254 @overload
255 def compile(pattern: Pattern[AnyStr], flags: _FlagsType = 0) -> Pattern[AnyStr]: ...
256 @overload
257 def search(pattern: str | Pattern[str], string: str, flags: _FlagsType = 0) -> Match[str] | None: ...
258 @overload
259 def search(pattern: bytes | Pattern[bytes], string: ReadableBuffer, flags: _FlagsType = 0) -> Match[bytes] | None: ...
260 @overload
261 def match(pattern: str | Pattern[str], string: str, flags: _FlagsType = 0) -> Match[str] | None: ...
262 @overload
263 def match(pattern: bytes | Pattern[bytes], string: ReadableBuffer, flags: _FlagsType = 0) -> Match[bytes] | None: ...
264 @overload
265 def fullmatch(pattern: str | Pattern[str], string: str, flags: _FlagsType = 0) -> Match[str] | None: ...
266 @overload
267 def fullmatch(pattern: bytes | Pattern[bytes], string: ReadableBuffer, flags: _FlagsType = 0) -> Match[bytes] | None: ...
268 @overload
269 def split(pattern: str | Pattern[str], string: str, maxsplit: int = 0, flags: _FlagsType = 0) -> list[str | MaybeNone]: ...
270 @overload
271 def split(
272 pattern: bytes | Pattern[bytes], string: ReadableBuffer, maxsplit: int = 0, flags: _FlagsType = 0
273 ) -> list[bytes | MaybeNone]: ...
274 @overload
275 def findall(pattern: str | Pattern[str], string: str, flags: _FlagsType = 0) -> list[Any]: ...
276 @overload
277 def findall(pattern: bytes | Pattern[bytes], string: ReadableBuffer, flags: _FlagsType = 0) -> list[Any]: ...
278 @overload
279 def finditer(pattern: str | Pattern[str], string: str, flags: _FlagsType = 0) -> Iterator[Match[str]]: ...
280 @overload
281 def finditer(pattern: bytes | Pattern[bytes], string: ReadableBuffer, flags: _FlagsType = 0) -> Iterator[Match[bytes]]: ...
282 @overload
283 def sub(
284 pattern: str | Pattern[str], repl: str | Callable[[Match[str]], str], string: str, count: int = 0, flags: _FlagsType = 0
285 ) -> str: ...
286 @overload
287 def sub(
288 pattern: bytes | Pattern[bytes],
289 repl: ReadableBuffer | Callable[[Match[bytes]], ReadableBuffer],
290 string: ReadableBuffer,
291 count: int = 0,
292 flags: _FlagsType = 0,
293 ) -> bytes: ...
294 @overload
295 def subn(
296 pattern: str | Pattern[str], repl: str | Callable[[Match[str]], str], string: str, count: int = 0, flags: _FlagsType = 0
297 ) -> tuple[str, int]: ...
298 @overload
299 def subn(
300 pattern: bytes | Pattern[bytes],
301 repl: ReadableBuffer | Callable[[Match[bytes]], ReadableBuffer],
302 string: ReadableBuffer,
303 count: int = 0,
304 flags: _FlagsType = 0,
305 ) -> tuple[bytes, int]: ...
306 def escape(pattern: AnyStr) -> AnyStr: ...
307 def purge() -> None: ...
308
309 if sys.version_info < (3, 13):
310 if sys.version_info >= (3, 11):
311 @deprecated("Deprecated since Python 3.11; removed in Python 3.13. Use `re.compile()` instead.")
312 def template(pattern: AnyStr | Pattern[AnyStr], flags: _FlagsType = 0) -> Pattern[AnyStr]: ... # undocumented
313 else:
314 def template(pattern: AnyStr | Pattern[AnyStr], flags: _FlagsType = 0) -> Pattern[AnyStr]: ... # undocumented