python / typeshed

Collection of library stubs for Python, with static types
Other
4.3k stars 1.73k forks source link

int pow to literal zero incorrect overload #12475

Closed alwaysmpe closed 1 month ago

alwaysmpe commented 1 month ago

The pow to a literal 0 overload has incorrect literal return.

Code:

https://github.com/python/typeshed/blob/1a837277234f12b546d4aa1252e4b44863c0a862/stdlib/builtins.pyi#L284-L287

however, -1 ** 0 == -1 so this should instead be:

@overload
def __pow__(self, x: Literal[0], /) -> Literal[1] | Literal[-1]: ...
overload
def __pow__(self, value: Literal[0], mod: None, /) -> Literal[1] | Literal[-1]: ...

although when I do:

>>> 1 ** 0
1
>>> -1 ** 0
-1
>>> pow(1, 0)
1
>>> pow(-1, 0)
1

this seems weird.

I'm running python 3.12.4 installed on linux using pyenv.

AlexWaygood commented 1 month ago

The expression -1 ** 0 desugars to -(1 ** 0). The existing overloads are correct:

>>> (-1).__pow__(0)
1
>>> -1 ** 0
-1
>>> (-1) ** 0
1
alwaysmpe commented 1 month ago

My bad, cheers

hauntsaninja commented 1 month ago

python -c 'import ast; print(ast.dump(ast.parse("-1 ** 0"), indent=4))' :-)