From 6a2bd1fce029696f4703d2ee0fc658441ab8116e Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Sat, 15 Jul 2023 22:49:10 +0200 Subject: [PATCH 1/4] Make bool.__new__ more specific --- stdlib/builtins.pyi | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index ea917bddb799..647923cf091e 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -898,8 +898,19 @@ class memoryview(Sequence[int]): def __buffer__(self, __flags: int) -> memoryview: ... def __release_buffer__(self, __buffer: memoryview) -> None: ... +class _Truthy(Protocol): + def __bool__(self) -> Literal[True]: ... + +class _Falsy(Protocol): + def __bool__(self) -> Literal[False]: ... + @final class bool(int): + @overload + def __new__(cls, __o: _Truthy) -> Literal[True]: ... + @overload + def __new__(cls, __o: _Falsy) -> Literal[False]: ... + @overload def __new__(cls, __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). From d62cae40c79b45c545a8b88f4332094225672116 Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Wed, 19 Jul 2023 11:24:35 +0100 Subject: [PATCH 2/4] Add no-arg literal cases --- stdlib/builtins.pyi | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 647923cf091e..e11b06025b6d 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -214,6 +214,8 @@ _NegativeInteger: TypeAlias = Literal[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, - _LiteralInteger = _PositiveInteger | _NegativeInteger | Literal[0] # noqa: Y026 # TODO: Use TypeAlias once mypy bugs are fixed class int: + @overload + def __new__(cls) -> Literal[0]: ... @overload def __new__(cls, __x: str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> Self: ... @overload @@ -432,6 +434,8 @@ class _TranslateTable(Protocol): def __getitem__(self, __key: int) -> str | int | None: ... class str(Sequence[str]): + @overload + def __new__(cls) -> Literal[""]: ... @overload def __new__(cls, object: object = ...) -> Self: ... @overload @@ -612,6 +616,8 @@ class str(Sequence[str]): def __getnewargs__(self) -> tuple[str]: ... class bytes(Sequence[int]): + @overload + def __new__(cls) -> Literal[b""]: ... @overload def __new__(cls, __o: Iterable[SupportsIndex] | SupportsIndex | SupportsBytes | ReadableBuffer) -> Self: ... @overload @@ -906,6 +912,8 @@ class _Falsy(Protocol): @final class bool(int): + @overload + def __new__(cls) -> Literal[False]: ... @overload def __new__(cls, __o: _Truthy) -> Literal[True]: ... @overload From d2541c92df74c5cea63dac7c877f4506153eb239 Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Wed, 19 Jul 2023 11:44:23 +0100 Subject: [PATCH 3/4] Remove changes to str, int and bytes --- stdlib/builtins.pyi | 6 ------ 1 file changed, 6 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index e11b06025b6d..149b1146f91a 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -214,8 +214,6 @@ _NegativeInteger: TypeAlias = Literal[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, - _LiteralInteger = _PositiveInteger | _NegativeInteger | Literal[0] # noqa: Y026 # TODO: Use TypeAlias once mypy bugs are fixed class int: - @overload - def __new__(cls) -> Literal[0]: ... @overload def __new__(cls, __x: str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> Self: ... @overload @@ -434,8 +432,6 @@ class _TranslateTable(Protocol): def __getitem__(self, __key: int) -> str | int | None: ... class str(Sequence[str]): - @overload - def __new__(cls) -> Literal[""]: ... @overload def __new__(cls, object: object = ...) -> Self: ... @overload @@ -616,8 +612,6 @@ class str(Sequence[str]): def __getnewargs__(self) -> tuple[str]: ... class bytes(Sequence[int]): - @overload - def __new__(cls) -> Literal[b""]: ... @overload def __new__(cls, __o: Iterable[SupportsIndex] | SupportsIndex | SupportsBytes | ReadableBuffer) -> Self: ... @overload From 3b645bf7ec3f4021aebb35a100ad0b24da50aa7d Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Wed, 19 Jul 2023 11:52:45 +0100 Subject: [PATCH 4/4] Simplify overloads Co-authored-by: Alex Waygood --- stdlib/builtins.pyi | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 149b1146f91a..ec077976610f 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -906,14 +906,12 @@ class _Falsy(Protocol): @final class bool(int): - @overload - def __new__(cls) -> Literal[False]: ... @overload def __new__(cls, __o: _Truthy) -> Literal[True]: ... @overload - def __new__(cls, __o: _Falsy) -> Literal[False]: ... + def __new__(cls, __o: _Falsy = ...) -> Literal[False]: ... @overload - def __new__(cls, __o: object = ...) -> Self: ... + def __new__(cls, __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