TYP: sum and prod shape-typing and improved dtypes#31160
Merged
Conversation
This comment has been minimized.
This comment has been minimized.
Member
Author
|
RE: mypy_primer
So to summarize, there are two action points for me:
|
|
Diff from mypy_primer, showing the effect of this PR on type check results on a corpus of open source code: jax (https://github.com/google/jax)
- jax/_src/pallas/pipelining/schedulers.py:412: error: Argument 3 to "check_async_done" has incompatible type "signedinteger[_64Bit]"; expected "int | Array" [arg-type]
+ jax/_src/pallas/pipelining/schedulers.py:412: error: Argument 3 to "check_async_done" has incompatible type "signedinteger[_32Bit | _64Bit]"; expected "int | Array" [arg-type]
colour (https://github.com/colour-science/colour)
+ colour/phenomena/sky/wilkie2021.py:927: error: Argument "theta" to "SkyParameters_Wilkie2021" has incompatible type "ndarray[tuple[Any, ...], dtype[Any]]"; expected "float" [arg-type]
+ colour/phenomena/sky/wilkie2021.py:928: error: Argument "gamma" to "SkyParameters_Wilkie2021" has incompatible type "ndarray[tuple[Any, ...], dtype[Any]]"; expected "float" [arg-type]
+ colour/phenomena/sky/wilkie2021.py:929: error: Argument "shadow" to "SkyParameters_Wilkie2021" has incompatible type "ndarray[tuple[Any, ...], dtype[Any]]"; expected "float" [arg-type]
+ colour/phenomena/sky/wilkie2021.py:930: error: Argument "zero" to "SkyParameters_Wilkie2021" has incompatible type "ndarray[tuple[Any, ...], dtype[Any]]"; expected "float" [arg-type]
pandas (https://github.com/pandas-dev/pandas)
+ pandas/io/parsers/base_parser.py:516: error: Incompatible types in assignment (expression has type "signedinteger[_32Bit | _64Bit]", variable has type "int") [assignment]
+ pandas/io/parsers/base_parser.py:518: error: Incompatible types in assignment (expression has type "signedinteger[_32Bit | _64Bit]", variable has type "int") [assignment]
+ pandas/core/internals/blocks.py:1279: error: Argument 2 to "setitem_datetimelike_compat" has incompatible type "signedinteger[_32Bit | _64Bit]"; expected "int" [arg-type]
+ pandas/core/indexes/base.py:5773: error: Argument 2 to "setitem_datetimelike_compat" has incompatible type "signedinteger[_32Bit | _64Bit]"; expected "int" [arg-type]
+ pandas/core/resample.py:2623: error: Incompatible types in assignment (expression has type "signedinteger[_32Bit | _64Bit]", variable has type "int") [assignment]
optuna (https://github.com/optuna/optuna)
+ optuna/importance/_fanova/_tree.py:311: error: Incompatible return value type (got "ndarray[Any, Any]", expected "float") [return-value]
numpy-stl (https://github.com/WoLpH/numpy-stl)
+ stl/base.py:448: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, int], dtype[float64]]", variable has type "ndarray[tuple[int, int], dtype[floating[_32Bit]]]") [assignment]
|
Member
Author
|
The remaining primer errors are, except for the The |
Member
|
Thanks Joren. |
This was referenced Apr 6, 2026
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This follows #31154, and uses a similar overload pattern as for
np[.ndarray].{mean,std,var}, butsumandproddiffer in that they don't upcastintegertofloat64, and instead upcastbool_toint_, so they require additional overloads.The
sumandprodcontracts are almost identical except fortimedelta64; which is acceptedsumbut not byprod.This also adds specialized overrides for the
sumandprodmethods of thenumber,bool, andtimedelta64(only forsum) scalar types, which might help in case you e.g..sum()onfloat64 | NDArray[float64]input, i.e. when you're not sure if you're summing a scalar or an array.AI Disclosure
N/A