Skip to content

Commit 48cfe5d

Browse files
authored
Further improve pow (#6325)
1 parent 2f0969c commit 48cfe5d

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed

stdlib/builtins.pyi

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ class super(object):
176176
@overload
177177
def __init__(self) -> None: ...
178178

179+
_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]
180+
_NegativeInteger = Literal[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20]
181+
179182
class int:
180183
@overload
181184
def __new__(cls: Type[_T], __x: str | bytes | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> _T: ...
@@ -217,11 +220,15 @@ class int:
217220
@overload
218221
def __pow__(self, __x: int, __modulo: Literal[0]) -> NoReturn: ...
219222
@overload
220-
def __pow__(self, __x: Literal[2, 3, 4, 5], __modulo: int | None = ...) -> int: ...
223+
def __pow__(self, __x: int, __modulo: int) -> int: ...
224+
@overload
225+
def __pow__(self, __x: _PositiveInteger, __modulo: None = ...) -> int: ...
226+
@overload
227+
def __pow__(self, __x: _NegativeInteger, __modulo: None = ...) -> float: ...
221228
# positive x -> int; negative x -> float
222229
# return type must be Any as `int | float` causes too many false-positive errors
223230
@overload
224-
def __pow__(self, __x: int, __modulo: int | None = ...) -> Any: ...
231+
def __pow__(self, __x: int, __modulo: None = ...) -> Any: ...
225232
def __rpow__(self, __x: int, __mod: int | None = ...) -> Any: ...
226233
def __and__(self, __n: int) -> int: ...
227234
def __or__(self, __n: int) -> int: ...
@@ -1279,44 +1286,74 @@ _M = TypeVar("_M", contravariant=True)
12791286
class _SupportsPow2(Protocol[_E, _T_co]):
12801287
def __pow__(self, __other: _E) -> _T_co: ...
12811288

1289+
class _SupportsPow3NoneOnly(Protocol[_E, _T_co]):
1290+
def __pow__(self, __other: _E, __modulo: None = ...) -> _T_co: ...
1291+
12821292
class _SupportsPow3(Protocol[_E, _M, _T_co]):
12831293
def __pow__(self, __other: _E, __modulo: _M) -> _T_co: ...
12841294

1295+
_SupportsSomeKindOfPow = Union[_SupportsPow2[Any, Any], _SupportsPow3NoneOnly[Any, Any], _SupportsPow3[Any, Any, Any]]
1296+
12851297
if sys.version_info >= (3, 8):
12861298
@overload
12871299
def pow(base: int, exp: int, mod: Literal[0]) -> NoReturn: ...
1300+
@overload
1301+
def pow(base: int, exp: int, mod: int) -> int: ...
1302+
@overload
1303+
def pow(base: int, exp: _PositiveInteger, mod: None = ...) -> int: ... # type: ignore[misc]
1304+
@overload
1305+
def pow(base: int, exp: _NegativeInteger, mod: None = ...) -> float: ... # type: ignore[misc]
12881306
# int base & positive-int exp -> int; int base & negative-int exp -> float
12891307
# return type must be Any as `int | float` causes too many false-positive errors
12901308
@overload
12911309
def pow(base: int, exp: int, mod: None = ...) -> Any: ...
12921310
@overload
1293-
def pow(base: int, exp: int, mod: int) -> int: ...
1294-
@overload
12951311
def pow(base: float, exp: int, mod: None = ...) -> float: ...
12961312
# float base & float exp could return float or complex
1297-
# return type must be Any as `float | complex` causes too many false-positive errors
1313+
# return type must be Any (same as complex base, complex exp),
1314+
# as `float | complex` causes too many false-positive errors
1315+
@overload
1316+
def pow(base: float, exp: complex | _SupportsSomeKindOfPow, mod: None = ...) -> Any: ...
12981317
@overload
1299-
def pow(base: float, exp: float, mod: None = ...) -> Any: ...
1318+
def pow(base: complex, exp: complex | _SupportsSomeKindOfPow, mod: None = ...) -> complex: ...
13001319
@overload
1301-
def pow(base: _SupportsPow2[_E, _T_co], exp: _E) -> _T_co: ...
1320+
def pow(base: _SupportsPow2[_E, _T_co], exp: _E, mod: None = ...) -> _T_co: ...
13021321
@overload
1303-
def pow(base: _SupportsPow3[_E, _M, _T_co], exp: _E, mod: _M) -> _T_co: ...
1322+
def pow(base: _SupportsPow3NoneOnly[_E, _T_co], exp: _E, mod: None = ...) -> _T_co: ...
1323+
@overload
1324+
def pow(base: _SupportsPow3[_E, _M, _T_co], exp: _E, mod: _M = ...) -> _T_co: ...
1325+
@overload
1326+
def pow(base: _SupportsSomeKindOfPow, exp: float, mod: None = ...) -> Any: ...
1327+
@overload
1328+
def pow(base: _SupportsSomeKindOfPow, exp: complex, mod: None = ...) -> complex: ...
13041329

13051330
else:
13061331
@overload
13071332
def pow(__base: int, __exp: int, __mod: Literal[0]) -> NoReturn: ...
13081333
@overload
1309-
def pow(__base: int, __exp: int, __mod: None = ...) -> Any: ...
1310-
@overload
13111334
def pow(__base: int, __exp: int, __mod: int) -> int: ...
13121335
@overload
1336+
def pow(__base: int, __exp: _PositiveInteger, __mod: None = ...) -> int: ... # type: ignore[misc]
1337+
@overload
1338+
def pow(__base: int, __exp: _NegativeInteger, __mod: None = ...) -> float: ... # type: ignore[misc]
1339+
@overload
1340+
def pow(__base: int, __exp: int, __mod: None = ...) -> Any: ...
1341+
@overload
13131342
def pow(__base: float, __exp: int, __mod: None = ...) -> float: ...
13141343
@overload
1315-
def pow(__base: float, __exp: float, __mod: None = ...) -> Any: ...
1344+
def pow(__base: float, __exp: complex | _SupportsSomeKindOfPow, __mod: None = ...) -> Any: ...
1345+
@overload
1346+
def pow(__base: complex, __exp: complex | _SupportsSomeKindOfPow, __mod: None = ...) -> complex: ...
1347+
@overload
1348+
def pow(__base: _SupportsPow2[_E, _T_co], __exp: _E, __mod: None = ...) -> _T_co: ...
1349+
@overload
1350+
def pow(__base: _SupportsPow3NoneOnly[_E, _T_co], __exp: _E, __mod: None = ...) -> _T_co: ...
1351+
@overload
1352+
def pow(__base: _SupportsPow3[_E, _M, _T_co], __exp: _E, __mod: _M = ...) -> _T_co: ...
13161353
@overload
1317-
def pow(__base: _SupportsPow2[_E, _T_co], __exp: _E) -> _T_co: ...
1354+
def pow(__base: _SupportsSomeKindOfPow, __exp: float, __mod: None = ...) -> Any: ...
13181355
@overload
1319-
def pow(__base: _SupportsPow3[_E, _M, _T_co], __exp: _E, __mod: _M) -> _T_co: ...
1356+
def pow(__base: _SupportsSomeKindOfPow, __exp: complex, __mod: None = ...) -> complex: ...
13201357

13211358
def quit(code: object = ...) -> NoReturn: ...
13221359

0 commit comments

Comments
 (0)