Skip to content

TYP: Semi-transparent numpy.shape shape-type annotations. - #27210

Merged
charris merged 1 commit into
numpy:mainfrom
jorenham:typing/numpy.shape
Aug 30, 2024
Merged

charris merged 1 commit into
numpy:mainfrom
jorenham:typing/numpy.shape

Conversation

@jorenham

@jorenham jorenham commented Aug 14, 2024 •

Copy link
Copy Markdown
Member

Before, numpy.shape() always returned tuple[int, ...].

This changes numpy.shape so return:

  • tuple[()] (the empty "0-d" tuple) for scalar-likes
  • tuple[int] for scalar sequences, memoryviews and bytearrays
  • tuple[int, int] for sequences of scalar sequences, e.g. [[1, 0], [0, 1]]
  • tuple[int, ...] for ndarray[Any, Any] (so not Any, see TYP: Stop using Any as shape-type default #27211 for details)
  • ShapeType: tuple[int, ...] for objects with a shape: ShapeType property, which includes dtype and ndarray, unless ShapeType is bound to Any (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 match NDArray).
  • tuple[int, ...] for any other array-likes

@jorenham jorenham changed the title TYP: Transparent numpy.shape shape-type annotations. TYP: Semi-transparent numpy.shape shape-type annotations. Aug 14, 2024
@jorenham

Copy link
Copy Markdown
Member Author

As explained in #27211, we'll be able to do this better and cleaner if we'd merge #27211 first.

@jorenham
jorenham force-pushed the typing/numpy.shape branch from 7834733 to c5d52f3 Compare August 15, 2024 09:44
@charris
charris merged commit f2666ed into numpy:main Aug 30, 2024
@charris

charris commented Aug 30, 2024

Copy link
Copy Markdown
Member

Thanks @jorenham .

@jorenham
jorenham deleted the typing/numpy.shape branch August 30, 2024 19:42
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.

2 participants