Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add regex stubs #6713

Merged
merged 18 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions stubs/regex/@tests/stubtest_allowlist.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# These classes are defined in regex/_regex.c and are returned by the public API functions.
# The stubs are defined in regex/_regex.pyi but these classes aren't present at runtime.
regex._regex.Match
regex._regex.Pattern
regex._regex.Scanner
regex._regex.Splitter
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions stubs/regex/METADATA.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = "2021.11.10"
1 change: 1 addition & 0 deletions stubs/regex/regex/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .regex import *
192 changes: 192 additions & 0 deletions stubs/regex/regex/_regex.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
from _typeshed import Self
from typing import Any, AnyStr, Callable, Generic, Mapping, TypeVar, overload
from typing_extensions import Literal, final

_T = TypeVar("_T")

@final
class Pattern(Generic[AnyStr]):
jpy-git marked this conversation as resolved.
Show resolved Hide resolved

pattern: AnyStr
flags: int
groups: int
groupindex: Mapping[str, int]
named_lists: Mapping[str, frozenset[AnyStr]]
def search(
self,
string: AnyStr,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> Match[AnyStr] | None: ...
def match(
self,
string: AnyStr,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> Match[AnyStr] | None: ...
def fullmatch(
self,
string: AnyStr,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> Match[AnyStr] | None: ...
def split(
self, string: AnyStr, maxsplit: int = ..., concurrent: bool | None = ..., timeout: float | None = ...
) -> list[AnyStr | Any]: ...
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
def splititer(
self, string: AnyStr, maxsplit: int = ..., concurrent: bool | None = ..., timeout: float | None = ...
) -> Splitter[AnyStr]: ...
def findall(
self,
string: AnyStr,
pos: int | None = ...,
endpos: int | None = ...,
overlapped: bool = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> list[Any]: ...
def finditer(
self,
string: AnyStr,
pos: int | None = ...,
endpos: int | None = ...,
overlapped: bool = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> Scanner[AnyStr]: ...
def sub(
self,
repl: AnyStr | Callable[[Match[AnyStr]], AnyStr],
string: AnyStr,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> AnyStr: ...
def subf(
self,
format: AnyStr | Callable[[Match[AnyStr]], AnyStr],
string: AnyStr,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> AnyStr: ...
def subn(
self,
repl: AnyStr | Callable[[Match[AnyStr]], AnyStr],
string: AnyStr,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> tuple[AnyStr, int]: ...
def subfn(
self,
format: AnyStr | Callable[[Match[AnyStr]], AnyStr],
string: AnyStr,
count: int = ...,
flags: int = ...,
pos: int | None = ...,
endpos: int | None = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> tuple[AnyStr, int]: ...
def scanner(
self,
string: AnyStr,
pos: int | None = ...,
endpos: int | None = ...,
overlapped: bool = ...,
concurrent: bool | None = ...,
timeout: float | None = ...,
) -> Scanner[AnyStr]: ...

@final
class Match(Generic[AnyStr]):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this missing __getitem__? After updating some modules I now get hundreds of "__getitem__" method not defined on type "Match[str]" errors.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not using regex and have not investigated, but this seems likely. re.Match (or rather typing.Match) also has a __getitem__() field. Cc @jpy-git

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, will make a PR 👍


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
@overload
def group(self, __group: Literal[0] = ...) -> AnyStr: ...
@overload
def group(self, __group: int | str = ...) -> AnyStr | Any: ...
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
@overload
def group(self, __group1: int | str, __group2: int | str, *groups: int | str) -> tuple[AnyStr | Any, ...]: ...
@overload
def groups(self, default: None = ...) -> tuple[AnyStr | Any, ...]: ...
@overload
def groups(self, default: _T) -> tuple[AnyStr | _T, ...]: ...
@overload
def groupdict(self, default: None = ...) -> dict[str, AnyStr | Any]: ...
@overload
def groupdict(self, default: _T) -> dict[str, AnyStr | _T]: ...
@overload
def span(self, __group: int | str = ...) -> tuple[int, int]: ...
@overload
def span(self, __group1: int | str, __group2: int | str, *groups: int | str) -> tuple[tuple[int, int], ...]: ...
@overload
def spans(self, __group: int | str = ...) -> list[tuple[int, int]]: ...
@overload
def spans(self, __group1: int | str, __group2: int | str, *groups: int | str) -> tuple[list[tuple[int, int]], ...]: ...
@overload
def start(self, __group: int | str = ...) -> int: ...
@overload
def start(self, __group1: int | str, __group2: int | str, *groups: int | str) -> tuple[int, ...]: ...
@overload
def starts(self, __group: int | str = ...) -> list[int]: ...
@overload
def starts(self, __group1: int | str, __group2: int | str, *groups: int | str) -> tuple[list[int], ...]: ...
@overload
def end(self, __group: int | str = ...) -> int: ...
@overload
def end(self, __group1: int | str, __group2: int | str, *groups: int | str) -> tuple[int, ...]: ...
@overload
def ends(self, __group: int | str = ...) -> list[int]: ...
@overload
def ends(self, __group1: int | str, __group2: int | str, *groups: int | str) -> tuple[list[int], ...]: ...
def expand(self, template: AnyStr) -> AnyStr: ...
def expandf(self, format: AnyStr) -> AnyStr: ...
@overload
def captures(self, __group: int | str = ...) -> list[AnyStr]: ...
@overload
def captures(self, __group1: int | str, __group2: int | str, *groups: int | str) -> tuple[list[AnyStr], ...]: ...
def capturesdict(self) -> dict[str, list[AnyStr]]: ...
def detach_string(self) -> None: ...

@final
class Splitter(Generic[AnyStr]):

pattern: 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]
def __iter__(self: Self) -> Self: ...
def __next__(self) -> Match[AnyStr]: ...
def match(self) -> Match[AnyStr] | None: ...
def search(self) -> Match[AnyStr] | None: ...
41 changes: 41 additions & 0 deletions stubs/regex/regex/_regex_core.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from typing import AnyStr

class error(Exception):
def __init__(self, message: str, pattern: AnyStr | None = ..., pos: int | None = ...) -> None: ...

A: int
ASCII: int
B: int
BESTMATCH: int
D: int
DEBUG: int
E: int
ENHANCEMATCH: int
F: int
FULLCASE: int
I: int
IGNORECASE: int
L: int
LOCALE: int
M: int
MULTILINE: int
P: int
POSIX: int
R: int
REVERSE: int
T: int
TEMPLATE: int
S: int
DOTALL: int
U: int
UNICODE: int
V0: int
VERSION0: int
V1: int
VERSION1: int
W: int
WORD: int
X: int
VERBOSE: int

DEFAULT_VERSION: int
Loading