From 3e37029bfec1d01584b91c2fc53818f52a849dbe Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 16 May 2016 11:25:59 -0700 Subject: [PATCH] Almost all re functions take a compiled pattern. (Even re.compile()!) (#203) * Almost all re functions take a compiled pattern. (Even re.compile()!) Fixes #188 Note: I'm using AnyStr so that the type of string used for pattern and for the rest of the arguments must match. This is not 100% correct, since Python 2 sometimes allows mixed types. But sometimes it doesn't, depending on the values (e.g. non-ASCII bytes), and Python 3 always insists on matching, so I think this is actually a good idea. * Same treatment for stdlib/3/re.pyi. --- stdlib/2.7/re.pyi | 50 ++++++++++++++++++++++++++++------ stdlib/3/re.pyi | 68 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 97 insertions(+), 21 deletions(-) diff --git a/stdlib/2.7/re.pyi b/stdlib/2.7/re.pyi index 79b3a1017..7c47bcd89 100644 --- a/stdlib/2.7/re.pyi +++ b/stdlib/2.7/re.pyi @@ -28,22 +28,43 @@ TEMPLATE = 0 class error(Exception): ... +@overload def compile(pattern: AnyStr, flags: int = ...) -> Pattern[AnyStr]: ... -def search(pattern: AnyStr, string: AnyStr, - flags: int = ...) -> Match[AnyStr]: ... -def match(pattern: AnyStr, string: AnyStr, - flags: int = ...) -> Match[AnyStr]: ... -def split(pattern: AnyStr, string: AnyStr, maxsplit: int = ..., - flags: int = ...) -> List[AnyStr]: ... -def findall(pattern: AnyStr, string: AnyStr, - flags: int = ...) -> List[AnyStr]: ... +@overload +def compile(pattern: Pattern[AnyStr], flags: int = ...) -> Pattern[AnyStr]: ... + +@overload +def search(pattern: AnyStr, string: AnyStr, flags: int = ...) -> Match[AnyStr]: ... +@overload +def search(pattern: Pattern[AnyStr], string: AnyStr, flags: int = ...) -> Match[AnyStr]: ... + +@overload +def match(pattern: AnyStr, string: AnyStr, flags: int = ...) -> Match[AnyStr]: ... +@overload +def match(pattern: Pattern[AnyStr], string: AnyStr, flags: int = ...) -> Match[AnyStr]: ... + +@overload +def split(pattern: AnyStr, string: AnyStr, + maxsplit: int = ..., flags: int = ...) -> List[AnyStr]: ... +@overload +def split(pattern: Pattern[AnyStr], string: AnyStr, + maxsplit: int = ..., flags: int = ...) -> List[AnyStr]: ... + +@overload +def findall(pattern: AnyStr, string: AnyStr, flags: int = ...) -> List[AnyStr]: ... +@overload +def findall(pattern: Pattern[AnyStr], string: AnyStr, flags: int = ...) -> List[AnyStr]: ... # Return an iterator yielding match objects over all non-overlapping matches # for the RE pattern in string. The string is scanned left-to-right, and # matches are returned in the order found. Empty matches are included in the # result unless they touch the beginning of another match. +@overload def finditer(pattern: AnyStr, string: AnyStr, flags: int = ...) -> Iterator[Match[AnyStr]]: ... +@overload +def finditer(pattern: Pattern[AnyStr], string: AnyStr, + flags: int = ...) -> Iterator[Match[AnyStr]]: ... @overload def sub(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ..., @@ -51,6 +72,12 @@ def sub(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ..., @overload def sub(pattern: AnyStr, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: int = ...) -> AnyStr: ... +@overload +def sub(pattern: Pattern[AnyStr], repl: AnyStr, string: AnyStr, count: int = ..., + flags: int = ...) -> AnyStr: ... +@overload +def sub(pattern: Pattern[AnyStr], repl: Callable[[Match[AnyStr]], AnyStr], + string: AnyStr, count: int = ..., flags: int = ...) -> AnyStr: ... @overload def subn(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ..., @@ -59,6 +86,13 @@ def subn(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ..., def subn(pattern: AnyStr, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: int = ...) -> Tuple[AnyStr, int]: ... +@overload +def subn(pattern: Pattern[AnyStr], repl: AnyStr, string: AnyStr, count: int = ..., + flags: int = ...) -> Tuple[AnyStr, int]: ... +@overload +def subn(pattern: Pattern[AnyStr], repl: Callable[[Match[AnyStr]], AnyStr], + string: AnyStr, count: int = ..., + flags: int = ...) -> Tuple[AnyStr, int]: ... def escape(string: AnyStr) -> AnyStr: ... diff --git a/stdlib/3/re.pyi b/stdlib/3/re.pyi index 8cd8bbac9..8369133d7 100644 --- a/stdlib/3/re.pyi +++ b/stdlib/3/re.pyi @@ -6,7 +6,7 @@ # and http://hg.python.org/cpython/file/618ea5612e83/Lib/re.py from typing import ( - List, Iterator, Callable, Tuple, Sequence, Dict, Union, + List, Iterator, overload, Callable, Tuple, Sequence, Dict, Generic, AnyStr, Match, Pattern ) @@ -29,29 +29,71 @@ UNICODE = 0 class error(Exception): ... +@overload def compile(pattern: AnyStr, flags: int = ...) -> Pattern[AnyStr]: ... -def search(pattern: AnyStr, string: AnyStr, - flags: int = ...) -> Match[AnyStr]: ... -def match(pattern: AnyStr, string: AnyStr, - flags: int = ...) -> Match[AnyStr]: ... -def split(pattern: AnyStr, string: AnyStr, maxsplit: int = ..., - flags: int = ...) -> List[AnyStr]: ... -def findall(pattern: AnyStr, string: AnyStr, - flags: int = ...) -> List[AnyStr]: ... +@overload +def compile(pattern: Pattern[AnyStr], flags: int = ...) -> Pattern[AnyStr]: ... + +@overload +def search(pattern: AnyStr, string: AnyStr, flags: int = ...) -> Match[AnyStr]: ... +@overload +def search(pattern: Pattern[AnyStr], string: AnyStr, flags: int = ...) -> Match[AnyStr]: ... + +@overload +def match(pattern: AnyStr, string: AnyStr, flags: int = ...) -> Match[AnyStr]: ... +@overload +def match(pattern: Pattern[AnyStr], string: AnyStr, flags: int = ...) -> Match[AnyStr]: ... + +@overload +def split(pattern: AnyStr, string: AnyStr, + maxsplit: int = ..., flags: int = ...) -> List[AnyStr]: ... +@overload +def split(pattern: Pattern[AnyStr], string: AnyStr, + maxsplit: int = ..., flags: int = ...) -> List[AnyStr]: ... + +@overload +def findall(pattern: AnyStr, string: AnyStr, flags: int = ...) -> List[AnyStr]: ... +@overload +def findall(pattern: Pattern[AnyStr], string: AnyStr, flags: int = ...) -> List[AnyStr]: ... # Return an iterator yielding match objects over all non-overlapping matches # for the RE pattern in string. The string is scanned left-to-right, and # matches are returned in the order found. Empty matches are included in the # result unless they touch the beginning of another match. +@overload def finditer(pattern: AnyStr, string: AnyStr, flags: int = ...) -> Iterator[Match[AnyStr]]: ... +@overload +def finditer(pattern: Pattern[AnyStr], string: AnyStr, + flags: int = ...) -> Iterator[Match[AnyStr]]: ... -def sub(pattern: AnyStr, repl: Union[AnyStr, Callable[[Match[AnyStr]], AnyStr]], +@overload +def sub(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ..., + flags: int = ...) -> AnyStr: ... +@overload +def sub(pattern: AnyStr, repl: Callable[[Match[AnyStr]], AnyStr], + string: AnyStr, count: int = ..., flags: int = ...) -> AnyStr: ... +@overload +def sub(pattern: Pattern[AnyStr], repl: AnyStr, string: AnyStr, count: int = ..., + flags: int = ...) -> AnyStr: ... +@overload +def sub(pattern: Pattern[AnyStr], repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ..., flags: int = ...) -> AnyStr: ... -def subn(pattern: AnyStr, repl: Union[AnyStr, Callable[[Match[AnyStr]], AnyStr]], - string: AnyStr, count: int = ..., flags: int = ...) -> Tuple[AnyStr, int]: - ... +@overload +def subn(pattern: AnyStr, repl: AnyStr, string: AnyStr, count: int = ..., + flags: int = ...) -> Tuple[AnyStr, int]: ... +@overload +def subn(pattern: AnyStr, repl: Callable[[Match[AnyStr]], AnyStr], + string: AnyStr, count: int = ..., + flags: int = ...) -> Tuple[AnyStr, int]: ... +@overload +def subn(pattern: Pattern[AnyStr], repl: AnyStr, string: AnyStr, count: int = ..., + flags: int = ...) -> Tuple[AnyStr, int]: ... +@overload +def subn(pattern: Pattern[AnyStr], repl: Callable[[Match[AnyStr]], AnyStr], + string: AnyStr, count: int = ..., + flags: int = ...) -> Tuple[AnyStr, int]: ... def escape(string: AnyStr) -> AnyStr: ...