Add more overloads to the re stubs to help out pyright (#9592)

This commit is contained in:
Alex Waygood
2023-01-28 02:32:50 +00:00
committed by GitHub
parent 18c46619f5
commit 7f986bdf85
3 changed files with 51 additions and 17 deletions

View File

@@ -0,0 +1,26 @@
from __future__ import annotations
import mmap
import re
import typing as t
from typing_extensions import assert_type
def check_search(str_pat: re.Pattern[str], bytes_pat: re.Pattern[bytes]) -> None:
assert_type(str_pat.search("x"), t.Optional[t.Match[str]])
assert_type(bytes_pat.search(b"x"), t.Optional[t.Match[bytes]])
assert_type(bytes_pat.search(bytearray(b"x")), t.Optional[t.Match[bytes]])
assert_type(bytes_pat.search(mmap.mmap(0, 10)), t.Optional[t.Match[bytes]])
def check_search_with_AnyStr(pattern: re.Pattern[t.AnyStr], string: t.AnyStr) -> re.Match[t.AnyStr]:
"""See issue #9591"""
match = pattern.search(string)
if match is None:
raise ValueError(f"'{string!r}' does not match {pattern!r}")
return match
def check_no_ReadableBuffer_false_negatives() -> None:
re.compile("foo").search(bytearray(b"foo")) # type: ignore
re.compile("foo").search(mmap.mmap(0, 10)) # type: ignore

View File

@@ -1,10 +0,0 @@
from __future__ import annotations
from typing import Match, Optional, Pattern
from typing_extensions import assert_type
def test_search(str_pat: Pattern[str], bytes_pat: Pattern[bytes]) -> None:
assert_type(str_pat.search("x"), Optional[Match[str]])
assert_type(bytes_pat.search(b"x"), Optional[Match[bytes]])
assert_type(bytes_pat.search(bytearray(b"x")), Optional[Match[bytes]])