| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Sorry, something went wrong.
There was a problem hiding this comment.
Thank you, also for the extensive tests.
Sorry, something went wrong.
| _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] |
There was a problem hiding this comment.
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.
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
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
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:
Additionally, the old stub did not account for empty iterables:
After
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:
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.