FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Improving type support for `math.prod` by cake-monotone · Pull Request #13572 · python/typeshed · GitHub

Repository navigation

Improving type support for math.prod - #13572

Merged
srittau merged 2 commits into
python:mainfrom
cake-monotone:improving-type-support-for-math-prod
Mar 3, 2025
Merged

srittau merged 2 commits into
python:mainfrom
cake-monotone:improving-type-support-for-math-prod

Conversation

cake-monotone commented Mar 2, 2025 •
edited
Loading

Copy link
Copy Markdown
Contributor

Related to #13501

The existing prod type stub did not properly support types like Fraction, complex, and Decimal provided by Python. To address this, I attempted to define the type stub in a way similar to builtins.sum.

Before

@overload
def prod(iterable: Iterable[SupportsIndex], /, *, start: SupportsIndex=1): ...
@overload
def prod(iterable: Iterable[SupportsFloatOrIndex], /, *, start: SupportsFloatOrIndex=1]): ...

Limitations

The previous type stub only checked for __index__ or __float__, but I believe this approach is entirely incorrect. There is no code in math_prod_impl that calls __index__ or __float__. As a result, the following cases fail:

import math

class Foo:
    def __index__(self) -> int:
        return 0

class Bar:
    def __float__(self) -> float:
        return 0.0

math.prod([Foo(), Foo()])  # Fails
math.prod([Bar(), Bar()])  # Fails

Additionally, the old stub did not account for empty iterables:

float_list: list[float] = []
math.prod(float_list)  # Returns 1 at runtime, but inferred as int.

After

_MultiplicableT1 = TypeVar("_MultiplicableT1", bound=SupportsMul[Any, Any])
_MultiplicableT2 = TypeVar("_MultiplicableT2", bound=SupportsMul[Any, Any])

class _SupportsProdWithNoDefaultGiven(SupportsMul[Any, Any], SupportsRMul[int, Any], Protocol): ...
_SupportsProdNoDefaultT = TypeVar("_SupportsProdNoDefaultT", bound=_SupportsProdWithNoDefaultGiven)

@overload
def prod(iterable: Iterable[bool | _LiteralInteger], /, *, start: int = 1) -> int: ...  # type: ignore[overload-overlap]
@overload
def prod(iterable: Iterable[_SupportsProdNoDefaultT], /) -> _SupportsProdNoDefaultT | Literal[1]: ...
@overload
def prod(iterable: Iterable[_MultiplicableT1], /, *, start: _MultiplicableT2) -> _MultiplicableT1 | _MultiplicableT2: ...

This implementation is largely derived from sum, so it shares the same limitations. You can find related PRs for sum here: #7578 and #8000.

Since those PRs were created in 2022, I tried different ways to improve this using newer typing features introduced since then, but none worked out. If anyone has a better idea, I'd love to hear it!

Improvements

With this change, prod correctly handles cases like:

prod([complex(1, 2), complex(3, 4)])  # Returns a complex number!
prod([Fraction(1, 2), Fraction(1, 2)])  # Returns a Fraction!

Limitations

This implementation still produces many false positives. Non-numeric types in Python use the multiplication operator in many different ways due to syntactic sugar, which makes precise typing pretty difficult.

Expression Runtime Result Actual Type Expected Type
prod("abcde") Runtime Error str | Literal[1]
prod([1, 2, 3], start="a") "aaaaaa" str int | str
prod([2], start=(1, 2)) (1, 2, 1, 2) tuple int | tuple[Literal[1], Literal[2]]

github-actions Bot commented Mar 2, 2025

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

srittau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thank you, also for the extensive tests.

srittau merged commit 9f11db4 into python:main Mar 3, 2025
mmingyu pushed a commit to mmingyu/typeshed that referenced this pull request May 16, 2025
Comment thread stdlib/math.pyi
Comment on lines +104 to +105
_PositiveInteger: TypeAlias = Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
_NegativeInteger: TypeAlias = Literal[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

What is this list of numbers? I was surprised by a mypy error saying I might only call math.prod() with integers between -20 ... +25.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL