From 9207bcb2de6d51be0864e09b0160abbb5d381e73 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Mon, 27 Dec 2021 16:32:44 +0000 Subject: [PATCH 01/18] Add initial regex stubs --- stubs/regex/METADATA.toml | 1 + stubs/regex/regex/__init__.pyi | 61 ++++++ stubs/regex/regex/_regex.pyi | 13 ++ stubs/regex/regex/regex.pyi | 353 +++++++++++++++++++++++++++++++++ 4 files changed, 428 insertions(+) create mode 100644 stubs/regex/METADATA.toml create mode 100644 stubs/regex/regex/__init__.pyi create mode 100644 stubs/regex/regex/_regex.pyi create mode 100644 stubs/regex/regex/regex.pyi diff --git a/stubs/regex/METADATA.toml b/stubs/regex/METADATA.toml new file mode 100644 index 000000000000..1ebc1e5e1410 --- /dev/null +++ b/stubs/regex/METADATA.toml @@ -0,0 +1 @@ +version = "2021.11.10" diff --git a/stubs/regex/regex/__init__.pyi b/stubs/regex/regex/__init__.pyi new file mode 100644 index 000000000000..b47bdfb5d0fd --- /dev/null +++ b/stubs/regex/regex/__init__.pyi @@ -0,0 +1,61 @@ +from .regex import * + +# Names in __all__ with no definition: +# A +# ASCII +# B +# BESTMATCH +# D +# DEBUG +# DEFAULT_VERSION +# DOTALL +# E +# ENHANCEMATCH +# F +# FULLCASE +# I +# IGNORECASE +# L +# LOCALE +# M +# MULTILINE +# Match +# P +# POSIX +# Pattern +# R +# REVERSE +# Regex +# S +# Scanner +# T +# TEMPLATE +# U +# UNICODE +# V0 +# V1 +# VERBOSE +# VERSION0 +# VERSION1 +# W +# WORD +# X +# __doc__ +# __version__ +# cache_all +# compile +# error +# escape +# findall +# finditer +# fullmatch +# match +# purge +# search +# split +# splititer +# sub +# subf +# subfn +# subn +# template diff --git a/stubs/regex/regex/_regex.pyi b/stubs/regex/regex/_regex.pyi new file mode 100644 index 000000000000..f2b5d25a8e23 --- /dev/null +++ b/stubs/regex/regex/_regex.pyi @@ -0,0 +1,13 @@ +from typing import Any + +class Pattern: + def __getattr__(self, name: str) -> Any: ... # incomplete + +class Match: + def __getattr__(self, name: str) -> Any: ... # incomplete + +class Scanner: + def __getattr__(self, name: str) -> Any: ... # incomplete + +class Splitter: + def __getattr__(self, name: str) -> Any: ... # incomplete diff --git a/stubs/regex/regex/regex.pyi b/stubs/regex/regex/regex.pyi new file mode 100644 index 000000000000..0e87aae5d66c --- /dev/null +++ b/stubs/regex/regex/regex.pyi @@ -0,0 +1,353 @@ +from typing import Any, AnyStr, Callable, NoReturn, Tuple, _Alias, overload + +import regex._regex as _regex + +__version__: str + +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 + +@overload +def compile(pattern: AnyStr, flags: int = ..., ignore_unused: bool = ..., **kwargs: Any) -> _regex.Pattern: ... +@overload +def compile(pattern: _regex.Pattern, flags: int = ..., ignore_unused: bool = ..., **kwargs: Any) -> _regex.Pattern: ... +@overload +def search( + pattern: AnyStr, + string: AnyStr, + flags: int = ..., + pos: int | None = ..., + endpos: int | None = ..., + partial: bool = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> _regex.Match | None: ... +@overload +def search( + pattern: _regex.Pattern, + string: AnyStr, + flags: int = ..., + pos: int | None = ..., + endpos: int | None = ..., + partial: bool = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> _regex.Match | None: ... +@overload +def match( + pattern: AnyStr, + string: AnyStr, + flags: int = ..., + pos: int | None = ..., + endpos: int | None = ..., + partial: bool = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> _regex.Match: ... +@overload +def match( + pattern: _regex.Pattern, + string: AnyStr, + flags: int = ..., + pos: int | None = ..., + endpos: int | None = ..., + partial: bool = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> _regex.Match: ... +@overload +def fullmatch( + pattern: AnyStr, + string: AnyStr, + flags: int = ..., + pos: int | None = ..., + endpos: int | None = ..., + partial: bool = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> _regex.Match: ... +@overload +def fullmatch( + pattern: _regex.Pattern, + string: AnyStr, + flags: int = ..., + pos: int | None = ..., + endpos: int | None = ..., + partial: bool = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> _regex.Match: ... +@overload +def split( + pattern: AnyStr, + string: AnyStr, + maxsplit: int = ..., + flags: int = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> list[AnyStr]: ... +@overload +def split( + pattern: _regex.Pattern, + string: AnyStr, + maxsplit: int = ..., + flags: int = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> list[AnyStr]: ... +@overload +def splititer( + pattern: AnyStr, + string: AnyStr, + maxsplit: int = ..., + flags: int = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> _regex.Splitter: ... +@overload +def splititer( + pattern: _regex.Pattern, + string: AnyStr, + maxsplit: int = ..., + flags: int = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> _regex.Splitter: ... +@overload +def findall( + pattern: AnyStr, + string: AnyStr, + flags: int = ..., + pos: int | None = ..., + endpos: int | None = ..., + overlapped: bool = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> list[str] | list[Tuple[str, ...]]: ... +@overload +def findall( + pattern: _regex.Pattern, + string: AnyStr, + flags: int = ..., + pos: int | None = ..., + endpos: int | None = ..., + overlapped: bool = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> list[str] | list[Tuple[str, ...]]: ... +@overload +def finditer( + pattern: AnyStr, + string: AnyStr, + flags: int = ..., + pos: int | None = ..., + endpos: int | None = ..., + overlapped: bool = ..., + partial: bool = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> _regex.Scanner: ... +@overload +def finditer( + pattern: _regex.Pattern, + string: AnyStr, + flags: int = ..., + pos: int | None = ..., + endpos: int | None = ..., + overlapped: bool = ..., + partial: bool = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> _regex.Scanner: ... +def purge() -> None: ... +@overload +def cache_all(value: bool = ...) -> NoReturn: ... +@overload +def cache_all(value: None = ...) -> bool: ... +def escape(pattern: AnyStr, special_only: bool = ..., literal_spaces: bool = ...) -> AnyStr: ... +@overload +def template(pattern: AnyStr, flags: int = ...) -> _regex.Pattern: ... +@overload +def template(pattern: _regex.Pattern, flags: int = ...) -> _regex.Pattern: ... +@overload +def sub( + pattern: AnyStr, + repl: AnyStr | Callable[[_regex.Match], AnyStr], + string: AnyStr, + count: int = ..., + flags: int = ..., + pos: int | None = ..., + endpos: int | None = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> AnyStr: ... +@overload +def sub( + pattern: _regex.Pattern, + repl: AnyStr | Callable[[_regex.Match], AnyStr], + string: AnyStr, + count: int = ..., + flags: int = ..., + pos: int | None = ..., + endpos: int | None = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> AnyStr: ... +@overload +def subf( + pattern: AnyStr, + format: AnyStr | Callable[[_regex.Match], AnyStr], + string: AnyStr, + count: int = ..., + flags: int = ..., + pos: int | None = ..., + endpos: int | None = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> AnyStr: ... +@overload +def subf( + pattern: _regex.Pattern, + format: AnyStr | Callable[[_regex.Match], AnyStr], + string: AnyStr, + count: int = ..., + flags: int = ..., + pos: int | None = ..., + endpos: int | None = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> AnyStr: ... +@overload +def subn( + pattern: AnyStr, + repl: AnyStr | Callable[[_regex.Match], AnyStr], + string: AnyStr, + count: int = ..., + flags: int = ..., + pos: int | None = ..., + endpos: int | None = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> AnyStr: ... +@overload +def subn( + pattern: _regex.Pattern, + repl: AnyStr | Callable[[_regex.Match], AnyStr], + string: AnyStr, + count: int = ..., + flags: int = ..., + pos: int | None = ..., + endpos: int | None = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> AnyStr: ... +@overload +def subfn( + pattern: AnyStr, + format: AnyStr | Callable[[_regex.Match], AnyStr], + string: AnyStr, + count: int = ..., + flags: int = ..., + pos: int | None = ..., + endpos: int | None = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> tuple[AnyStr, int]: ... +@overload +def subfn( + pattern: _regex.Pattern, + format: AnyStr | Callable[[_regex.Match], AnyStr], + string: AnyStr, + count: int = ..., + flags: int = ..., + pos: int | None = ..., + endpos: int | None = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ignore_unused: bool = ..., + **kwargs: Any, +) -> tuple[AnyStr, int]: ... + +Pattern: _regex.Pattern +Match: _regex.Match +Regex = compile + +# Names in __all__ with no definition: +# Scanner +# error From 76656d4b2dcd6e45094bf75b02305c3e44aa1c6c Mon Sep 17 00:00:00 2001 From: jpy-git Date: Mon, 27 Dec 2021 18:39:32 +0000 Subject: [PATCH 02/18] Remove stubgen comments --- stubs/regex/regex/__init__.pyi | 60 ---------------------------------- stubs/regex/regex/regex.pyi | 4 --- 2 files changed, 64 deletions(-) diff --git a/stubs/regex/regex/__init__.pyi b/stubs/regex/regex/__init__.pyi index b47bdfb5d0fd..f310be632191 100644 --- a/stubs/regex/regex/__init__.pyi +++ b/stubs/regex/regex/__init__.pyi @@ -1,61 +1 @@ from .regex import * - -# Names in __all__ with no definition: -# A -# ASCII -# B -# BESTMATCH -# D -# DEBUG -# DEFAULT_VERSION -# DOTALL -# E -# ENHANCEMATCH -# F -# FULLCASE -# I -# IGNORECASE -# L -# LOCALE -# M -# MULTILINE -# Match -# P -# POSIX -# Pattern -# R -# REVERSE -# Regex -# S -# Scanner -# T -# TEMPLATE -# U -# UNICODE -# V0 -# V1 -# VERBOSE -# VERSION0 -# VERSION1 -# W -# WORD -# X -# __doc__ -# __version__ -# cache_all -# compile -# error -# escape -# findall -# finditer -# fullmatch -# match -# purge -# search -# split -# splititer -# sub -# subf -# subfn -# subn -# template diff --git a/stubs/regex/regex/regex.pyi b/stubs/regex/regex/regex.pyi index 0e87aae5d66c..1270e68e1dd5 100644 --- a/stubs/regex/regex/regex.pyi +++ b/stubs/regex/regex/regex.pyi @@ -347,7 +347,3 @@ def subfn( Pattern: _regex.Pattern Match: _regex.Match Regex = compile - -# Names in __all__ with no definition: -# Scanner -# error From 4b43f7dbb4880794a5c3fb0fafddbfb6fcdca291 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Mon, 27 Dec 2021 18:54:44 +0000 Subject: [PATCH 03/18] Remove unused import --- stubs/regex/regex/regex.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/regex/regex/regex.pyi b/stubs/regex/regex/regex.pyi index 1270e68e1dd5..9388e2054f0c 100644 --- a/stubs/regex/regex/regex.pyi +++ b/stubs/regex/regex/regex.pyi @@ -1,4 +1,4 @@ -from typing import Any, AnyStr, Callable, NoReturn, Tuple, _Alias, overload +from typing import Any, AnyStr, Callable, NoReturn, Tuple, overload import regex._regex as _regex From 6f7a6cf2165ac6c2292731836ea5ed706889698d Mon Sep 17 00:00:00 2001 From: jpy-git Date: Tue, 28 Dec 2021 22:07:08 +0000 Subject: [PATCH 04/18] Add C module classes --- stubs/regex/@tests/stubtest_allowlist.txt | 4 + stubs/regex/regex/_regex.pyi | 189 ++++++++++++++++++++-- stubs/regex/regex/regex.pyi | 96 +++++------ 3 files changed, 234 insertions(+), 55 deletions(-) create mode 100644 stubs/regex/@tests/stubtest_allowlist.txt diff --git a/stubs/regex/@tests/stubtest_allowlist.txt b/stubs/regex/@tests/stubtest_allowlist.txt new file mode 100644 index 000000000000..b40625acf75a --- /dev/null +++ b/stubs/regex/@tests/stubtest_allowlist.txt @@ -0,0 +1,4 @@ +regex._regex.Match +regex._regex.Pattern +regex._regex.Scanner +regex._regex.Splitter diff --git a/stubs/regex/regex/_regex.pyi b/stubs/regex/regex/_regex.pyi index f2b5d25a8e23..ab4e666bae36 100644 --- a/stubs/regex/regex/_regex.pyi +++ b/stubs/regex/regex/_regex.pyi @@ -1,13 +1,184 @@ -from typing import Any +from typing import AnyStr, Callable, FrozenSet, Generic, Iterator, NoReturn, TypeVar, overload -class Pattern: - def __getattr__(self, name: str) -> Any: ... # incomplete +_T = TypeVar("_T") -class Match: - def __getattr__(self, name: str) -> Any: ... # incomplete +class Pattern(Generic[AnyStr]): -class Scanner: - def __getattr__(self, name: str) -> Any: ... # incomplete + pattern: AnyStr + flags: int + groups: int + groupindex: dict[str, int] + named_lists: dict[str, FrozenSet[AnyStr]] + def search( + self, + string: AnyStr, + pos: int | None = ..., + endpos: int | None = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ) -> Match[AnyStr] | None: ... + def match( + self, + string: AnyStr, + pos: int | None = ..., + endpos: int | None = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ) -> Match[AnyStr] | None: ... + def fullmatch( + self, + string: AnyStr, + pos: int | None = ..., + endpos: int | None = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ) -> Match[AnyStr] | None: ... + def split( + self, string: AnyStr, maxsplit: int = ..., concurrent: bool | None = ..., timeout: int | None = ... + ) -> list[AnyStr]: ... + def splititer( + self, string: AnyStr, maxsplit: int = ..., concurrent: bool | None = ..., timeout: int | None = ... + ) -> Splitter[AnyStr]: ... + def findall( + self, + string: AnyStr, + pos: int | None = ..., + endpos: int | None = ..., + overlapped: bool = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ) -> list[AnyStr] | list[tuple[AnyStr, ...]]: ... + def finditer( + self, + string: AnyStr, + pos: int | None = ..., + endpos: int | None = ..., + overlapped: bool = ..., + concurrent: bool | None = ..., + timeout: int | 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: int | 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: int | 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: int | 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: int | None = ..., + ) -> tuple[AnyStr, int]: ... + def scanner( + self, + string: AnyStr, + pos: int | None = ..., + endpos: int | None = ..., + overlapped: bool = ..., + concurrent: bool | None = ..., + timeout: int | None = ..., + ) -> Scanner[AnyStr]: ... -class Splitter: - def __getattr__(self, name: str) -> Any: ... # incomplete +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 + @overload + def group(self, __group: int | str = ...) -> AnyStr | None: ... + @overload + def group(self, __group1: int | str, __group2: int | str, *groups: int | str) -> tuple[AnyStr | None, ...]: ... + @overload + def groups(self, default: None = ...) -> tuple[AnyStr | None, ...]: ... + @overload + def groups(self, default: _T) -> tuple[AnyStr | _T, ...]: ... + @overload + def groupdict(self, default: None = ...) -> dict[str, AnyStr | None]: ... + @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) -> NoReturn: ... + +class Splitter(Generic[AnyStr]): + + pattern: Pattern[AnyStr] + def __iter__(self) -> Iterator[AnyStr]: ... + def __next__(self) -> AnyStr: ... + def split(self) -> AnyStr | None: ... + +class Scanner(Generic[AnyStr]): + + pattern: Pattern[AnyStr] + def __iter__(self) -> Iterator[Match[AnyStr]]: ... + def __next__(self) -> Match[AnyStr]: ... + def match(self) -> Match[AnyStr] | None: ... + def search(self) -> Match[AnyStr] | None: ... diff --git a/stubs/regex/regex/regex.pyi b/stubs/regex/regex/regex.pyi index 9388e2054f0c..444201988542 100644 --- a/stubs/regex/regex/regex.pyi +++ b/stubs/regex/regex/regex.pyi @@ -1,4 +1,4 @@ -from typing import Any, AnyStr, Callable, NoReturn, Tuple, overload +from typing import Any, AnyStr, Callable, NoReturn, overload import regex._regex as _regex @@ -42,9 +42,11 @@ VERBOSE: int DEFAULT_VERSION: int @overload -def compile(pattern: AnyStr, flags: int = ..., ignore_unused: bool = ..., **kwargs: Any) -> _regex.Pattern: ... +def compile(pattern: AnyStr, flags: int = ..., ignore_unused: bool = ..., **kwargs: Any) -> _regex.Pattern[AnyStr]: ... @overload -def compile(pattern: _regex.Pattern, flags: int = ..., ignore_unused: bool = ..., **kwargs: Any) -> _regex.Pattern: ... +def compile( + pattern: _regex.Pattern[AnyStr], flags: int = ..., ignore_unused: bool = ..., **kwargs: Any +) -> _regex.Pattern[AnyStr]: ... @overload def search( pattern: AnyStr, @@ -57,10 +59,10 @@ def search( timeout: int | None = ..., ignore_unused: bool = ..., **kwargs: Any, -) -> _regex.Match | None: ... +) -> _regex.Match[AnyStr] | None: ... @overload def search( - pattern: _regex.Pattern, + pattern: _regex.Pattern[AnyStr], string: AnyStr, flags: int = ..., pos: int | None = ..., @@ -70,7 +72,7 @@ def search( timeout: int | None = ..., ignore_unused: bool = ..., **kwargs: Any, -) -> _regex.Match | None: ... +) -> _regex.Match[AnyStr] | None: ... @overload def match( pattern: AnyStr, @@ -83,10 +85,10 @@ def match( timeout: int | None = ..., ignore_unused: bool = ..., **kwargs: Any, -) -> _regex.Match: ... +) -> _regex.Match[AnyStr] | None: ... @overload def match( - pattern: _regex.Pattern, + pattern: _regex.Pattern[AnyStr], string: AnyStr, flags: int = ..., pos: int | None = ..., @@ -96,7 +98,7 @@ def match( timeout: int | None = ..., ignore_unused: bool = ..., **kwargs: Any, -) -> _regex.Match: ... +) -> _regex.Match[AnyStr] | None: ... @overload def fullmatch( pattern: AnyStr, @@ -109,10 +111,10 @@ def fullmatch( timeout: int | None = ..., ignore_unused: bool = ..., **kwargs: Any, -) -> _regex.Match: ... +) -> _regex.Match[AnyStr] | None: ... @overload def fullmatch( - pattern: _regex.Pattern, + pattern: _regex.Pattern[AnyStr], string: AnyStr, flags: int = ..., pos: int | None = ..., @@ -122,7 +124,7 @@ def fullmatch( timeout: int | None = ..., ignore_unused: bool = ..., **kwargs: Any, -) -> _regex.Match: ... +) -> _regex.Match[AnyStr] | None: ... @overload def split( pattern: AnyStr, @@ -136,7 +138,7 @@ def split( ) -> list[AnyStr]: ... @overload def split( - pattern: _regex.Pattern, + pattern: _regex.Pattern[AnyStr], string: AnyStr, maxsplit: int = ..., flags: int = ..., @@ -155,10 +157,10 @@ def splititer( timeout: int | None = ..., ignore_unused: bool = ..., **kwargs: Any, -) -> _regex.Splitter: ... +) -> _regex.Splitter[AnyStr]: ... @overload def splititer( - pattern: _regex.Pattern, + pattern: _regex.Pattern[AnyStr], string: AnyStr, maxsplit: int = ..., flags: int = ..., @@ -166,7 +168,7 @@ def splititer( timeout: int | None = ..., ignore_unused: bool = ..., **kwargs: Any, -) -> _regex.Splitter: ... +) -> _regex.Splitter[AnyStr]: ... @overload def findall( pattern: AnyStr, @@ -179,10 +181,10 @@ def findall( timeout: int | None = ..., ignore_unused: bool = ..., **kwargs: Any, -) -> list[str] | list[Tuple[str, ...]]: ... +) -> list[AnyStr] | list[tuple[AnyStr, ...]]: ... @overload def findall( - pattern: _regex.Pattern, + pattern: _regex.Pattern[AnyStr], string: AnyStr, flags: int = ..., pos: int | None = ..., @@ -192,7 +194,7 @@ def findall( timeout: int | None = ..., ignore_unused: bool = ..., **kwargs: Any, -) -> list[str] | list[Tuple[str, ...]]: ... +) -> list[AnyStr] | list[tuple[AnyStr, ...]]: ... @overload def finditer( pattern: AnyStr, @@ -206,10 +208,10 @@ def finditer( timeout: int | None = ..., ignore_unused: bool = ..., **kwargs: Any, -) -> _regex.Scanner: ... +) -> _regex.Scanner[AnyStr]: ... @overload def finditer( - pattern: _regex.Pattern, + pattern: _regex.Pattern[AnyStr], string: AnyStr, flags: int = ..., pos: int | None = ..., @@ -220,21 +222,11 @@ def finditer( timeout: int | None = ..., ignore_unused: bool = ..., **kwargs: Any, -) -> _regex.Scanner: ... -def purge() -> None: ... -@overload -def cache_all(value: bool = ...) -> NoReturn: ... -@overload -def cache_all(value: None = ...) -> bool: ... -def escape(pattern: AnyStr, special_only: bool = ..., literal_spaces: bool = ...) -> AnyStr: ... -@overload -def template(pattern: AnyStr, flags: int = ...) -> _regex.Pattern: ... -@overload -def template(pattern: _regex.Pattern, flags: int = ...) -> _regex.Pattern: ... +) -> _regex.Scanner[AnyStr]: ... @overload def sub( pattern: AnyStr, - repl: AnyStr | Callable[[_regex.Match], AnyStr], + repl: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: int = ..., @@ -247,8 +239,8 @@ def sub( ) -> AnyStr: ... @overload def sub( - pattern: _regex.Pattern, - repl: AnyStr | Callable[[_regex.Match], AnyStr], + pattern: _regex.Pattern[AnyStr], + repl: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: int = ..., @@ -262,7 +254,7 @@ def sub( @overload def subf( pattern: AnyStr, - format: AnyStr | Callable[[_regex.Match], AnyStr], + format: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: int = ..., @@ -275,8 +267,8 @@ def subf( ) -> AnyStr: ... @overload def subf( - pattern: _regex.Pattern, - format: AnyStr | Callable[[_regex.Match], AnyStr], + pattern: _regex.Pattern[AnyStr], + format: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: int = ..., @@ -290,7 +282,7 @@ def subf( @overload def subn( pattern: AnyStr, - repl: AnyStr | Callable[[_regex.Match], AnyStr], + repl: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: int = ..., @@ -303,8 +295,8 @@ def subn( ) -> AnyStr: ... @overload def subn( - pattern: _regex.Pattern, - repl: AnyStr | Callable[[_regex.Match], AnyStr], + pattern: _regex.Pattern[AnyStr], + repl: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: int = ..., @@ -318,7 +310,7 @@ def subn( @overload def subfn( pattern: AnyStr, - format: AnyStr | Callable[[_regex.Match], AnyStr], + format: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: int = ..., @@ -331,8 +323,8 @@ def subfn( ) -> tuple[AnyStr, int]: ... @overload def subfn( - pattern: _regex.Pattern, - format: AnyStr | Callable[[_regex.Match], AnyStr], + pattern: _regex.Pattern[AnyStr], + format: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: int = ..., @@ -343,7 +335,19 @@ def subfn( ignore_unused: bool = ..., **kwargs: Any, ) -> tuple[AnyStr, int]: ... +def purge() -> None: ... +@overload +def cache_all(value: bool = ...) -> NoReturn: ... +@overload +def cache_all(value: None) -> bool: ... +def escape(pattern: AnyStr, special_only: bool = ..., literal_spaces: bool = ...) -> AnyStr: ... +@overload +def template(pattern: AnyStr, flags: int = ...) -> _regex.Pattern[AnyStr]: ... +@overload +def template(pattern: _regex.Pattern[AnyStr], flags: int = ...) -> _regex.Pattern[AnyStr]: ... -Pattern: _regex.Pattern -Match: _regex.Match +Pattern: type[_regex.Pattern] +Match: type[_regex.Match] Regex = compile + +# TODO: Scanner, error From 15f4fec5c399fcc86ff6e9ea26f5a5e747dc9768 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Tue, 28 Dec 2021 22:10:28 +0000 Subject: [PATCH 05/18] Use type alias for Pattern and Match --- stubs/regex/regex/regex.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stubs/regex/regex/regex.pyi b/stubs/regex/regex/regex.pyi index 444201988542..851e90a50ca7 100644 --- a/stubs/regex/regex/regex.pyi +++ b/stubs/regex/regex/regex.pyi @@ -346,8 +346,8 @@ def template(pattern: AnyStr, flags: int = ...) -> _regex.Pattern[AnyStr]: ... @overload def template(pattern: _regex.Pattern[AnyStr], flags: int = ...) -> _regex.Pattern[AnyStr]: ... -Pattern: type[_regex.Pattern] -Match: type[_regex.Match] +Pattern = _regex.Pattern +Match = _regex.Match Regex = compile # TODO: Scanner, error From 7d3bbf330bd5a5356f1f0203e8f93285edcf9ea3 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Tue, 28 Dec 2021 22:14:00 +0000 Subject: [PATCH 06/18] Use frozenset over Frozenset --- stubs/regex/regex/_regex.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stubs/regex/regex/_regex.pyi b/stubs/regex/regex/_regex.pyi index ab4e666bae36..10e95faffe6b 100644 --- a/stubs/regex/regex/_regex.pyi +++ b/stubs/regex/regex/_regex.pyi @@ -1,4 +1,4 @@ -from typing import AnyStr, Callable, FrozenSet, Generic, Iterator, NoReturn, TypeVar, overload +from typing import AnyStr, Callable, Generic, Iterator, NoReturn, TypeVar, overload _T = TypeVar("_T") @@ -8,7 +8,7 @@ class Pattern(Generic[AnyStr]): flags: int groups: int groupindex: dict[str, int] - named_lists: dict[str, FrozenSet[AnyStr]] + named_lists: dict[str, frozenset[AnyStr]] def search( self, string: AnyStr, From 8284e84b4793f9a11fc4130276ceaa35604a2743 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Wed, 29 Dec 2021 01:06:47 +0000 Subject: [PATCH 07/18] Add error and Scanner --- stubs/regex/regex/_regex_core.pyi | 45 +++++++++++++++++++++++++++++++ stubs/regex/regex/regex.pyi | 40 ++------------------------- 2 files changed, 47 insertions(+), 38 deletions(-) create mode 100644 stubs/regex/regex/_regex_core.pyi diff --git a/stubs/regex/regex/_regex_core.pyi b/stubs/regex/regex/_regex_core.pyi new file mode 100644 index 000000000000..2c0ea080e305 --- /dev/null +++ b/stubs/regex/regex/_regex_core.pyi @@ -0,0 +1,45 @@ +from typing import Any, AnyStr, Callable + +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 + +class Scanner: + def __init__(self, lexicon: list[tuple[AnyStr, Callable[[Scanner, AnyStr], Any] | Any | None]], flags: int = ...) -> None: ... + def scan(self, string: AnyStr) -> tuple[list[Any], AnyStr]: ... diff --git a/stubs/regex/regex/regex.pyi b/stubs/regex/regex/regex.pyi index 851e90a50ca7..4a7838b490d2 100644 --- a/stubs/regex/regex/regex.pyi +++ b/stubs/regex/regex/regex.pyi @@ -1,46 +1,10 @@ from typing import Any, AnyStr, Callable, NoReturn, overload import regex._regex as _regex +from regex._regex_core import * __version__: str -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 - @overload def compile(pattern: AnyStr, flags: int = ..., ignore_unused: bool = ..., **kwargs: Any) -> _regex.Pattern[AnyStr]: ... @overload @@ -350,4 +314,4 @@ Pattern = _regex.Pattern Match = _regex.Match Regex = compile -# TODO: Scanner, error +# TODO: Scanner From 1be94ad3890e344ab90803ea845356a0adc38052 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Wed, 29 Dec 2021 01:06:56 +0000 Subject: [PATCH 08/18] Add error and Scanner --- stubs/regex/regex/regex.pyi | 2 -- 1 file changed, 2 deletions(-) diff --git a/stubs/regex/regex/regex.pyi b/stubs/regex/regex/regex.pyi index 4a7838b490d2..6ad087713656 100644 --- a/stubs/regex/regex/regex.pyi +++ b/stubs/regex/regex/regex.pyi @@ -313,5 +313,3 @@ def template(pattern: _regex.Pattern[AnyStr], flags: int = ...) -> _regex.Patter Pattern = _regex.Pattern Match = _regex.Match Regex = compile - -# TODO: Scanner From 5ddc0983158b40de8148cad365e16b92775892e0 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Wed, 29 Dec 2021 12:39:18 +0000 Subject: [PATCH 09/18] use list[Any] instead of union return --- stubs/regex/regex/_regex.pyi | 6 +++--- stubs/regex/regex/regex.pyi | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/stubs/regex/regex/_regex.pyi b/stubs/regex/regex/_regex.pyi index 10e95faffe6b..01035a043079 100644 --- a/stubs/regex/regex/_regex.pyi +++ b/stubs/regex/regex/_regex.pyi @@ -1,4 +1,4 @@ -from typing import AnyStr, Callable, Generic, Iterator, NoReturn, TypeVar, overload +from typing import Any, AnyStr, Callable, Generic, Iterator, TypeVar, overload _T = TypeVar("_T") @@ -47,7 +47,7 @@ class Pattern(Generic[AnyStr]): overlapped: bool = ..., concurrent: bool | None = ..., timeout: int | None = ..., - ) -> list[AnyStr] | list[tuple[AnyStr, ...]]: ... + ) -> list[Any]: ... def finditer( self, string: AnyStr, @@ -166,7 +166,7 @@ class Match(Generic[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) -> NoReturn: ... + def detach_string(self) -> None: ... class Splitter(Generic[AnyStr]): diff --git a/stubs/regex/regex/regex.pyi b/stubs/regex/regex/regex.pyi index 6ad087713656..0e5161cdf330 100644 --- a/stubs/regex/regex/regex.pyi +++ b/stubs/regex/regex/regex.pyi @@ -1,4 +1,4 @@ -from typing import Any, AnyStr, Callable, NoReturn, overload +from typing import Any, AnyStr, Callable, overload import regex._regex as _regex from regex._regex_core import * @@ -145,7 +145,7 @@ def findall( timeout: int | None = ..., ignore_unused: bool = ..., **kwargs: Any, -) -> list[AnyStr] | list[tuple[AnyStr, ...]]: ... +) -> list[Any]: ... @overload def findall( pattern: _regex.Pattern[AnyStr], @@ -158,7 +158,7 @@ def findall( timeout: int | None = ..., ignore_unused: bool = ..., **kwargs: Any, -) -> list[AnyStr] | list[tuple[AnyStr, ...]]: ... +) -> list[Any]: ... @overload def finditer( pattern: AnyStr, @@ -301,7 +301,7 @@ def subfn( ) -> tuple[AnyStr, int]: ... def purge() -> None: ... @overload -def cache_all(value: bool = ...) -> NoReturn: ... +def cache_all(value: bool = ...) -> None: ... @overload def cache_all(value: None) -> bool: ... def escape(pattern: AnyStr, special_only: bool = ..., literal_spaces: bool = ...) -> AnyStr: ... From fca60fb3fd29505fca3c4b0158c4d5e22099ac70 Mon Sep 17 00:00:00 2001 From: Joseph Young <80432516+jpy-git@users.noreply.github.com> Date: Thu, 30 Dec 2021 13:21:46 +0000 Subject: [PATCH 10/18] Update stubs/regex/regex/regex.pyi Co-authored-by: Alex Waygood --- stubs/regex/regex/regex.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stubs/regex/regex/regex.pyi b/stubs/regex/regex/regex.pyi index 0e5161cdf330..e43b2861f9cd 100644 --- a/stubs/regex/regex/regex.pyi +++ b/stubs/regex/regex/regex.pyi @@ -1,7 +1,7 @@ from typing import Any, AnyStr, Callable, overload -import regex._regex as _regex -from regex._regex_core import * +from . import _regex +from ._regex_core import * __version__: str From 7944312d82edf76cd94ced382e12c3361a60357d Mon Sep 17 00:00:00 2001 From: jpy-git Date: Thu, 30 Dec 2021 13:31:09 +0000 Subject: [PATCH 11/18] Remove unnecessary overrides --- stubs/regex/regex/regex.pyi | 187 +++--------------------------------- 1 file changed, 13 insertions(+), 174 deletions(-) diff --git a/stubs/regex/regex/regex.pyi b/stubs/regex/regex/regex.pyi index e43b2861f9cd..79be27859559 100644 --- a/stubs/regex/regex/regex.pyi +++ b/stubs/regex/regex/regex.pyi @@ -5,15 +5,11 @@ from ._regex_core import * __version__: str -@overload -def compile(pattern: AnyStr, flags: int = ..., ignore_unused: bool = ..., **kwargs: Any) -> _regex.Pattern[AnyStr]: ... -@overload def compile( - pattern: _regex.Pattern[AnyStr], flags: int = ..., ignore_unused: bool = ..., **kwargs: Any + pattern: AnyStr | _regex.Pattern[AnyStr], flags: int = ..., ignore_unused: bool = ..., **kwargs: Any ) -> _regex.Pattern[AnyStr]: ... -@overload def search( - pattern: AnyStr, + pattern: AnyStr | _regex.Pattern[AnyStr], string: AnyStr, flags: int = ..., pos: int | None = ..., @@ -24,48 +20,8 @@ def search( ignore_unused: bool = ..., **kwargs: Any, ) -> _regex.Match[AnyStr] | None: ... -@overload -def search( - pattern: _regex.Pattern[AnyStr], - string: AnyStr, - flags: int = ..., - pos: int | None = ..., - endpos: int | None = ..., - partial: bool = ..., - concurrent: bool | None = ..., - timeout: int | None = ..., - ignore_unused: bool = ..., - **kwargs: Any, -) -> _regex.Match[AnyStr] | None: ... -@overload def match( - pattern: AnyStr, - string: AnyStr, - flags: int = ..., - pos: int | None = ..., - endpos: int | None = ..., - partial: bool = ..., - concurrent: bool | None = ..., - timeout: int | None = ..., - ignore_unused: bool = ..., - **kwargs: Any, -) -> _regex.Match[AnyStr] | None: ... -@overload -def match( - pattern: _regex.Pattern[AnyStr], - string: AnyStr, - flags: int = ..., - pos: int | None = ..., - endpos: int | None = ..., - partial: bool = ..., - concurrent: bool | None = ..., - timeout: int | None = ..., - ignore_unused: bool = ..., - **kwargs: Any, -) -> _regex.Match[AnyStr] | None: ... -@overload -def fullmatch( - pattern: AnyStr, + pattern: AnyStr | _regex.Pattern[AnyStr], string: AnyStr, flags: int = ..., pos: int | None = ..., @@ -76,9 +32,8 @@ def fullmatch( ignore_unused: bool = ..., **kwargs: Any, ) -> _regex.Match[AnyStr] | None: ... -@overload def fullmatch( - pattern: _regex.Pattern[AnyStr], + pattern: AnyStr | _regex.Pattern[AnyStr], string: AnyStr, flags: int = ..., pos: int | None = ..., @@ -89,9 +44,8 @@ def fullmatch( ignore_unused: bool = ..., **kwargs: Any, ) -> _regex.Match[AnyStr] | None: ... -@overload def split( - pattern: AnyStr, + pattern: AnyStr | _regex.Pattern[AnyStr], string: AnyStr, maxsplit: int = ..., flags: int = ..., @@ -100,20 +54,8 @@ def split( ignore_unused: bool = ..., **kwargs: Any, ) -> list[AnyStr]: ... -@overload -def split( - pattern: _regex.Pattern[AnyStr], - string: AnyStr, - maxsplit: int = ..., - flags: int = ..., - concurrent: bool | None = ..., - timeout: int | None = ..., - ignore_unused: bool = ..., - **kwargs: Any, -) -> list[AnyStr]: ... -@overload def splititer( - pattern: AnyStr, + pattern: AnyStr | _regex.Pattern[AnyStr], string: AnyStr, maxsplit: int = ..., flags: int = ..., @@ -122,33 +64,8 @@ def splititer( ignore_unused: bool = ..., **kwargs: Any, ) -> _regex.Splitter[AnyStr]: ... -@overload -def splititer( - pattern: _regex.Pattern[AnyStr], - string: AnyStr, - maxsplit: int = ..., - flags: int = ..., - concurrent: bool | None = ..., - timeout: int | None = ..., - ignore_unused: bool = ..., - **kwargs: Any, -) -> _regex.Splitter[AnyStr]: ... -@overload -def findall( - pattern: AnyStr, - string: AnyStr, - flags: int = ..., - pos: int | None = ..., - endpos: int | None = ..., - overlapped: bool = ..., - concurrent: bool | None = ..., - timeout: int | None = ..., - ignore_unused: bool = ..., - **kwargs: Any, -) -> list[Any]: ... -@overload def findall( - pattern: _regex.Pattern[AnyStr], + pattern: AnyStr | _regex.Pattern[AnyStr], string: AnyStr, flags: int = ..., pos: int | None = ..., @@ -159,9 +76,8 @@ def findall( ignore_unused: bool = ..., **kwargs: Any, ) -> list[Any]: ... -@overload def finditer( - pattern: AnyStr, + pattern: AnyStr | _regex.Pattern[AnyStr], string: AnyStr, flags: int = ..., pos: int | None = ..., @@ -173,23 +89,8 @@ def finditer( ignore_unused: bool = ..., **kwargs: Any, ) -> _regex.Scanner[AnyStr]: ... -@overload -def finditer( - pattern: _regex.Pattern[AnyStr], - string: AnyStr, - flags: int = ..., - pos: int | None = ..., - endpos: int | None = ..., - overlapped: bool = ..., - partial: bool = ..., - concurrent: bool | None = ..., - timeout: int | None = ..., - ignore_unused: bool = ..., - **kwargs: Any, -) -> _regex.Scanner[AnyStr]: ... -@overload def sub( - pattern: AnyStr, + pattern: AnyStr | _regex.Pattern[AnyStr], repl: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., @@ -201,37 +102,8 @@ def sub( ignore_unused: bool = ..., **kwargs: Any, ) -> AnyStr: ... -@overload -def sub( - pattern: _regex.Pattern[AnyStr], - repl: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr], - string: AnyStr, - count: int = ..., - flags: int = ..., - pos: int | None = ..., - endpos: int | None = ..., - concurrent: bool | None = ..., - timeout: int | None = ..., - ignore_unused: bool = ..., - **kwargs: Any, -) -> AnyStr: ... -@overload -def subf( - pattern: AnyStr, - format: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr], - string: AnyStr, - count: int = ..., - flags: int = ..., - pos: int | None = ..., - endpos: int | None = ..., - concurrent: bool | None = ..., - timeout: int | None = ..., - ignore_unused: bool = ..., - **kwargs: Any, -) -> AnyStr: ... -@overload def subf( - pattern: _regex.Pattern[AnyStr], + pattern: AnyStr | _regex.Pattern[AnyStr], format: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., @@ -243,9 +115,8 @@ def subf( ignore_unused: bool = ..., **kwargs: Any, ) -> AnyStr: ... -@overload def subn( - pattern: AnyStr, + pattern: AnyStr | _regex.Pattern[AnyStr], repl: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., @@ -257,37 +128,8 @@ def subn( ignore_unused: bool = ..., **kwargs: Any, ) -> AnyStr: ... -@overload -def subn( - pattern: _regex.Pattern[AnyStr], - repl: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr], - string: AnyStr, - count: int = ..., - flags: int = ..., - pos: int | None = ..., - endpos: int | None = ..., - concurrent: bool | None = ..., - timeout: int | None = ..., - ignore_unused: bool = ..., - **kwargs: Any, -) -> AnyStr: ... -@overload -def subfn( - pattern: AnyStr, - format: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr], - string: AnyStr, - count: int = ..., - flags: int = ..., - pos: int | None = ..., - endpos: int | None = ..., - concurrent: bool | None = ..., - timeout: int | None = ..., - ignore_unused: bool = ..., - **kwargs: Any, -) -> tuple[AnyStr, int]: ... -@overload def subfn( - pattern: _regex.Pattern[AnyStr], + pattern: AnyStr | _regex.Pattern[AnyStr], format: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., @@ -305,10 +147,7 @@ def cache_all(value: bool = ...) -> None: ... @overload def cache_all(value: None) -> bool: ... def escape(pattern: AnyStr, special_only: bool = ..., literal_spaces: bool = ...) -> AnyStr: ... -@overload -def template(pattern: AnyStr, flags: int = ...) -> _regex.Pattern[AnyStr]: ... -@overload -def template(pattern: _regex.Pattern[AnyStr], flags: int = ...) -> _regex.Pattern[AnyStr]: ... +def template(pattern: AnyStr | _regex.Pattern[AnyStr], flags: int = ...) -> _regex.Pattern[AnyStr]: ... Pattern = _regex.Pattern Match = _regex.Match From 021ca84ae484d91926c25f1a4494d3a388ad7875 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Thu, 30 Dec 2021 13:37:07 +0000 Subject: [PATCH 12/18] Use Mapping over dict --- stubs/regex/regex/_regex.pyi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stubs/regex/regex/_regex.pyi b/stubs/regex/regex/_regex.pyi index 01035a043079..494fd57afea2 100644 --- a/stubs/regex/regex/_regex.pyi +++ b/stubs/regex/regex/_regex.pyi @@ -1,4 +1,4 @@ -from typing import Any, AnyStr, Callable, Generic, Iterator, TypeVar, overload +from typing import Any, AnyStr, Callable, Generic, Iterator, Mapping, TypeVar, overload _T = TypeVar("_T") @@ -7,8 +7,8 @@ class Pattern(Generic[AnyStr]): pattern: AnyStr flags: int groups: int - groupindex: dict[str, int] - named_lists: dict[str, frozenset[AnyStr]] + groupindex: Mapping[str, int] + named_lists: Mapping[str, frozenset[AnyStr]] def search( self, string: AnyStr, From 0453e3cc2af336b2f4ec36b9430e17a9f3b769e8 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Thu, 30 Dec 2021 13:52:38 +0000 Subject: [PATCH 13/18] Remove undocumented Scanner class and use _typeshed.Self --- stubs/regex/regex/_regex.pyi | 7 ++++--- stubs/regex/regex/_regex_core.pyi | 6 +----- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/stubs/regex/regex/_regex.pyi b/stubs/regex/regex/_regex.pyi index 494fd57afea2..4db155de9dc1 100644 --- a/stubs/regex/regex/_regex.pyi +++ b/stubs/regex/regex/_regex.pyi @@ -1,4 +1,5 @@ -from typing import Any, AnyStr, Callable, Generic, Iterator, Mapping, TypeVar, overload +from _typeshed import Self +from typing import Any, AnyStr, Callable, Generic, Mapping, TypeVar, overload _T = TypeVar("_T") @@ -171,14 +172,14 @@ class Match(Generic[AnyStr]): class Splitter(Generic[AnyStr]): pattern: Pattern[AnyStr] - def __iter__(self) -> Iterator[AnyStr]: ... + def __iter__(self: Self) -> Self: ... def __next__(self) -> AnyStr: ... def split(self) -> AnyStr | None: ... class Scanner(Generic[AnyStr]): pattern: Pattern[AnyStr] - def __iter__(self) -> Iterator[Match[AnyStr]]: ... + def __iter__(self: Self) -> Self: ... def __next__(self) -> Match[AnyStr]: ... def match(self) -> Match[AnyStr] | None: ... def search(self) -> Match[AnyStr] | None: ... diff --git a/stubs/regex/regex/_regex_core.pyi b/stubs/regex/regex/_regex_core.pyi index 2c0ea080e305..c3f5685c13dd 100644 --- a/stubs/regex/regex/_regex_core.pyi +++ b/stubs/regex/regex/_regex_core.pyi @@ -1,4 +1,4 @@ -from typing import Any, AnyStr, Callable +from typing import AnyStr class error(Exception): def __init__(self, message: str, pattern: AnyStr | None = ..., pos: int | None = ...) -> None: ... @@ -39,7 +39,3 @@ X: int VERBOSE: int DEFAULT_VERSION: int - -class Scanner: - def __init__(self, lexicon: list[tuple[AnyStr, Callable[[Scanner, AnyStr], Any] | Any | None]], flags: int = ...) -> None: ... - def scan(self, string: AnyStr) -> tuple[list[Any], AnyStr]: ... From 5ce5e17ce43eeea9f6ec77a95288d623a5039092 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Thu, 30 Dec 2021 13:57:27 +0000 Subject: [PATCH 14/18] Add stubtest comment --- stubs/regex/@tests/stubtest_allowlist.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stubs/regex/@tests/stubtest_allowlist.txt b/stubs/regex/@tests/stubtest_allowlist.txt index b40625acf75a..1d7059527231 100644 --- a/stubs/regex/@tests/stubtest_allowlist.txt +++ b/stubs/regex/@tests/stubtest_allowlist.txt @@ -1,3 +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, +# so therefore we exclude them from the stubtest. regex._regex.Match regex._regex.Pattern regex._regex.Scanner From 1961df157162440ea09ff65e0c8b9afdc1af52ad Mon Sep 17 00:00:00 2001 From: jpy-git Date: Thu, 30 Dec 2021 18:11:35 +0000 Subject: [PATCH 15/18] Align returns with re --- stubs/regex/regex/_regex.pyi | 14 +++++++------- stubs/regex/regex/regex.pyi | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/stubs/regex/regex/_regex.pyi b/stubs/regex/regex/_regex.pyi index 4db155de9dc1..f6fee53d85be 100644 --- a/stubs/regex/regex/_regex.pyi +++ b/stubs/regex/regex/_regex.pyi @@ -36,7 +36,7 @@ class Pattern(Generic[AnyStr]): ) -> Match[AnyStr] | None: ... def split( self, string: AnyStr, maxsplit: int = ..., concurrent: bool | None = ..., timeout: int | None = ... - ) -> list[AnyStr]: ... + ) -> list[AnyStr | Any]: ... def splititer( self, string: AnyStr, maxsplit: int = ..., concurrent: bool | None = ..., timeout: int | None = ... ) -> Splitter[AnyStr]: ... @@ -125,15 +125,15 @@ class Match(Generic[AnyStr]): lastgroup: str | None lastindex: int | None @overload - def group(self, __group: int | str = ...) -> AnyStr | None: ... + def group(self, __group: int | str = ...) -> AnyStr | Any: ... @overload - def group(self, __group1: int | str, __group2: int | str, *groups: int | str) -> tuple[AnyStr | None, ...]: ... + def group(self, __group1: int | str, __group2: int | str, *groups: int | str) -> tuple[AnyStr | Any, ...]: ... @overload - def groups(self, default: None = ...) -> tuple[AnyStr | None, ...]: ... + 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 | None]: ... + def groupdict(self, default: None = ...) -> dict[str, AnyStr | Any]: ... @overload def groupdict(self, default: _T) -> dict[str, AnyStr | _T]: ... @overload @@ -173,8 +173,8 @@ class Splitter(Generic[AnyStr]): pattern: Pattern[AnyStr] def __iter__(self: Self) -> Self: ... - def __next__(self) -> AnyStr: ... - def split(self) -> AnyStr | None: ... + def __next__(self) -> AnyStr | Any: ... + def split(self) -> AnyStr | Any: ... class Scanner(Generic[AnyStr]): diff --git a/stubs/regex/regex/regex.pyi b/stubs/regex/regex/regex.pyi index 79be27859559..1244a28ddf04 100644 --- a/stubs/regex/regex/regex.pyi +++ b/stubs/regex/regex/regex.pyi @@ -53,7 +53,7 @@ def split( timeout: int | None = ..., ignore_unused: bool = ..., **kwargs: Any, -) -> list[AnyStr]: ... +) -> list[AnyStr | Any]: ... def splititer( pattern: AnyStr | _regex.Pattern[AnyStr], string: AnyStr, @@ -127,7 +127,7 @@ def subn( timeout: int | None = ..., ignore_unused: bool = ..., **kwargs: Any, -) -> AnyStr: ... +) -> tuple[AnyStr, int]: ... def subfn( pattern: AnyStr | _regex.Pattern[AnyStr], format: AnyStr | Callable[[_regex.Match[AnyStr]], AnyStr], From dad0a1fd1e384b3860b573ea7d7c3e5d03eb5f4e Mon Sep 17 00:00:00 2001 From: Joseph Young <80432516+jpy-git@users.noreply.github.com> Date: Thu, 30 Dec 2021 18:44:03 +0000 Subject: [PATCH 16/18] Update stubs/regex/@tests/stubtest_allowlist.txt Co-authored-by: Akuli --- stubs/regex/@tests/stubtest_allowlist.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stubs/regex/@tests/stubtest_allowlist.txt b/stubs/regex/@tests/stubtest_allowlist.txt index 1d7059527231..2f05cbed12c7 100644 --- a/stubs/regex/@tests/stubtest_allowlist.txt +++ b/stubs/regex/@tests/stubtest_allowlist.txt @@ -1,6 +1,5 @@ # 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, -# so therefore we exclude them from the stubtest. +# 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 From c7e64ae9267a35501021299d9b1cd74cc935b05a Mon Sep 17 00:00:00 2001 From: jpy-git Date: Thu, 30 Dec 2021 19:05:13 +0000 Subject: [PATCH 17/18] Implement review feedback --- stubs/regex/regex/_regex.pyi | 32 +++++++++++++++++++------------- stubs/regex/regex/regex.pyi | 22 +++++++++++----------- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/stubs/regex/regex/_regex.pyi b/stubs/regex/regex/_regex.pyi index f6fee53d85be..3af64f5eda9c 100644 --- a/stubs/regex/regex/_regex.pyi +++ b/stubs/regex/regex/_regex.pyi @@ -1,8 +1,9 @@ from _typeshed import Self -from typing import Any, AnyStr, Callable, Generic, Mapping, TypeVar, overload +from typing import Any, AnyStr, Callable, Generic, Literal, Mapping, TypeVar, final, overload _T = TypeVar("_T") +@final class Pattern(Generic[AnyStr]): pattern: AnyStr @@ -16,7 +17,7 @@ class Pattern(Generic[AnyStr]): pos: int | None = ..., endpos: int | None = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ) -> Match[AnyStr] | None: ... def match( self, @@ -24,7 +25,7 @@ class Pattern(Generic[AnyStr]): pos: int | None = ..., endpos: int | None = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ) -> Match[AnyStr] | None: ... def fullmatch( self, @@ -32,13 +33,13 @@ class Pattern(Generic[AnyStr]): pos: int | None = ..., endpos: int | None = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ) -> Match[AnyStr] | None: ... def split( - self, string: AnyStr, maxsplit: int = ..., concurrent: bool | None = ..., timeout: int | None = ... + self, string: AnyStr, maxsplit: int = ..., concurrent: bool | None = ..., timeout: float | None = ... ) -> list[AnyStr | Any]: ... def splititer( - self, string: AnyStr, maxsplit: int = ..., concurrent: bool | None = ..., timeout: int | None = ... + self, string: AnyStr, maxsplit: int = ..., concurrent: bool | None = ..., timeout: float | None = ... ) -> Splitter[AnyStr]: ... def findall( self, @@ -47,7 +48,7 @@ class Pattern(Generic[AnyStr]): endpos: int | None = ..., overlapped: bool = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ) -> list[Any]: ... def finditer( self, @@ -56,7 +57,7 @@ class Pattern(Generic[AnyStr]): endpos: int | None = ..., overlapped: bool = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ) -> Scanner[AnyStr]: ... def sub( self, @@ -67,7 +68,7 @@ class Pattern(Generic[AnyStr]): pos: int | None = ..., endpos: int | None = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ) -> AnyStr: ... def subf( self, @@ -78,7 +79,7 @@ class Pattern(Generic[AnyStr]): pos: int | None = ..., endpos: int | None = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ) -> AnyStr: ... def subn( self, @@ -89,7 +90,7 @@ class Pattern(Generic[AnyStr]): pos: int | None = ..., endpos: int | None = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ) -> tuple[AnyStr, int]: ... def subfn( self, @@ -100,7 +101,7 @@ class Pattern(Generic[AnyStr]): pos: int | None = ..., endpos: int | None = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ) -> tuple[AnyStr, int]: ... def scanner( self, @@ -109,9 +110,10 @@ class Pattern(Generic[AnyStr]): endpos: int | None = ..., overlapped: bool = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ) -> Scanner[AnyStr]: ... +@final class Match(Generic[AnyStr]): re: Pattern[AnyStr] @@ -125,6 +127,8 @@ class Match(Generic[AnyStr]): lastgroup: str | None lastindex: int | None @overload + def group(self, __group: Literal[0] = ...) -> AnyStr: ... + @overload def group(self, __group: int | str = ...) -> AnyStr | Any: ... @overload def group(self, __group1: int | str, __group2: int | str, *groups: int | str) -> tuple[AnyStr | Any, ...]: ... @@ -169,6 +173,7 @@ class Match(Generic[AnyStr]): def capturesdict(self) -> dict[str, list[AnyStr]]: ... def detach_string(self) -> None: ... +@final class Splitter(Generic[AnyStr]): pattern: Pattern[AnyStr] @@ -176,6 +181,7 @@ class Splitter(Generic[AnyStr]): def __next__(self) -> AnyStr | Any: ... def split(self) -> AnyStr | Any: ... +@final class Scanner(Generic[AnyStr]): pattern: Pattern[AnyStr] diff --git a/stubs/regex/regex/regex.pyi b/stubs/regex/regex/regex.pyi index 1244a28ddf04..69812a26c45e 100644 --- a/stubs/regex/regex/regex.pyi +++ b/stubs/regex/regex/regex.pyi @@ -16,7 +16,7 @@ def search( endpos: int | None = ..., partial: bool = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ignore_unused: bool = ..., **kwargs: Any, ) -> _regex.Match[AnyStr] | None: ... @@ -28,7 +28,7 @@ def match( endpos: int | None = ..., partial: bool = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ignore_unused: bool = ..., **kwargs: Any, ) -> _regex.Match[AnyStr] | None: ... @@ -40,7 +40,7 @@ def fullmatch( endpos: int | None = ..., partial: bool = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ignore_unused: bool = ..., **kwargs: Any, ) -> _regex.Match[AnyStr] | None: ... @@ -50,7 +50,7 @@ def split( maxsplit: int = ..., flags: int = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ignore_unused: bool = ..., **kwargs: Any, ) -> list[AnyStr | Any]: ... @@ -60,7 +60,7 @@ def splititer( maxsplit: int = ..., flags: int = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ignore_unused: bool = ..., **kwargs: Any, ) -> _regex.Splitter[AnyStr]: ... @@ -72,7 +72,7 @@ def findall( endpos: int | None = ..., overlapped: bool = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ignore_unused: bool = ..., **kwargs: Any, ) -> list[Any]: ... @@ -85,7 +85,7 @@ def finditer( overlapped: bool = ..., partial: bool = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ignore_unused: bool = ..., **kwargs: Any, ) -> _regex.Scanner[AnyStr]: ... @@ -98,7 +98,7 @@ def sub( pos: int | None = ..., endpos: int | None = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ignore_unused: bool = ..., **kwargs: Any, ) -> AnyStr: ... @@ -111,7 +111,7 @@ def subf( pos: int | None = ..., endpos: int | None = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ignore_unused: bool = ..., **kwargs: Any, ) -> AnyStr: ... @@ -124,7 +124,7 @@ def subn( pos: int | None = ..., endpos: int | None = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ignore_unused: bool = ..., **kwargs: Any, ) -> tuple[AnyStr, int]: ... @@ -137,7 +137,7 @@ def subfn( pos: int | None = ..., endpos: int | None = ..., concurrent: bool | None = ..., - timeout: int | None = ..., + timeout: float | None = ..., ignore_unused: bool = ..., **kwargs: Any, ) -> tuple[AnyStr, int]: ... From eb84b3658f18dcbd00d95dfcdf1be2479ff654b8 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Thu, 30 Dec 2021 19:09:28 +0000 Subject: [PATCH 18/18] Use typing_extensions for Literal and final --- stubs/regex/regex/_regex.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stubs/regex/regex/_regex.pyi b/stubs/regex/regex/_regex.pyi index 3af64f5eda9c..0e591540092d 100644 --- a/stubs/regex/regex/_regex.pyi +++ b/stubs/regex/regex/_regex.pyi @@ -1,5 +1,6 @@ from _typeshed import Self -from typing import Any, AnyStr, Callable, Generic, Literal, Mapping, TypeVar, final, overload +from typing import Any, AnyStr, Callable, Generic, Mapping, TypeVar, overload +from typing_extensions import Literal, final _T = TypeVar("_T")