From dcca46bab64a56c89af4a55779248d69d34b49f8 Mon Sep 17 00:00:00 2001 From: Trim21 Date: Sat, 5 Oct 2024 01:17:19 +0800 Subject: [PATCH 1/3] use AnyStr and add missing overload --- .gitignore | 2 + src/re2-stubs/__init__.pyi | 88 ++++++++++---------------------------- 2 files changed, 25 insertions(+), 65 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dfcd050 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.venv/ +.idea/ diff --git a/src/re2-stubs/__init__.pyi b/src/re2-stubs/__init__.pyi index 2915770..efe0324 100644 --- a/src/re2-stubs/__init__.pyi +++ b/src/re2-stubs/__init__.pyi @@ -117,79 +117,37 @@ def purge() -> None: ... class _Regexp(Generic[AnyStr]): def __init__(self, pattern: AnyStr, options: Options) -> None: ... - @overload - def search( - self: _Regexp[str], text: str, pos: int | None = None, endpos: int | None = None - ) -> _Match[str] | None: ... - @overload + def search( - self: _Regexp[bytes], - text: bytes, - pos: int | None = None, - endpos: int | None = None, - ) -> _Match[bytes] | None: ... - @overload - def match( - self: _Regexp[str], text: str, pos: int | None = None, endpos: int | None = None - ) -> _Match[str] | None: ... - @overload + self, text: AnyStr, pos: int | None = None, endpos: int | None = None + ) -> _Match[AnyStr] | None: ... + def match( - self: _Regexp[bytes], - text: bytes, - pos: int | None = None, - endpos: int | None = None, - ) -> _Match[bytes] | None: ... - @overload - def fullmatch( - self: _Regexp[str], text: str, pos: int | None = None, endpos: int | None = None - ) -> _Match[str] | None: ... - @overload + self, text: AnyStr, pos: int | None = None, endpos: int | None = None + ) -> _Match[AnyStr] | None: ... + def fullmatch( - self: _Regexp[bytes], - text: bytes, - pos: int | None = None, - endpos: int | None = None, - ) -> _Match[bytes] | None: ... - @overload - def finditer( - self: _Regexp[str], text: str, pos: int | None = None, endpos: int | None = None - ) -> Iterator[_Match[str]]: ... - @overload + self, text: str, pos: int | None = None, endpos: int | None = None + ) -> _Match[AnyStr] | None: ... + def finditer( - self: _Regexp[bytes], - text: bytes, - pos: int | None = None, - endpos: int | None = None, - ) -> Iterator[_Match[bytes]]: ... - @overload - def findall( - self: _Regexp[str], text: str, pos: int | None = None, endpos: int | None = None - ) -> list[str]: ... + self, text: AnyStr, pos: int | None = None, endpos: int | None = None + ) -> Iterator[_Match[AnyStr]]: ... + @overload def findall( - self: _Regexp[bytes], - text: bytes, - pos: int | None = None, - endpos: int | None = None, - ) -> list[bytes]: ... - @overload - def split(self: _Regexp[str], text: str, maxsplit: int = 0) -> list[str]: ... - @overload - def split(self: _Regexp[bytes], text: bytes, maxsplit: int = 0) -> list[bytes]: ... - @overload - def subn( - self: _Regexp[str], repl: str, text: str, count: int = 0 - ) -> tuple[str, int]: ... - @overload - def subn( - self: _Regexp[bytes], repl: bytes, text: bytes, count: int = 0 - ) -> tuple[bytes, int]: ... + self, text: AnyStr, pos: int | None = None, endpos: int | None = None + ) -> list[AnyStr]: ... + + def split(self, text: AnyStr, maxsplit: int = 0) -> list[AnyStr]: ... + + def subn(self, repl: str, text: AnyStr, count: int = 0) -> tuple[AnyStr, int]: ... + @overload - def sub(self: _Regexp[str], repl: str, text: str, count: int = 0) -> str: ... + def sub(self, repl: AnyStr, text: str, count: int = 0) -> AnyStr: ... @overload - def sub( - self: _Regexp[bytes], repl: bytes, text: bytes, count: int = 0 - ) -> bytes: ... + def sub(self, repl: _Match[AnyStr], text: bytes, count: int = 0) -> AnyStr: ... + @property def pattern(self) -> AnyStr: ... @property From ff3c4812902b5ec82ecf098bd7cdfc7ae4834b90 Mon Sep 17 00:00:00 2001 From: Trim21 Date: Sat, 5 Oct 2024 01:20:55 +0800 Subject: [PATCH 2/3] fix callable as repl --- src/re2-stubs/__init__.pyi | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/re2-stubs/__init__.pyi b/src/re2-stubs/__init__.pyi index efe0324..369b970 100644 --- a/src/re2-stubs/__init__.pyi +++ b/src/re2-stubs/__init__.pyi @@ -1,3 +1,4 @@ +from _collections_abc import Callable from typing import AnyStr, Generic, Iterator, Literal, TypeVar, overload # References: @@ -127,7 +128,7 @@ class _Regexp(Generic[AnyStr]): ) -> _Match[AnyStr] | None: ... def fullmatch( - self, text: str, pos: int | None = None, endpos: int | None = None + self, text: AnyStr, pos: int | None = None, endpos: int | None = None ) -> _Match[AnyStr] | None: ... def finditer( @@ -141,12 +142,12 @@ class _Regexp(Generic[AnyStr]): def split(self, text: AnyStr, maxsplit: int = 0) -> list[AnyStr]: ... - def subn(self, repl: str, text: AnyStr, count: int = 0) -> tuple[AnyStr, int]: ... + def subn(self, repl: AnyStr, text: AnyStr, count: int = 0) -> tuple[AnyStr, int]: ... @overload def sub(self, repl: AnyStr, text: str, count: int = 0) -> AnyStr: ... @overload - def sub(self, repl: _Match[AnyStr], text: bytes, count: int = 0) -> AnyStr: ... + def sub(self, repl: Callable[[_Match[AnyStr]], str], text: AnyStr, count: int = 0) -> AnyStr: ... @property def pattern(self) -> AnyStr: ... From 2d3fa87dad7936d12a553a7b91e7416958bf1725 Mon Sep 17 00:00:00 2001 From: Trim21 Date: Sat, 5 Oct 2024 05:38:29 +0800 Subject: [PATCH 3/3] add overload only --- src/re2-stubs/__init__.pyi | 103 +++++++++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 27 deletions(-) diff --git a/src/re2-stubs/__init__.pyi b/src/re2-stubs/__init__.pyi index 369b970..c4747da 100644 --- a/src/re2-stubs/__init__.pyi +++ b/src/re2-stubs/__init__.pyi @@ -1,5 +1,4 @@ -from _collections_abc import Callable -from typing import AnyStr, Generic, Iterator, Literal, TypeVar, overload +from typing import AnyStr, Generic, Iterator, Literal, TypeVar, overload, Callable # References: # - https://github.com/google/re2/blob/main/re2/re2.h and @@ -100,7 +99,7 @@ def subn( @overload def sub( pattern: _Regexp[str] | str, - repl: str, + repl: str | Callable[[_Match[str]], str], text: str, count: int = 0, options: Options | None = None, @@ -108,7 +107,7 @@ def sub( @overload def sub( pattern: _Regexp[bytes] | bytes, - repl: bytes, + repl: bytes | Callable[[_Match[bytes]], bytes], text: bytes, count: int = 0, options: Options | None = None, @@ -118,37 +117,87 @@ def purge() -> None: ... class _Regexp(Generic[AnyStr]): def __init__(self, pattern: AnyStr, options: Options) -> None: ... - + @overload def search( - self, text: AnyStr, pos: int | None = None, endpos: int | None = None - ) -> _Match[AnyStr] | None: ... - + self: _Regexp[str], text: str, pos: int | None = None, endpos: int | None = None + ) -> _Match[str] | None: ... + @overload + def search( + self: _Regexp[bytes], + text: bytes, + pos: int | None = None, + endpos: int | None = None, + ) -> _Match[bytes] | None: ... + @overload def match( - self, text: AnyStr, pos: int | None = None, endpos: int | None = None - ) -> _Match[AnyStr] | None: ... - + self: _Regexp[str], text: str, pos: int | None = None, endpos: int | None = None + ) -> _Match[str] | None: ... + @overload + def match( + self: _Regexp[bytes], + text: bytes, + pos: int | None = None, + endpos: int | None = None, + ) -> _Match[bytes] | None: ... + @overload def fullmatch( - self, text: AnyStr, pos: int | None = None, endpos: int | None = None - ) -> _Match[AnyStr] | None: ... - + self: _Regexp[str], text: str, pos: int | None = None, endpos: int | None = None + ) -> _Match[str] | None: ... + @overload + def fullmatch( + self: _Regexp[bytes], + text: bytes, + pos: int | None = None, + endpos: int | None = None, + ) -> _Match[bytes] | None: ... + @overload def finditer( - self, text: AnyStr, pos: int | None = None, endpos: int | None = None - ) -> Iterator[_Match[AnyStr]]: ... - + self: _Regexp[str], text: str, pos: int | None = None, endpos: int | None = None + ) -> Iterator[_Match[str]]: ... + @overload + def finditer( + self: _Regexp[bytes], + text: bytes, + pos: int | None = None, + endpos: int | None = None, + ) -> Iterator[_Match[bytes]]: ... @overload def findall( - self, text: AnyStr, pos: int | None = None, endpos: int | None = None - ) -> list[AnyStr]: ... - - def split(self, text: AnyStr, maxsplit: int = 0) -> list[AnyStr]: ... - - def subn(self, repl: AnyStr, text: AnyStr, count: int = 0) -> tuple[AnyStr, int]: ... - + self: _Regexp[str], text: str, pos: int | None = None, endpos: int | None = None + ) -> list[str]: ... @overload - def sub(self, repl: AnyStr, text: str, count: int = 0) -> AnyStr: ... + def findall( + self: _Regexp[bytes], + text: bytes, + pos: int | None = None, + endpos: int | None = None, + ) -> list[bytes]: ... @overload - def sub(self, repl: Callable[[_Match[AnyStr]], str], text: AnyStr, count: int = 0) -> AnyStr: ... - + def split(self: _Regexp[str], text: str, maxsplit: int = 0) -> list[str]: ... + @overload + def split(self: _Regexp[bytes], text: bytes, maxsplit: int = 0) -> list[bytes]: ... + @overload + def subn( + self: _Regexp[str], repl: str, text: str, count: int = 0 + ) -> tuple[str, int]: ... + @overload + def subn( + self: _Regexp[bytes], repl: bytes, text: bytes, count: int = 0 + ) -> tuple[bytes, int]: ... + @overload + def sub( + self: _Regexp[str], + repl: str | Callable[[_Match[str]], str], + text: str, + count: int = 0, + ) -> str: ... + @overload + def sub( + self: _Regexp[bytes], + repl: bytes | Callable[[_Match[bytes]], bytes], + text: bytes, + count: int = 0, + ) -> bytes: ... @property def pattern(self) -> AnyStr: ... @property