From c9988cfe91caf3567b6781776003fe8a9359b664 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 16 Nov 2021 21:01:20 +0000 Subject: [PATCH 1/5] Initial commit --- stdlib/builtins.pyi | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 895673e898c0..c88e3ff14563 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -220,7 +220,11 @@ class int: @overload def __pow__(self, __x: int, __modulo: Literal[0]) -> NoReturn: ... @overload - def __pow__(self, __x: Literal[2, 3, 4, 5], __modulo: int | None = ...) -> int: ... + def __pow__( + self, + __x: Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28], + __modulo: int | None = ... + ) -> int: ... # positive x -> int; negative x -> float # return type must be Any as `int | float` causes too many false-positive errors @overload @@ -1282,6 +1286,9 @@ _M = TypeVar("_M", contravariant=True) class _SupportsPow2(Protocol[_E, _T_co]): def __pow__(self, __other: _E) -> _T_co: ... +class _SupportsPow3NoneOnly(Protocol[_E, _T_co]): + def __pow__(self, __other: _E, __modulo: None = ...) -> _T_co: ... + class _SupportsPow3(Protocol[_E, _M, _T_co]): def __pow__(self, __other: _E, __modulo: _M) -> _T_co: ... From e5372d687bc8398918808134818c3b27181a2ee9 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 16 Nov 2021 23:31:31 +0000 Subject: [PATCH 2/5] Improve `pow` --- stdlib/builtins.pyi | 51 ++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index c88e3ff14563..c674dedcfa62 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -179,6 +179,10 @@ class super(object): @overload def __init__(self) -> None: ... +_PositiveInteger = Literal[ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 +] + class int: @overload def __new__(cls: Type[_T], __x: str | bytes | SupportsInt | SupportsIndex | _SupportsTrunc = ...) -> _T: ... @@ -220,15 +224,13 @@ class int: @overload def __pow__(self, __x: int, __modulo: Literal[0]) -> NoReturn: ... @overload - def __pow__( - self, - __x: Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28], - __modulo: int | None = ... - ) -> int: ... + def __pow__(self, __x: int, __modulo: int) -> int: ... + @overload + def __pow__(self, __x: _PositiveInteger, __modulo: None = ...) -> int: ... # positive x -> int; negative x -> float # return type must be Any as `int | float` causes too many false-positive errors @overload - def __pow__(self, __x: int, __modulo: int | None = ...) -> Any: ... + def __pow__(self, __x: int, __modulo: None = ...) -> Any: ... def __rpow__(self, __x: int, __mod: int | None = ...) -> Any: ... def __and__(self, __n: int) -> int: ... def __or__(self, __n: int) -> int: ... @@ -1292,41 +1294,56 @@ class _SupportsPow3NoneOnly(Protocol[_E, _T_co]): class _SupportsPow3(Protocol[_E, _M, _T_co]): def __pow__(self, __other: _E, __modulo: _M) -> _T_co: ... +_SupportsSomeKindOfPow = _SupportsPow2[Any, Any] | _SupportsPow3NoneOnly[Any, Any] | _SupportsPow3[Any, Any, Any] + if sys.version_info >= (3, 8): @overload def pow(base: int, exp: int, mod: Literal[0]) -> NoReturn: ... + @overload + def pow(base: int, exp: int, mod: int) -> int: ... + @overload + def pow(base: int, exp: _PositiveInteger, mod: None = ...) -> int: ... # int base & positive-int exp -> int; int base & negative-int exp -> float # return type must be Any as `int | float` causes too many false-positive errors @overload def pow(base: int, exp: int, mod: None = ...) -> Any: ... @overload - def pow(base: int, exp: int, mod: int) -> int: ... - @overload def pow(base: float, exp: int, mod: None = ...) -> float: ... # float base & float exp could return float or complex - # return type must be Any as `float | complex` causes too many false-positive errors + # return type must be Any (same as complex base, complex exp), + # as `float | complex` causes too many false-positive errors + @overload + def pow(base: complex, exp: complex | _SupportsSomeKindOfPow, mod: None = ...) -> Any: ... + @overload + def pow(base: _SupportsPow2[_E, _T_co], exp: _E, mod: None = ...) -> _T_co: ... @overload - def pow(base: float, exp: float, mod: None = ...) -> Any: ... + def pow(base: _SupportsPow3NoneOnly[_E, _T_co], exp: _E, mod: None = ...) -> _T_co: ... @overload - def pow(base: _SupportsPow2[_E, _T_co], exp: _E) -> _T_co: ... + def pow(base: _SupportsPow3[_E, _M, _T_co], exp: _E, mod: _M = ...) -> _T_co: ... @overload - def pow(base: _SupportsPow3[_E, _M, _T_co], exp: _E, mod: _M) -> _T_co: ... + def pow(base: _SupportsSomeKindOfPow, exp: complex, mod: None = ...) -> complex: ... else: @overload def pow(__base: int, __exp: int, __mod: Literal[0]) -> NoReturn: ... @overload - def pow(__base: int, __exp: int, __mod: None = ...) -> Any: ... - @overload def pow(__base: int, __exp: int, __mod: int) -> int: ... @overload + def pow(__base: int, __exp: _PositiveInteger, __mod: None = ...) -> int: ... + @overload + def pow(__base: int, __exp: int, __mod: None = ...) -> Any: ... + @overload def pow(__base: float, __exp: int, __mod: None = ...) -> float: ... @overload - def pow(__base: float, __exp: float, __mod: None = ...) -> Any: ... + def pow(__base: complex, __exp: complex | _SupportsSomeKindOfPow, __mod: None = ...) -> Any: ... + @overload + def pow(__base: _SupportsPow2[_E, _T_co], __exp: _E, __mod: None = ...) -> _T_co: ... + @overload + def pow(__base: _SupportsPow3NoneOnly[_E, _T_co], __exp: _E, __mod: None = ...) -> _T_co: ... @overload - def pow(__base: _SupportsPow2[_E, _T_co], __exp: _E) -> _T_co: ... + def pow(__base: _SupportsPow3[_E, _M, _T_co], __exp: _E, __mod: _M = ...) -> _T_co: ... @overload - def pow(__base: _SupportsPow3[_E, _M, _T_co], __exp: _E, __mod: _M) -> _T_co: ... + def pow(__base: _SupportsSomeKindOfPow, __exp: complex, __mod: None = ...) -> complex: ... def quit(code: object = ...) -> NoReturn: ... From ee79994a26254dbf4762df872f291002583439f2 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 16 Nov 2021 23:45:41 +0000 Subject: [PATCH 3/5] Improve special-casing for `complex` --- stdlib/builtins.pyi | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index c674dedcfa62..7c2c119cfe15 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1313,7 +1313,9 @@ if sys.version_info >= (3, 8): # return type must be Any (same as complex base, complex exp), # as `float | complex` causes too many false-positive errors @overload - def pow(base: complex, exp: complex | _SupportsSomeKindOfPow, mod: None = ...) -> Any: ... + def pow(base: float, exp: complex | _SupportsSomeKindOfPow, mod: None = ...) -> Any: ... + @overload + def pow(base: complex, exp: complex | _SupportsSomeKindOfPow, mod: None = ...) -> complex: ... @overload def pow(base: _SupportsPow2[_E, _T_co], exp: _E, mod: None = ...) -> _T_co: ... @overload @@ -1321,6 +1323,8 @@ if sys.version_info >= (3, 8): @overload def pow(base: _SupportsPow3[_E, _M, _T_co], exp: _E, mod: _M = ...) -> _T_co: ... @overload + def pow(base: _SupportsSomeKindOfPow, exp: float, mod: None = ...) -> Any: ... + @overload def pow(base: _SupportsSomeKindOfPow, exp: complex, mod: None = ...) -> complex: ... else: @@ -1335,7 +1339,9 @@ else: @overload def pow(__base: float, __exp: int, __mod: None = ...) -> float: ... @overload - def pow(__base: complex, __exp: complex | _SupportsSomeKindOfPow, __mod: None = ...) -> Any: ... + def pow(__base: float, __exp: complex | _SupportsSomeKindOfPow, __mod: None = ...) -> Any: ... + @overload + def pow(__base: complex, __exp: complex | _SupportsSomeKindOfPow, __mod: None = ...) -> complex: ... @overload def pow(__base: _SupportsPow2[_E, _T_co], __exp: _E, __mod: None = ...) -> _T_co: ... @overload @@ -1343,6 +1349,8 @@ else: @overload def pow(__base: _SupportsPow3[_E, _M, _T_co], __exp: _E, __mod: _M = ...) -> _T_co: ... @overload + def pow(__base: _SupportsSomeKindOfPow, __exp: float, __mod: None = ...) -> Any: ... + @overload def pow(__base: _SupportsSomeKindOfPow, __exp: complex, __mod: None = ...) -> complex: ... def quit(code: object = ...) -> NoReturn: ... From 7ff423f0770252e247ba33441107f88369ea0c68 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 17 Nov 2021 00:33:54 +0000 Subject: [PATCH 4/5] Fix mypy errors --- stdlib/builtins.pyi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 7c2c119cfe15..98d59b25d676 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1294,7 +1294,7 @@ class _SupportsPow3NoneOnly(Protocol[_E, _T_co]): class _SupportsPow3(Protocol[_E, _M, _T_co]): def __pow__(self, __other: _E, __modulo: _M) -> _T_co: ... -_SupportsSomeKindOfPow = _SupportsPow2[Any, Any] | _SupportsPow3NoneOnly[Any, Any] | _SupportsPow3[Any, Any, Any] +_SupportsSomeKindOfPow = Union[_SupportsPow2[Any, Any], _SupportsPow3NoneOnly[Any, Any], _SupportsPow3[Any, Any, Any]] if sys.version_info >= (3, 8): @overload @@ -1302,7 +1302,7 @@ if sys.version_info >= (3, 8): @overload def pow(base: int, exp: int, mod: int) -> int: ... @overload - def pow(base: int, exp: _PositiveInteger, mod: None = ...) -> int: ... + def pow(base: int, exp: _PositiveInteger, mod: None = ...) -> int: ... # type: ignore[misc] # int base & positive-int exp -> int; int base & negative-int exp -> float # return type must be Any as `int | float` causes too many false-positive errors @overload @@ -1333,7 +1333,7 @@ else: @overload def pow(__base: int, __exp: int, __mod: int) -> int: ... @overload - def pow(__base: int, __exp: _PositiveInteger, __mod: None = ...) -> int: ... + def pow(__base: int, __exp: _PositiveInteger, __mod: None = ...) -> int: ... # type: ignore[misc] @overload def pow(__base: int, __exp: int, __mod: None = ...) -> Any: ... @overload From 14a8b3dd7deba05307cb40453f3f364a910eaa81 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 17 Nov 2021 01:38:50 +0000 Subject: [PATCH 5/5] Heard you liked overloads, so I got you more overloads --- stdlib/builtins.pyi | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 98d59b25d676..97c15c218e69 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -179,9 +179,8 @@ class super(object): @overload def __init__(self) -> None: ... -_PositiveInteger = Literal[ - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 -] +_PositiveInteger = Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25] +_NegativeInteger = Literal[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20] class int: @overload @@ -227,6 +226,8 @@ class int: def __pow__(self, __x: int, __modulo: int) -> int: ... @overload def __pow__(self, __x: _PositiveInteger, __modulo: None = ...) -> int: ... + @overload + def __pow__(self, __x: _NegativeInteger, __modulo: None = ...) -> float: ... # positive x -> int; negative x -> float # return type must be Any as `int | float` causes too many false-positive errors @overload @@ -1303,6 +1304,8 @@ if sys.version_info >= (3, 8): def pow(base: int, exp: int, mod: int) -> int: ... @overload def pow(base: int, exp: _PositiveInteger, mod: None = ...) -> int: ... # type: ignore[misc] + @overload + def pow(base: int, exp: _NegativeInteger, mod: None = ...) -> float: ... # type: ignore[misc] # int base & positive-int exp -> int; int base & negative-int exp -> float # return type must be Any as `int | float` causes too many false-positive errors @overload @@ -1335,6 +1338,8 @@ else: @overload def pow(__base: int, __exp: _PositiveInteger, __mod: None = ...) -> int: ... # type: ignore[misc] @overload + def pow(__base: int, __exp: _NegativeInteger, __mod: None = ...) -> float: ... # type: ignore[misc] + @overload def pow(__base: int, __exp: int, __mod: None = ...) -> Any: ... @overload def pow(__base: float, __exp: int, __mod: None = ...) -> float: ...