Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve stubs for __pow__ #6287

Merged
merged 9 commits into from
Nov 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,13 @@ class int:
def __rmod__(self, __x: int) -> int: ...
def __rdivmod__(self, __x: int) -> tuple[int, int]: ...
@overload
def __pow__(self, __x: Literal[2], __modulo: int | None = ...) -> int: ...
def __pow__(self, __x: int, __modulo: Literal[0]) -> NoReturn: ...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't result of x ** 0 == 1?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pow(1, 1, 0):

In [4]: pow(1, 1, 0)

Traceback (most recent call last):
  File "/main_instance_shell/jelle/venv/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3343, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-23a0b67588cd>", line 1, in <module>
    pow(1, 1, 0)
ValueError: pow() 3rd argument cannot be 0

@overload
def __pow__(self, __x: int, __modulo: int | None = ...) -> Any: ... # Return type can be int or float, depending on x.
def __pow__(self, __x: Literal[2, 3, 4, 5], __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
def __pow__(self, __x: int, __modulo: int | None = ...) -> Any: ...
def __rpow__(self, __x: int, __mod: int | None = ...) -> Any: ...
def __and__(self, __n: int) -> int: ...
def __or__(self, __n: int) -> int: ...
Expand Down Expand Up @@ -276,9 +280,12 @@ class float:
def __truediv__(self, __x: float) -> float: ...
def __mod__(self, __x: float) -> float: ...
def __divmod__(self, __x: float) -> tuple[float, float]: ...
def __pow__(
self, __x: float, __mod: None = ...
) -> float: ... # In Python 3, returns complex if self is negative and x is not whole
@overload
def __pow__(self, __x: int, __mod: None = ...) -> float: ...
# positive x -> float; negative x -> complex
# return type must be Any as `float | complex` causes too many false-positive errors
@overload
def __pow__(self, __x: float, __mod: None = ...) -> Any: ...
def __radd__(self, __x: float) -> float: ...
def __rsub__(self, __x: float) -> float: ...
def __rmul__(self, __x: float) -> float: ...
Expand Down Expand Up @@ -1271,25 +1278,35 @@ class _SupportsPow3(Protocol[_E, _M, _T_co]):

if sys.version_info >= (3, 8):
@overload
def pow(base: int, exp: int, mod: None = ...) -> Any: ... # returns int or float depending on whether exp is non-negative
def pow(base: int, exp: int, mod: Literal[0]) -> NoReturn: ...
# 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: float, mod: None = ...) -> float: ...
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
@overload
def pow(base: float, exp: float, mod: None = ...) -> Any: ...
@overload
def pow(base: _SupportsPow2[_E, _T_co], exp: _E) -> _T_co: ...
@overload
def pow(base: _SupportsPow3[_E, _M, _T_co], exp: _E, mod: _M) -> _T_co: ...

else:
@overload
def pow(
__base: int, __exp: int, __mod: None = ...
) -> Any: ... # returns int or float depending on whether exp is non-negative
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: float, __exp: float, __mod: None = ...) -> float: ...
def pow(__base: float, __exp: int, __mod: None = ...) -> float: ...
@overload
def pow(__base: float, __exp: float, __mod: None = ...) -> Any: ...
@overload
def pow(__base: _SupportsPow2[_E, _T_co], __exp: _E) -> _T_co: ...
@overload
Expand Down