Skip to content

TYP: mean and ndarray.mean shape-typing and improved dtypes#31151

Merged
charris merged 2 commits into
numpy:mainfrom
jorenham:typing/mean
Apr 4, 2026
Merged

TYP: mean and ndarray.mean shape-typing and improved dtypes#31151
charris merged 2 commits into
numpy:mainfrom
jorenham:typing/mean

Conversation

@jorenham

@jorenham jorenham commented Apr 4, 2026

Copy link
Copy Markdown
Member

This significantly improves np.mean and np.ndarray.mean. These now have support for shape-typing, and have improved input-dependent return dtypes.

This also fixes the incompatible overlapping overloads in np.mean, towards #27032

Once this is reviewed and merged, I'll do the same for std and var.

AI Disclosure

I am not a robot ☑️

@github-actions

github-actions Bot commented Apr 4, 2026

Copy link
Copy Markdown

Diff from mypy_primer, showing the effect of this PR on type check results on a corpus of open source code:

colour (https://github.com/colour-science/colour)
- colour/utilities/metrics.py:88: error: Incompatible return value type (got "floating[_16Bit] | floating[_32Bit] | float64", expected "ndarray[tuple[Any, ...], dtype[floating[_16Bit] | floating[_32Bit] | float64]]")  [return-value]
+ colour/recovery/otsu2018.py:905: error: Redundant cast to "ndarray[tuple[Any, ...], dtype[floating[_16Bit] | floating[_32Bit] | float64]]"  [redundant-cast]

scipy (https://github.com/scipy/scipy)
- scipy/signal/_spectral_py.py:275: error: Item "Buffer" of "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | str | _NestedSequence[complex | bytes | str]" has no attribute "reshape"  [union-attr]
- scipy/signal/_spectral_py.py:275: error: Item "_SupportsArray[dtype[Any]]" of "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | str | _NestedSequence[complex | bytes | str]" has no attribute "reshape"  [union-attr]
- scipy/signal/_spectral_py.py:275: error: Item "_NestedSequence[_SupportsArray[dtype[Any]]]" of "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | str | _NestedSequence[complex | bytes | str]" has no attribute "reshape"  [union-attr]
- scipy/signal/_spectral_py.py:275: error: Item "complex" of "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | str | _NestedSequence[complex | bytes | str]" has no attribute "reshape"  [union-attr]
- scipy/signal/_spectral_py.py:275: error: Item "str" of "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | str | _NestedSequence[complex | bytes | str]" has no attribute "reshape"  [union-attr]
- scipy/signal/_spectral_py.py:275: error: Item "_NestedSequence[complex | bytes | str]" of "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | str | _NestedSequence[complex | bytes | str]" has no attribute "reshape"  [union-attr]
- scipy/signal/_spectral_py.py:320: error: Item "Buffer" of "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]" has no attribute "dtype"  [union-attr]
- scipy/signal/_spectral_py.py:320: error: Item "_SupportsArray[dtype[Any]]" of "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]" has no attribute "dtype"  [union-attr]
- scipy/signal/_spectral_py.py:320: error: Item "_NestedSequence[_SupportsArray[dtype[Any]]]" of "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]" has no attribute "dtype"  [union-attr]
- scipy/signal/_spectral_py.py:320: error: Item "complex" of "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]" has no attribute "dtype"  [union-attr]
- scipy/signal/_spectral_py.py:320: error: Item "bytes" of "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]" has no attribute "dtype"  [union-attr]
- scipy/signal/_spectral_py.py:320: error: Item "str" of "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]" has no attribute "dtype"  [union-attr]
- scipy/signal/_spectral_py.py:320: error: Item "_NestedSequence[complex | bytes | str]" of "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]" has no attribute "dtype"  [union-attr]

@charris charris merged commit 939e084 into numpy:main Apr 4, 2026
15 checks passed
@charris

charris commented Apr 4, 2026

Copy link
Copy Markdown
Member

Thanks Joren.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants