Skip to content

math.prod typing incorrect #11913

Closed
Closed

Description

@karolinepauls

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

Activity

  1. added
    stubs: false positive
    on May 13, 2024
  2. srittau commented on May 13, 2024

    @srittau
    Collaborator

    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!)

  3. Akuli commented on May 13, 2024

    @Akuli
    Collaborator

    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
    
  4. real-or-random commented on May 16, 2024

    @real-or-random

    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
    
  5. kalvdans commented on Nov 5, 2025

    @kalvdans

    Fixed by #13572?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stubs: false positive

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions