8000 Improving type support for `math.prod` by cake-monotone · Pull Request #13572 · python/typeshed · GitHub
[go: up one dir, main page]

Skip to content

Improving type support for math.prod #13572

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

8000
Merged

Conversation

cake-monotone
Copy link
Contributor
@cake-monotone cake-monotone commented Mar 2, 2025

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]]

Copy link
Contributor
github-actions bot commented Mar 2, 2025

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

Copy link
Collaborator
@srittau srittau left a comment

Choose a reason for hiding this comment

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

Thank you, also for the extensive tests.

@srittau srittau merged commit 9f11db4 into python:main Mar 3, 2025
55 checks passed
mmingyu pushed a commit to mmingyu/typeshed that referenced this pull request May 16, 2025
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.

2 participants
0