From 9e6fda08b12f60d3981236200885b0c95958ff86 Mon Sep 17 00:00:00 2001 From: David Brownman Date: Mon, 7 Mar 2022 12:44:32 -0800 Subject: [PATCH 1/3] add overload to tuple.__new__ to better express an empty tuple --- stdlib/builtins.pyi | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 9ce6959a1abf..9a4b1636eef3 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -784,6 +784,9 @@ class slice: def indices(self, __len: SupportsIndex) -> tuple[int, int, int]: ... class tuple(Sequence[_T_co], Generic[_T_co]): + @overload + def __new__(cls) -> tuple[()]: ... + @overload def __new__(cls: type[Self], __iterable: Iterable[_T_co] = ...) -> Self: ... def __len__(self) -> int: ... def __contains__(self, __x: object) -> bool: ... From 1514582cfa6f13a4d05df4f5c1f970398447bf6a Mon Sep 17 00:00:00 2001 From: David Brownman Date: Mon, 7 Mar 2022 14:01:23 -0800 Subject: [PATCH 2/3] remove default from iterable in one of the call signatures --- stdlib/builtins.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 9a4b1636eef3..f10e625bd2c2 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -787,7 +787,7 @@ class tuple(Sequence[_T_co], Generic[_T_co]): @overload def __new__(cls) -> tuple[()]: ... @overload - def __new__(cls: type[Self], __iterable: Iterable[_T_co] = ...) -> Self: ... + def __new__(cls: type[Self], __iterable: Iterable[_T_co]) -> Self: ... def __len__(self) -> int: ... def __contains__(self, __x: object) -> bool: ... @overload From d556d1701714f615134de20d7f5ee8bebf7db884 Mon Sep 17 00:00:00 2001 From: David Brownman Date: Mon, 7 Mar 2022 23:53:32 -0800 Subject: [PATCH 3/3] reorder overloads; add comment --- stdlib/builtins.pyi | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index f10e625bd2c2..5c3b7ad41734 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -784,10 +784,12 @@ class slice: def indices(self, __len: SupportsIndex) -> tuple[int, int, int]: ... class tuple(Sequence[_T_co], Generic[_T_co]): - @overload - def __new__(cls) -> tuple[()]: ... + # overloads are ordered this way to pass `isinstance` checks + # see: https://github.com/python/typeshed/pull/7454#issuecomment-1061490888 @overload def __new__(cls: type[Self], __iterable: Iterable[_T_co]) -> Self: ... + @overload + def __new__(cls) -> tuple[()]: ... def __len__(self) -> int: ... def __contains__(self, __x: object) -> bool: ... @overload