-
-
Notifications
You must be signed in to change notification settings - Fork 12k
TYP: lib._function_base_impl: many typing improvements
#30246
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
Conversation
|
This turned out to be a lot more work than I had initially anticipated 😅 |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
The primer error for The primer error for The disappeared |
|
Ping @MarcoGorelli - any chance you can give this a once-over? |
|
Yea that would help :). In the meantime, let's see if mr. robot has anything useful to say about this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR significantly improves typing for functions in numpy.lib._function_base_impl, adding shape-typing support, more accurate dtype specifications, validation for callable signatures, and comprehensive overloads for 30+ functions.
Key Changes:
- Enhanced type precision with shape-aware types (e.g.,
_Array1D,_Array2D) and specific dtype returns - Expanded overloads for functions like
average,median,percentile,quantile,gradient, etc. to handle different input shapes and scalar types - Added callable signature validation for
piecewisefunction usingParamSpec
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| numpy/lib/_function_base_impl.pyi | Extensive typing improvements including new type aliases, overloads for shape-preservation, and more precise return types for 30+ functions |
| numpy/typing/tests/data/reveal/lib_function_base.pyi | Updated type assertion tests to validate new typing improvements with shape-specific and dtype-specific checks |
| numpy/typing/tests/data/fail/lib_function_base.pyi | Updated error type expectations to match new overload signatures |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Ok I have to admit that that was a pretty solid review. I think I'll ask copilot for reviews more often now. |
|
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/models/rgb/cylindrical.py:531: error: List item 0 has incompatible type "ndarray[tuple[Any, ...], dtype[floating[_16Bit] | floating[_32Bit] | float64]]"; expected "_SupportsArray[dtype[numpy.bool[builtins.bool]]] | _NestedSequence[_SupportsArray[dtype[numpy.bool[builtins.bool]]]] | builtins.bool | _NestedSequence[builtins.bool]" [list-item]
+ colour/models/rgb/cylindrical.py:532: error: List item 1 has incompatible type "ndarray[tuple[Any, ...], dtype[floating[_16Bit] | floating[_32Bit] | float64]]"; expected "_SupportsArray[dtype[numpy.bool[builtins.bool]]] | _NestedSequence[_SupportsArray[dtype[numpy.bool[builtins.bool]]]] | builtins.bool | _NestedSequence[builtins.bool]" [list-item]
+ colour/models/rgb/cylindrical.py:533: error: List item 2 has incompatible type "ndarray[tuple[Any, ...], dtype[floating[_16Bit] | floating[_32Bit] | float64]]"; expected "_SupportsArray[dtype[numpy.bool[builtins.bool]]] | _NestedSequence[_SupportsArray[dtype[numpy.bool[builtins.bool]]]] | builtins.bool | _NestedSequence[builtins.bool]" [list-item]
+ colour/models/rgb/cylindrical.py:534: error: List item 3 has incompatible type "ndarray[tuple[Any, ...], dtype[floating[_16Bit] | floating[_32Bit] | float64]]"; expected "_SupportsArray[dtype[numpy.bool[builtins.bool]]] | _NestedSequence[_SupportsArray[dtype[numpy.bool[builtins.bool]]]] | builtins.bool | _NestedSequence[builtins.bool]" [list-item]
+ colour/models/rgb/cylindrical.py:535: error: List item 4 has incompatible type "ndarray[tuple[Any, ...], dtype[floating[_16Bit] | floating[_32Bit] | float64]]"; expected "_SupportsArray[dtype[numpy.bool[builtins.bool]]] | _NestedSequence[_SupportsArray[dtype[numpy.bool[builtins.bool]]]] | builtins.bool | _NestedSequence[builtins.bool]" [list-item]
+ colour/models/rgb/cylindrical.py:536: error: List item 5 has incompatible type "ndarray[tuple[Any, ...], dtype[floating[_16Bit] | floating[_32Bit] | float64]]"; expected "_SupportsArray[dtype[numpy.bool[builtins.bool]]] | _NestedSequence[_SupportsArray[dtype[numpy.bool[builtins.bool]]]] | builtins.bool | _NestedSequence[builtins.bool]" [list-item]
optuna (https://github.com/optuna/optuna)
+ optuna/samplers/_tpe/_truncnorm.py:262: error: No overload variant of "select" matches argument types "list[Any]", "list[object]", "ndarray[tuple[Any, ...], dtype[Any]]" [call-overload]
+ optuna/samplers/_tpe/_truncnorm.py:262: note: Possible overload variants:
+ optuna/samplers/_tpe/_truncnorm.py:262: note: def [_ArrayT: ndarray[tuple[Any, ...], dtype[Any]]] select(condlist: _SizedIterable[_SupportsArray[dtype[numpy.bool[builtins.bool]]] | _NestedSequence[_SupportsArray[dtype[numpy.bool[builtins.bool]]]] | builtins.bool | _NestedSequence[builtins.bool]], choicelist: Sequence[_ArrayT], default: complex | str | bytes | generic[Any] = ...) -> _ArrayT
+ optuna/samplers/_tpe/_truncnorm.py:262: note: def [_ScalarT: generic[Any]] select(condlist: _SizedIterable[_SupportsArray[dtype[numpy.bool[builtins.bool]]] | _NestedSequence[_SupportsArray[dtype[numpy.bool[builtins.bool]]]] | builtins.bool | _NestedSequence[builtins.bool]], choicelist: Sequence[_SupportsArray[dtype[_ScalarT]] | _NestedSequence[_SupportsArray[dtype[_ScalarT]]]] | ndarray[tuple[Any, ...], dtype[_ScalarT]], default: complex | str | bytes | generic[Any] = ...) -> ndarray[tuple[Any, ...], dtype[_ScalarT]]
+ optuna/samplers/_tpe/_truncnorm.py:262: note: def select(condlist: _SizedIterable[_SupportsArray[dtype[numpy.bool[builtins.bool]]] | _NestedSequence[_SupportsArray[dtype[numpy.bool[builtins.bool]]]] | builtins.bool | _NestedSequence[builtins.bool]], choicelist: Sequence[Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]], default: complex | str | bytes | generic[Any] = ...) -> ndarray[tuple[Any, ...], dtype[Any]]
arviz (https://github.com/arviz-devs/arviz)
- arviz/stats/ecdf_utils.py:324: error: Incompatible return value type (got "floating[Any]", expected "float") [return-value]
|
|
Thanks Joren. |
* TYP: ``lib._function_base_impl``: many typing improvements * TYP: use the any trick when calling ``gradiant`` with arrays of unknown shape * TYP: fix regression in ``append`` caught by mypy_primer * TYP: Apply Copilot's review suggestions (yes, they actually made sense)
* TYP: ``lib._function_base_impl``: many typing improvements * TYP: use the any trick when calling ``gradiant`` with arrays of unknown shape * TYP: fix regression in ``append`` caught by mypy_primer * TYP: Apply Copilot's review suggestions (yes, they actually made sense)
For most
numpy.lib._function_base_implfunctions, the adds support for shape-typing, improves the accuracy of returned dtypes, adds validation for callable argument signatures, and adds inline overload comments, and fixes overlapping overloads, towards #27032.The following functions are affected:
np.averagenp.asarray_chkfinitenp.piecewisenp.extractnp.selectnp.copynp.gradientnp.diffnp.interpnp.anglenp.unwrapnp.sort_complexnp.covnp.corrcoefnp.blackmannp.bartlettnp.hanningnp.hammingnp.kaisernp.i0np.sincnp.mediannp.percentilenp.quantilenp.trapezoidnp.meshgridnp.placenp.deletenp.insertnp.appendnp.digitize