Skip to content

Commit 3324e22

Browse files
authored
Improve stubs for __pow__ (#6287)
1 parent 5bd7475 commit 3324e22

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

stdlib/builtins.pyi

+28-11
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,13 @@ class int:
221221
def __rmod__(self, __x: int) -> int: ...
222222
def __rdivmod__(self, __x: int) -> tuple[int, int]: ...
223223
@overload
224-
def __pow__(self, __x: Literal[2], __modulo: int | None = ...) -> int: ...
224+
def __pow__(self, __x: int, __modulo: Literal[0]) -> NoReturn: ...
225225
@overload
226-
def __pow__(self, __x: int, __modulo: int | None = ...) -> Any: ... # Return type can be int or float, depending on x.
226+
def __pow__(self, __x: Literal[2, 3, 4, 5], __modulo: int | None = ...) -> int: ...
227+
# positive x -> int; negative x -> float
228+
# return type must be Any as `int | float` causes too many false-positive errors
229+
@overload
230+
def __pow__(self, __x: int, __modulo: int | None = ...) -> Any: ...
227231
def __rpow__(self, __x: int, __mod: int | None = ...) -> Any: ...
228232
def __and__(self, __n: int) -> int: ...
229233
def __or__(self, __n: int) -> int: ...
@@ -276,9 +280,12 @@ class float:
276280
def __truediv__(self, __x: float) -> float: ...
277281
def __mod__(self, __x: float) -> float: ...
278282
def __divmod__(self, __x: float) -> tuple[float, float]: ...
279-
def __pow__(
280-
self, __x: float, __mod: None = ...
281-
) -> float: ... # In Python 3, returns complex if self is negative and x is not whole
283+
@overload
284+
def __pow__(self, __x: int, __mod: None = ...) -> float: ...
285+
# positive x -> float; negative x -> complex
286+
# return type must be Any as `float | complex` causes too many false-positive errors
287+
@overload
288+
def __pow__(self, __x: float, __mod: None = ...) -> Any: ...
282289
def __radd__(self, __x: float) -> float: ...
283290
def __rsub__(self, __x: float) -> float: ...
284291
def __rmul__(self, __x: float) -> float: ...
@@ -1271,25 +1278,35 @@ class _SupportsPow3(Protocol[_E, _M, _T_co]):
12711278

12721279
if sys.version_info >= (3, 8):
12731280
@overload
1274-
def pow(base: int, exp: int, mod: None = ...) -> Any: ... # returns int or float depending on whether exp is non-negative
1281+
def pow(base: int, exp: int, mod: Literal[0]) -> NoReturn: ...
1282+
# int base & positive-int exp -> int; int base & negative-int exp -> float
1283+
# return type must be Any as `int | float` causes too many false-positive errors
1284+
@overload
1285+
def pow(base: int, exp: int, mod: None = ...) -> Any: ...
12751286
@overload
12761287
def pow(base: int, exp: int, mod: int) -> int: ...
12771288
@overload
1278-
def pow(base: float, exp: float, mod: None = ...) -> float: ...
1289+
def pow(base: float, exp: int, mod: None = ...) -> float: ...
1290+
# float base & float exp could return float or complex
1291+
# return type must be Any as `float | complex` causes too many false-positive errors
1292+
@overload
1293+
def pow(base: float, exp: float, mod: None = ...) -> Any: ...
12791294
@overload
12801295
def pow(base: _SupportsPow2[_E, _T_co], exp: _E) -> _T_co: ...
12811296
@overload
12821297
def pow(base: _SupportsPow3[_E, _M, _T_co], exp: _E, mod: _M) -> _T_co: ...
12831298

12841299
else:
12851300
@overload
1286-
def pow(
1287-
__base: int, __exp: int, __mod: None = ...
1288-
) -> Any: ... # returns int or float depending on whether exp is non-negative
1301+
def pow(__base: int, __exp: int, __mod: Literal[0]) -> NoReturn: ...
1302+
@overload
1303+
def pow(__base: int, __exp: int, __mod: None = ...) -> Any: ...
12891304
@overload
12901305
def pow(__base: int, __exp: int, __mod: int) -> int: ...
12911306
@overload
1292-
def pow(__base: float, __exp: float, __mod: None = ...) -> float: ...
1307+
def pow(__base: float, __exp: int, __mod: None = ...) -> float: ...
1308+
@overload
1309+
def pow(__base: float, __exp: float, __mod: None = ...) -> Any: ...
12931310
@overload
12941311
def pow(__base: _SupportsPow2[_E, _T_co], __exp: _E) -> _T_co: ...
12951312
@overload

0 commit comments

Comments
 (0)