TYP: Semi-transparent numpy.shape
shape-type annotations.
#27210
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.
Before,
numpy.shape()
always returnedtuple[int, ...]
.This changes
numpy.shape
so return:tuple[()]
(the empty "0-d" tuple) for scalar-likestuple[int]
for scalar sequences,memoryview
s andbytearray
stuple[int, int]
for sequences of scalar sequences, e.g.[[1, 0], [0, 1]]
tuple[int, ...]
forndarray[Any, Any]
(so notAny
, see TYP: Stop usingAny
as shape-type default #27211 for details)ShapeType: tuple[int, ...]
for objects with ashape: ShapeType
property, which includesdtype
andndarray
, unlessShapeType
is bound toAny
(npt.NDArray
currently does this). This doesn't work on mypy, probably due to one of its (many) bugs. I wasn't able to find a workaround for this unfortunately (dimension-specific overloads aren't even possible, as that would also matchNDArray
).tuple[int, ...]
for any other array-likes