Skip to content

MAINT: Refine a number of np.generic annotations - #17673

Merged
charris merged 2 commits into
numpy:masterfrom
BvB93:generic-specific
Nov 14, 2020
Merged

charris merged 2 commits into
numpy:masterfrom
BvB93:generic-specific

Conversation

@BvB93

@BvB93 BvB93 commented Oct 29, 2020 •

Copy link
Copy Markdown
Member

This PR moves a number of the _ArrayOrScalarCommon (typing-only) superclass to ndarray and/or generic.

The methods of generic can generally speaking be annotated more accurately (compared to
ndarray) due to their fixed size and dtype. This PR does exactly that: it takes a number of
methods from _ArrayOrScalarCommon and moves them to ndarray and generic.
The latter case is herein accompanied by an update of the respective annotations; for example
def ndim(self) -> int: ... becomes def ndim(self) -> Literal[0]: ... for generic.

Below is an overview of all the generic changes w.r.t. ndarray:

  • base: Always returns None.
  • dtype: Now returns a parametrized dtype (w.r.t. itself) rather than just plain dtype[Any].
  • size: Always returns 1.
  • ndim: Always returns 0.
  • shape & strides: Always returns ().
  • byteswap: The inplace parameter must always be set to False.
  • item: The passed axis must always be 0, () or (0,).
  • squeeze: The passed axis must always be 0 or ().
  • transpose: The passed axis must always be ().

A few others were completelly removed from generic. The respective methods (incorrectly)
claim the modify the generic inplace, even though they might not necessarily raise an exception
during runtime:

  • fill: Silently fails.
  • resize: Silently fails.
  • itemset: Raises a ValueError.

@charris

charris commented Nov 14, 2020

Copy link
Copy Markdown
Member

Needs rebase.

@BvB93

BvB93 commented Nov 14, 2020

Copy link
Copy Markdown
Member Author

Alright, the latest rebase seems to have fixed things.

@charris
charris merged commit 4a4ca39 into numpy:master Nov 14, 2020
@charris

charris commented Nov 14, 2020

Copy link
Copy Markdown
Member

Thanks Bas.

@BvB93
BvB93 deleted the generic-specific branch November 14, 2020 22:28
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