From 51a3cf072cd82f1f163ee587a92a7feb25c23233 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Tue, 22 Feb 2022 12:29:39 +0100 Subject: [PATCH] Add comments about mypy limitation regarding TypeVar constraints (#7350) --- stdlib/builtins.pyi | 2 ++ stdlib/re.pyi | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index d5d4b46a9..b327deddc 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -725,6 +725,8 @@ class memoryview(Sized, Sequence[int]): @final class bool(int): def __new__(cls: type[Self], __o: object = ...) -> Self: ... + # The following overloads could be represented more elegantly with a TypeVar("_B", bool, int), + # however mypy has a bug regarding TypeVar constraints (https://github.com/python/mypy/issues/11880). @overload def __and__(self, __x: bool) -> bool: ... @overload diff --git a/stdlib/re.pyi b/stdlib/re.pyi index 01a60d170..5150bf267 100644 --- a/stdlib/re.pyi +++ b/stdlib/re.pyi @@ -51,6 +51,11 @@ if sys.version_info < (3, 7): # undocumented _pattern_type: type +# Type-wise these overloads are unnecessary, they could also be modeled using +# unions in the parameter types. However mypy has a bug regarding TypeVar +# constraints (https://github.com/python/mypy/issues/11880), +# which limits us here because AnyStr is a constrained TypeVar. + @overload def compile(pattern: AnyStr, flags: _FlagsType = ...) -> Pattern[AnyStr]: ... @overload