Skip to content

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

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

Merged
merged 1 commit into from
Aug 30, 2024

Conversation

jorenham
Copy link
Member

@jorenham jorenham commented Aug 14, 2024

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
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
66 checks passed
@charris
Copy link
Member

charris commented Aug 30, 2024

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