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 5 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
27 changes: 20 additions & 7 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,12 @@ class int:
def __rmod__(self, __x: int) -> int: ...
def __rdivmod__(self, __x: int) -> tuple[int, int]: ...
@overload
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: Literal[2], __modulo: int | None = ...) -> int: ...
@overload
def __pow__(self, __x: Literal[3], __modulo: int | None = ...) -> int: ...
@overload
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved
def __pow__(self, __x: int, __modulo: int | None = ...) -> Any: ... # Return type can be int or float, depending on x.
def __rpow__(self, __x: int, __mod: int | None = ...) -> Any: ...
def __and__(self, __n: int) -> int: ...
Expand Down Expand Up @@ -276,9 +280,10 @@ 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: ...
@overload
def __pow__(self, __x: float, __mod: None = ...) -> Any: ... # return type could be complex or float depending on x
def __radd__(self, __x: float) -> float: ...
def __rsub__(self, __x: float) -> float: ...
def __rmul__(self, __x: float) -> float: ...
Expand Down Expand Up @@ -324,12 +329,12 @@ class complex:
def __add__(self, __x: complex) -> complex: ...
def __sub__(self, __x: complex) -> complex: ...
def __mul__(self, __x: complex) -> complex: ...
def __pow__(self, __x: complex, mod: None = ...) -> complex: ...
def __pow__(self, __x: complex | int | float, mod: None = ...) -> complex: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

complex | float is enough. Type checkers imagine that int is a subclass of float, even though it actually isn't.

A protocol involving SupportsIndex might be more accurate, if these behave similarly to math functions (#6211 and #6216).

def __truediv__(self, __x: complex) -> complex: ...
def __radd__(self, __x: complex) -> complex: ...
def __rsub__(self, __x: complex) -> complex: ...
def __rmul__(self, __x: complex) -> complex: ...
def __rpow__(self, __x: complex, __mod: None = ...) -> complex: ...
def __rpow__(self, __x: complex | int | float, __mod: None = ...) -> complex: ...
def __rtruediv__(self, __x: complex) -> complex: ...
def __eq__(self, __x: object) -> bool: ...
def __ne__(self, __x: object) -> bool: ...
Expand Down Expand Up @@ -1270,26 +1275,34 @@ class _SupportsPow3(Protocol[_E, _M, _T_co]):
def __pow__(self, __other: _E, __modulo: _M) -> _T_co: ...

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: None = ...) -> Any: ... # returns int or float depending on whether exp is non-negative
@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: ... # return type could be float or complex depending on x
Copy link
Member

Choose a reason for hiding this comment

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

depending on exp

Copy link
Collaborator

Choose a reason for hiding this comment

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

It also depends on base. If the base is positive, then any power will also be positive. Complex numbers need a negative base and a negative power.

Maybe the whole "depending on" part should be deleted? It obviously depends on the arguments in some way, which is really all that needs to be said.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's worth spelling out the conditions... pow is like catnip for typeshed contributors, because everyone always starts off thinking only about positive integers :-)

Copy link
Member Author

Choose a reason for hiding this comment

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

Hah, sorry about that @hauntsaninja

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