TYP: Shape-typed array constructors: numpy.{empty,zeros,ones,full}
#27171
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.
Added overloads to the (direct) array constructor functions in the stubs. These "bind" the type of the passed
shape
argument to the shape-type parameter of thenumpy.ndarray
. Note that this is only possible when the shape is passed asshape: int
orshape: tuple[int, int]
, since e.g. thelist
type contains no information about its length (i.e. the number of dimensions).This affects the type-signatures of:
numpy.empty
(numpy._core.multiarray.empty
)numpy.zeros
(numpy._core.multiarray.zeros
)numpy.ones
(numpy._core.numeric.ones
)numpy.full
(numpy._core.numeric.full
)implementation notes:
typing.TypedDict
andtyping_extensions.Unpack
, as defined in PEP 692.empty
,zeros
andones
have an identical signature (from a typing perspective), and each one has 12 overloads, I refactored them using a callable protocol, and decorated it with@typing.type_check_only
.typing_extensions
in the stubs, see TYP: Assume thattyping_extensions
is always available in the stubs #27132 .This should be backwards-compatible, because the
ndarray
shape-type parameter is covariant, see #26081.