From d451794386e0cc2e3e61e979310e60e4d2278bac Mon Sep 17 00:00:00 2001 From: donbarbos Date: Sat, 10 Jan 2026 22:44:01 +0400 Subject: [PATCH 1/3] [sre_compile] Use TypeGuard for isstring Source: https://github.com/python/cpython/blob/aa8578dc54df2af9daa3353566359e602e5905cf/Lib/re/_compiler.py#L587 --- stdlib/sre_compile.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/sre_compile.pyi b/stdlib/sre_compile.pyi index d8f0b7937e99..9dd54c7a91a6 100644 --- a/stdlib/sre_compile.pyi +++ b/stdlib/sre_compile.pyi @@ -3,9 +3,10 @@ from sre_constants import * from sre_constants import _NamedIntConstant from sre_parse import SubPattern from typing import Any, Final +from typing_extensions import TypeGuard MAXCODE: Final[int] def dis(code: list[_NamedIntConstant]) -> None: ... -def isstring(obj: Any) -> bool: ... +def isstring(obj: object) -> TypeGuard[str | bytes]: ... def compile(p: str | bytes | SubPattern, flags: int = 0) -> Pattern[Any]: ... From da5179cbd94767211d06d42e51d8f35ed43c05f5 Mon Sep 17 00:00:00 2001 From: donbarbos Date: Sun, 11 Jan 2026 01:56:06 +0400 Subject: [PATCH 2/3] Retain knowledge about str,bytes types --- stdlib/sre_compile.pyi | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/stdlib/sre_compile.pyi b/stdlib/sre_compile.pyi index 9dd54c7a91a6..4c0a11a3cc76 100644 --- a/stdlib/sre_compile.pyi +++ b/stdlib/sre_compile.pyi @@ -2,11 +2,16 @@ from re import Pattern from sre_constants import * from sre_constants import _NamedIntConstant from sre_parse import SubPattern -from typing import Any, Final +from typing import Any, Final, Literal, overload from typing_extensions import TypeGuard MAXCODE: Final[int] def dis(code: list[_NamedIntConstant]) -> None: ... +@overload +def isstring(obj: str) -> Literal[True]: ... +@overload +def isstring(obj: bytes) -> Literal[True]: ... +@overload def isstring(obj: object) -> TypeGuard[str | bytes]: ... def compile(p: str | bytes | SubPattern, flags: int = 0) -> Pattern[Any]: ... From 51071a53bdd54e3ef4914be7f426b8c6ab49274b Mon Sep 17 00:00:00 2001 From: Semyon Moroz Date: Sat, 10 Jan 2026 22:03:21 +0000 Subject: [PATCH 3/3] merge into one overload --- stdlib/sre_compile.pyi | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/stdlib/sre_compile.pyi b/stdlib/sre_compile.pyi index 4c0a11a3cc76..e29d63d48591 100644 --- a/stdlib/sre_compile.pyi +++ b/stdlib/sre_compile.pyi @@ -9,9 +9,7 @@ MAXCODE: Final[int] def dis(code: list[_NamedIntConstant]) -> None: ... @overload -def isstring(obj: str) -> Literal[True]: ... -@overload -def isstring(obj: bytes) -> Literal[True]: ... +def isstring(obj: str | bytes) -> Literal[True]: ... @overload def isstring(obj: object) -> TypeGuard[str | bytes]: ... def compile(p: str | bytes | SubPattern, flags: int = 0) -> Pattern[Any]: ...