Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH: Should _ShapeType be covariant? #25729

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

Closed
Jacob-Stevens-Haas opened this issue Jan 30, 2024 · 0 comments · Fixed by #26081
Closed

ENH: Should _ShapeType be covariant? #25729

Jacob-Stevens-Haas opened this issue Jan 30, 2024 · 0 comments · Fixed by #26081

Comments

@Jacob-Stevens-Haas
Copy link
Contributor

Jacob-Stevens-Haas commented Jan 30, 2024

Proposed new feature or change:

I believe _ShapeType should be covariant. For instance, the following code does not typecheck because _ShapeType is invariant:

from typing import NewType
import numpy as np
from numpy.typing import NBitBase

Time = NewType("Time", int)
Series = NewType("Series", int)
AnyFloat = np.dtype[np.floating[NBitBase]]

arr: np.ndarray[tuple[Time, Series], AnyFloat] = np.arange(4.0).reshape((2, 2))

def foo(a: np.ndarray[tuple[int, ...], AnyFloat]) -> None: ...

foo(arr)

I don't think there's any problems making it covariant, because I would assume it will be replaced by TypeVarTuple (PEP 646), and tuples are covariant. In the interim, there's a few functions that change the shape of the array, such as __getitem__, but it seems those return ndarray[Any,] except in the overload:

    @overload
    def __getitem__(self: NDArray[void], key: list[str]) -> ndarray[_ShapeType, _dtype[void]]: ...

I'm not sure what situation this is handling, since I've never seen self typed.

There's also the chararray, memmap, matrix, recordarray, MaskedArray, and MaskedRecord types (of which the latter two define an identical _ShapeType type variable), but their methods also appear to return Any as the shape of any return that isn't the same shape.

Would you mind if I PR'd this and saw if it passes type checking?

@Jacob-Stevens-Haas Jacob-Stevens-Haas changed the title ENH: _ShapeType should be covariant ENH: Should _ShapeType be covariant? Jan 30, 2024
@rgommers rgommers added this to the 2.1.0 release milestone Aug 7, 2024
ArvidJB pushed a commit to ArvidJB/numpy that referenced this issue Nov 1, 2024
Fixes numpy#25729

This change allows future changes to the static typing of numpy that modify
or only work with certain numbers of dimensions.  It also applies the change
to subclasses of ndarray and adds tests.  It allows users to statically
type their array shapes with subtypes of tuple (e.g. NamedTuple) and tuples
of int subtypes (e.g. Literal or NewType).

For a discussion of the merits of TypeVarTuple vs a tuple-bound TypeVar, see
the linked PR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants