TYP: np.asarray improved shape-typing - #32263
Merged
charris merged 1 commit intoAug 12, 2026
Merged
Conversation
|
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/array.py:414: note: def [ShapeT: tuple[int, ...], DTypeT: dtype[Any]] asarray(a: _SupportsArray[ndarray[ShapeT, DTypeT]], dtype: None = ..., order: Literal['K', 'A', 'C', 'F'] | None = ..., *, device: Literal['cpu'] | None = ..., copy: bool | None = ..., like: _SupportsArrayFunc | None = ...) -> ndarray[ShapeT, DTypeT]
+ jax/_src/array.py:414: note: def asarray(a: bool, dtype: None = ..., order: Literal['K', 'A', 'C', 'F'] | None = ..., *, device: Literal['cpu'] | None = ..., copy: bool | None = ..., like: _SupportsArrayFunc | None = ...) -> ndarray[tuple[()], dtype[numpy.bool[builtins.bool]]]
+ jax/_src/array.py:414: note: def asarray(a: int, dtype: None = ..., order: Literal['K', 'A', 'C', 'F'] | None = ..., *, device: Literal['cpu'] | None = ..., copy: bool | None = ..., like: _SupportsArrayFunc | None = ...) -> ndarray[tuple[()], dtype[signedinteger[_32Bit | _64Bit] | Any]]
+ jax/_src/array.py:414: note: def asarray(a: float, dtype: None = ..., order: Literal['K', 'A', 'C', 'F'] | None = ..., *, device: Literal['cpu'] | None = ..., copy: bool | None = ..., like: _SupportsArrayFunc | None = ...) -> ndarray[tuple[()], dtype[float64 | Any]]
+ jax/_src/array.py:414: note: def asarray(a: complex, dtype: None = ..., order: Literal['K', 'A', 'C', 'F'] | None = ..., *, device: Literal['cpu'] | None = ..., copy: bool | None = ..., like: _SupportsArrayFunc | None = ...) -> ndarray[tuple[()], dtype[complex128 | Any]]
+ jax/_src/array.py:414: note: def [ScalarT: generic[Any]] asarray(a: complex | str | generic[Any], dtype: type[ScalarT] | dtype[ScalarT] | _HasDType[dtype[ScalarT]] | _HasNumPyDType[dtype[ScalarT]], order: Literal['K', 'A', 'C', 'F'] | None = ..., *, device: Literal['cpu'] | None = ..., copy: bool | None = ..., like: _SupportsArrayFunc | None = ...) -> ndarray[tuple[()], dtype[ScalarT]]
+ jax/_src/array.py:414: note: def asarray(a: complex | str | generic[Any], dtype: type | str | dtype[Any] | _HasDType[dtype[Any]] | _HasNumPyDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict, order: Literal['K', 'A', 'C', 'F'] | None = ..., *, device: Literal['cpu'] | None = ..., copy: bool | None = ..., like: _SupportsArrayFunc | None = ...) -> ndarray[tuple[()], dtype[Any]]
+ jax/_src/array.py:414: note: def asarray(a: Sequence[Sequence[list[float]]], dtype: None = ..., order: Literal['K', 'A', 'C', 'F'] | None = ..., *, device: Literal['cpu'] | None = ..., copy: bool | None = ..., like: _SupportsArrayFunc | None = ...) -> ndarray[tuple[int, int, int], dtype[float64]]
+ jax/_src/array.py:414: note: def asarray(a: Sequence[Sequence[Sequence[bool]]], dtype: None = ..., order: Literal['K', 'A', 'C', 'F'] | None = ..., *, device: Literal['cpu'] | None = ..., copy: bool | None = ..., like: _SupportsArrayFunc | None = ...) -> ndarray[tuple[int, int, int], dtype[numpy.bool[builtins.bool]]]
+ jax/_src/array.py:414: note: def asarray(a: Sequence[Sequence[list[int]]], dtype: None = ..., order: Literal['K', 'A', 'C', 'F'] | None = ..., *, device: Literal['cpu'] | None = ..., copy: bool | None = ..., like: _SupportsArrayFunc | None = ...) -> ndarray[tuple[int, int, int], dtype[signedinteger[_32Bit | _64Bit]]]
+ jax/_src/array.py:414: note: def asarray(a: Sequence[Sequence[list[complex]]], dtype: None = ..., order: Literal['K', 'A', 'C', 'F'] | None = ..., *, device: Literal['cpu'] | None = ..., copy: bool | None = ..., like: _SupportsArrayFunc | None = ...) -> ndarray[tuple[int, int, int], dtype[complex128]]
+ jax/_src/array.py:414: note: def [ScalarT: generic[Any]] asarray(a: Sequence[Sequence[Sequence[complex | str | bytes | generic[Any]]]], dtype: type[ScalarT] | dtype[ScalarT] | _HasDType[dtype[ScalarT]] | _HasNumPyDType[dtype[ScalarT]], order: Literal['K', 'A', 'C', 'F'] | None = ..., *, device: Literal['cpu'] | None = ..., copy: bool | None = ..., like: _SupportsArrayFunc | None = ...) -> ndarray[tuple[int, int, int], dtype[ScalarT]]
+ jax/_src/array.py:414: note: def asarray(a: Sequence[Sequence[Sequence[complex | str | bytes | generic[Any]]]], dtype: type | str | dtype[Any] | _HasDType[dtype[Any]] | _HasNumPyDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict, order: Literal['K', 'A', 'C', 'F'] | None = ..., *, device: Literal['cpu'] | None = ..., copy: bool | None = ..., like: _SupportsArrayFunc | None = ...) -> ndarray[tuple[int, int, int], dtype[Any]]
optuna (https://github.com/optuna/optuna)
./optuna/study/_multi_objective.py:182: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
|
Member
Author
|
The mypy_primer diff for jax are just |
Member
|
Thanks Joren. |
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 ports the
np.arrayshape-typing improvements from #32246 tonp.asarray.Mostly mechanical, so I used AI to copy over the overloads, and I then cleaned up some of the mistakes it made myself :p