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

math.prod typing incorrect #11913

Open
karolinepauls opened this issue May 13, 2024 · 3 comments
Open

math.prod typing incorrect #11913

karolinepauls opened this issue May 13, 2024 · 3 comments
Labels
stubs: false positive Type checkers report false errors

Comments

@karolinepauls
Copy link

Test file (decimal_prod.py):

from decimal import Decimal
from math import prod
from typing import TYPE_CHECKING

result = prod([Decimal(1), Decimal(2)])

if TYPE_CHECKING:
    reveal_type(result)
else:
    print(type(result))

Test:

$ mypy --version
mypy 1.10.0 (compiled: yes)
$ mypy decimal_prod.py
decimal_prod.py:8: note: Revealed type is "builtins.float"
Success: no issues found in 1 source file
$ python decimal_prod.py
<class 'decimal.Decimal'>

defined here: https://github.com/python/typeshed/blob/main/stdlib/math.pyi#L102-L105

@srittau srittau added the stubs: false positive Type checkers report false errors label May 13, 2024
@srittau
Copy link
Collaborator

srittau commented May 13, 2024

It sounds as if we should use a TypeVar here, but the existing overload makes me a bit anxious. Any fix should include tests to check a few use cases. (But as always: PR welcome!)

@Akuli
Copy link
Collaborator

Akuli commented May 13, 2024

Presumably all non-int return types should be a union with Literal[1]. No matter what type the sequence elements are, it is possible to get the integer 1 with an empty sequence:

>>> import math
>>> math.prod([])
1

@real-or-random
Copy link

Presumably all non-int return types should be a union with Literal[1]. No matter what type the sequence elements are, it is possible to get the integer 1 with an empty sequence:

>>> import math
>>> math.prod([])
1

And now try:

>>> math.prod([], start=17)
> 17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stubs: false positive Type checkers report false errors
Projects
None yet
Development

No branches or pull requests

4 participants