regex: accept buffers (#7680)

Similar to #7679

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Akuli <akuviljanen17@gmail.com>
This commit is contained in:
Jelle Zijlstra
2022-05-10 15:08:36 -07:00
committed by GitHub
parent 68a781d04e
commit b00b4f3447
2 changed files with 407 additions and 104 deletions

View File

@@ -1,4 +1,4 @@
from _typeshed import Self
from _typeshed import ReadableBuffer, Self
from collections.abc import Callable, Mapping
from typing import Any, AnyStr, Generic, TypeVar, overload
from typing_extensions import Literal, final
@@ -7,126 +7,273 @@ _T = TypeVar("_T")
@final
class Pattern(Generic[AnyStr]):
pattern: AnyStr
flags: int
groups: int
groupindex: Mapping[str, int]
named_lists: Mapping[str, frozenset[AnyStr]]
@property
def flags(self) -> int: ...
@property
def groupindex(self) -> Mapping[str, int]: ...
@property
def groups(self) -> int: ...
@property
def pattern(self) -> AnyStr: ...
@property
def named_lists(self) -> Mapping[str, frozenset[AnyStr]]: ...
@overload
def search(
self,
string: AnyStr,
pos: int | None = ...,
endpos: int | None = ...,
self: Pattern[str],
string: str,
pos: int = ...,
endpos: int = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> Match[AnyStr] | None: ...
) -> Match[str] | None: ...
@overload
def search(
self: Pattern[bytes],
string: ReadableBuffer,
pos: int = ...,
endpos: int = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> Match[bytes] | None: ...
@overload
def match(
self,
string: AnyStr,
pos: int | None = ...,
endpos: int | None = ...,
self: Pattern[str],
string: str,
pos: int = ...,
endpos: int = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> Match[AnyStr] | None: ...
) -> Match[str] | None: ...
@overload
def match(
self: Pattern[bytes],
string: ReadableBuffer,
pos: int = ...,
endpos: int = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> Match[bytes] | None: ...
@overload
def fullmatch(
self,
string: AnyStr,
pos: int | None = ...,
endpos: int | None = ...,
self: Pattern[str],
string: str,
pos: int = ...,
endpos: int = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> Match[AnyStr] | None: ...
) -> Match[str] | None: ...
@overload
def fullmatch(
self: Pattern[bytes],
string: ReadableBuffer,
pos: int = ...,
endpos: int = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> Match[bytes] | None: ...
@overload
def split(
self, string: AnyStr, maxsplit: int = ..., concurrent: bool | None = ..., timeout: float | None = ...
) -> list[AnyStr | Any]: ...
self: Pattern[str], string: str, maxsplit: int = ..., concurrent: bool | None = ..., timeout: float | None = ...
) -> list[str | Any]: ...
@overload
def split(
self: Pattern[bytes],
string: ReadableBuffer,
maxsplit: int = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> list[bytes | Any]: ...
@overload
def splititer(
self, string: AnyStr, maxsplit: int = ..., concurrent: bool | None = ..., timeout: float | None = ...
) -> Splitter[AnyStr]: ...
self: Pattern[str], string: str, maxsplit: int = ..., concurrent: bool | None = ..., timeout: float | None = ...
) -> Splitter[str]: ...
@overload
def splititer(
self: Pattern[bytes],
string: ReadableBuffer,
maxsplit: int = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> Splitter[bytes]: ...
@overload
def findall(
self,
string: AnyStr,
pos: int | None = ...,
endpos: int | None = ...,
self: Pattern[str],
string: str,
pos: int = ...,
endpos: int = ...,
overlapped: bool = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> list[Any]: ...
@overload
def findall(
self: Pattern[bytes],
string: ReadableBuffer,
pos: int = ...,
endpos: int = ...,
overlapped: bool = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> list[Any]: ...
@overload
def finditer(
self,
string: AnyStr,
pos: int | None = ...,
endpos: int | None = ...,
self: Pattern[str],
string: str,
pos: int = ...,
endpos: int = ...,
overlapped: bool = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> Scanner[AnyStr]: ...
) -> Scanner[str]: ...
@overload
def finditer(
self: Pattern[bytes],
string: ReadableBuffer,
pos: int = ...,
endpos: int = ...,
overlapped: bool = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> Scanner[bytes]: ...
@overload
def sub(
self,
repl: AnyStr | Callable[[Match[AnyStr]], AnyStr],
string: AnyStr,
self: Pattern[str],
repl: str | Callable[[Match[str]], str],
string: str,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> AnyStr: ...
) -> str: ...
@overload
def sub(
self: Pattern[bytes],
repl: ReadableBuffer | Callable[[Match[bytes]], ReadableBuffer],
string: ReadableBuffer,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> bytes: ...
@overload
def subf(
self,
format: AnyStr | Callable[[Match[AnyStr]], AnyStr],
string: AnyStr,
self: Pattern[str],
format: str | Callable[[Match[str]], str],
string: str,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> AnyStr: ...
) -> str: ...
@overload
def subf(
self: Pattern[bytes],
format: ReadableBuffer | Callable[[Match[bytes]], ReadableBuffer],
string: ReadableBuffer,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> bytes: ...
@overload
def subn(
self,
repl: AnyStr | Callable[[Match[AnyStr]], AnyStr],
string: AnyStr,
self: Pattern[str],
repl: str | Callable[[Match[str]], str],
string: str,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> tuple[AnyStr, int]: ...
) -> tuple[str, int]: ...
@overload
def subn(
self: Pattern[bytes],
repl: ReadableBuffer | Callable[[Match[bytes]], ReadableBuffer],
string: ReadableBuffer,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> tuple[bytes, int]: ...
@overload
def subfn(
self,
format: AnyStr | Callable[[Match[AnyStr]], AnyStr],
string: AnyStr,
self: Pattern[str],
format: str | Callable[[Match[str]], str],
string: str,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> tuple[AnyStr, int]: ...
) -> tuple[str, int]: ...
@overload
def subfn(
self: Pattern[bytes],
format: ReadableBuffer | Callable[[Match[bytes]], ReadableBuffer],
string: ReadableBuffer,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> tuple[bytes, int]: ...
@overload
def scanner(
self,
string: AnyStr,
self: Pattern[str],
string: str,
pos: int | None = ...,
endpos: int | None = ...,
overlapped: bool = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> Scanner[AnyStr]: ...
) -> Scanner[str]: ...
@overload
def scanner(
self: Pattern[bytes],
string: bytes,
pos: int | None = ...,
endpos: int | None = ...,
overlapped: bool = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> Scanner[bytes]: ...
@final
class Match(Generic[AnyStr]):
re: Pattern[AnyStr]
string: AnyStr
pos: int
endpos: int
partial: bool
regs: tuple[tuple[int, int], ...]
fuzzy_counts: tuple[int, int, int]
fuzzy_changes: tuple[list[int], list[int], list[int]]
lastgroup: str | None
lastindex: int | None
@property
def pos(self) -> int: ...
@property
def endpos(self) -> int: ...
@property
def lastindex(self) -> int | None: ...
@property
def lastgroup(self) -> str | None: ...
@property
def string(self) -> AnyStr: ...
@property
def re(self) -> Pattern[AnyStr]: ...
@property
def partial(self) -> bool: ...
@property
def regs(self) -> tuple[tuple[int, int], ...]: ...
@property
def fuzzy_counts(self) -> tuple[int, int, int]: ...
@property
def fuzzy_changes(self) -> tuple[list[int], list[int], list[int]]: ...
@overload
def group(self, __group: Literal[0] = ...) -> AnyStr: ...
@overload
@@ -180,16 +327,16 @@ class Match(Generic[AnyStr]):
@final
class Splitter(Generic[AnyStr]):
pattern: Pattern[AnyStr]
@property
def pattern(self) -> Pattern[AnyStr]: ...
def __iter__(self: Self) -> Self: ...
def __next__(self) -> AnyStr | Any: ...
def split(self) -> AnyStr | Any: ...
@final
class Scanner(Generic[AnyStr]):
pattern: Pattern[AnyStr]
@property
def pattern(self) -> Pattern[AnyStr]: ...
def __iter__(self: Self) -> Self: ...
def __next__(self) -> Match[AnyStr]: ...
def match(self) -> Match[AnyStr] | None: ...

View File

@@ -1,3 +1,4 @@
from _typeshed import ReadableBuffer
from collections.abc import Callable
from typing import Any, AnyStr, overload
@@ -9,9 +10,10 @@ __version__: str
def compile(
pattern: AnyStr | _regex.Pattern[AnyStr], flags: int = ..., ignore_unused: bool = ..., **kwargs: Any
) -> _regex.Pattern[AnyStr]: ...
@overload
def search(
pattern: AnyStr | _regex.Pattern[AnyStr],
string: AnyStr,
pattern: str | Pattern[str],
string: str,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
@@ -20,10 +22,24 @@ def search(
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> _regex.Match[AnyStr] | None: ...
) -> _regex.Match[str] | None: ...
@overload
def search(
pattern: bytes | Pattern[bytes],
string: ReadableBuffer,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
partial: bool = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> _regex.Match[bytes] | None: ...
@overload
def match(
pattern: AnyStr | _regex.Pattern[AnyStr],
string: AnyStr,
pattern: str | Pattern[str],
string: str,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
@@ -32,10 +48,24 @@ def match(
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> _regex.Match[AnyStr] | None: ...
) -> _regex.Match[str] | None: ...
@overload
def match(
pattern: bytes | Pattern[bytes],
string: ReadableBuffer,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
partial: bool = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> _regex.Match[bytes] | None: ...
@overload
def fullmatch(
pattern: AnyStr | _regex.Pattern[AnyStr],
string: AnyStr,
pattern: str | Pattern[str],
string: str,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
@@ -44,30 +74,68 @@ def fullmatch(
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> _regex.Match[AnyStr] | None: ...
) -> _regex.Match[str] | None: ...
@overload
def fullmatch(
pattern: bytes | Pattern[bytes],
string: ReadableBuffer,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
partial: bool = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> _regex.Match[bytes] | None: ...
@overload
def split(
pattern: AnyStr | _regex.Pattern[AnyStr],
string: AnyStr,
pattern: str | _regex.Pattern[str],
string: str,
maxsplit: int = ...,
flags: int = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> list[AnyStr | Any]: ...
) -> list[str | Any]: ...
@overload
def split(
pattern: ReadableBuffer | _regex.Pattern[bytes],
string: ReadableBuffer,
maxsplit: int = ...,
flags: int = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> list[bytes | Any]: ...
@overload
def splititer(
pattern: AnyStr | _regex.Pattern[AnyStr],
string: AnyStr,
pattern: str | _regex.Pattern[str],
string: str,
maxsplit: int = ...,
flags: int = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> _regex.Splitter[AnyStr]: ...
) -> _regex.Splitter[str]: ...
@overload
def splititer(
pattern: ReadableBuffer | _regex.Pattern[bytes],
string: ReadableBuffer,
maxsplit: int = ...,
flags: int = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> _regex.Splitter[bytes]: ...
@overload
def findall(
pattern: AnyStr | _regex.Pattern[AnyStr],
string: AnyStr,
pattern: str | _regex.Pattern[str],
string: str,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
@@ -77,9 +145,23 @@ def findall(
ignore_unused: bool = ...,
**kwargs: Any,
) -> list[Any]: ...
@overload
def findall(
pattern: ReadableBuffer | _regex.Pattern[bytes],
string: ReadableBuffer,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
overlapped: bool = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> list[Any]: ...
@overload
def finditer(
pattern: AnyStr | _regex.Pattern[AnyStr],
string: AnyStr,
pattern: str | _regex.Pattern[str],
string: str,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
@@ -89,11 +171,26 @@ def finditer(
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> _regex.Scanner[AnyStr]: ...
) -> _regex.Scanner[str]: ...
@overload
def finditer(
pattern: ReadableBuffer | _regex.Pattern[bytes],
string: ReadableBuffer,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
overlapped: bool = ...,
partial: bool = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> _regex.Scanner[bytes]: ...
@overload
def sub(
pattern: AnyStr | _regex.Pattern[AnyStr],
repl: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr],
string: AnyStr,
pattern: str | _regex.Pattern[str],
repl: str | Callable[[_regex.Match[str]], str],
string: str,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
@@ -102,11 +199,26 @@ def sub(
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> AnyStr: ...
) -> str: ...
@overload
def sub(
pattern: ReadableBuffer | _regex.Pattern[bytes],
repl: ReadableBuffer | Callable[[_regex.Match[bytes]], ReadableBuffer],
string: ReadableBuffer,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> bytes: ...
@overload
def subf(
pattern: AnyStr | _regex.Pattern[AnyStr],
format: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr],
string: AnyStr,
pattern: str | _regex.Pattern[str],
format: str | Callable[[_regex.Match[str]], str],
string: str,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
@@ -115,11 +227,26 @@ def subf(
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> AnyStr: ...
) -> str: ...
@overload
def subf(
pattern: ReadableBuffer | _regex.Pattern[bytes],
format: ReadableBuffer | Callable[[_regex.Match[bytes]], ReadableBuffer],
string: ReadableBuffer,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> bytes: ...
@overload
def subn(
pattern: AnyStr | _regex.Pattern[AnyStr],
repl: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr],
string: AnyStr,
pattern: str | _regex.Pattern[str],
repl: str | Callable[[_regex.Match[str]], str],
string: str,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
@@ -128,11 +255,26 @@ def subn(
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> tuple[AnyStr, int]: ...
) -> tuple[str, int]: ...
@overload
def subn(
pattern: ReadableBuffer | _regex.Pattern[bytes],
repl: ReadableBuffer | Callable[[_regex.Match[bytes]], ReadableBuffer],
string: ReadableBuffer,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> tuple[bytes, int]: ...
@overload
def subfn(
pattern: AnyStr | _regex.Pattern[AnyStr],
format: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr],
string: AnyStr,
pattern: str | _regex.Pattern[str],
format: str | Callable[[_regex.Match[str]], str],
string: str,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
@@ -141,7 +283,21 @@ def subfn(
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> tuple[AnyStr, int]: ...
) -> tuple[str, int]: ...
@overload
def subfn(
pattern: ReadableBuffer | _regex.Pattern[bytes],
format: ReadableBuffer | Callable[[_regex.Match[bytes]], ReadableBuffer],
string: ReadableBuffer,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
ignore_unused: bool = ...,
**kwargs: Any,
) -> tuple[bytes, int]: ...
def purge() -> None: ...
@overload
def cache_all(value: bool = ...) -> None: ...