Skip to content
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

Returning same generic type as input in NDArray #107

Open
nonnull-ca opened this issue Apr 24, 2023 · 0 comments
Open

Returning same generic type as input in NDArray #107

nonnull-ca opened this issue Apr 24, 2023 · 0 comments

Comments

@nonnull-ca
Copy link

nonnull-ca commented Apr 24, 2023

I'm probably just missing something obvious here; how do I specify a function that takes an arbitrary 2d or higher ndarray and returns an array of the same type, but with the first two dimensions changed?

First problem is the dtype. The obvious approach:

# This works
def resize_3d_int8(arr: NDArray[Shape["A, B, C"], Int8]) -> NDArray[Shape["*, *, C"], Int8]:
    return arr # I know this is a silly example.

# ...but this generic version doesn't.

T = TypeVar("T")
def resize_generic(arr: NDArray[Shape["A, B, ..."], T]) -> NDArray[Shape["*, *, ..."], T]:
    return arr # I know this is a silly example.

...doesn't work:

Type argument "T" of "ndarray" must be a subtype of "dtype[Any]"

I've tried various attempts at declaring T as a subtype of dtype[Any], but haven't had any luck.

You can of course use Any, but that would be too relaxed. It would allow a function to always return an ndarry of floats regardless of input dtype, for instance.

Second problem is that, even assuming said dtype issue can get sorted, this type constraint is still too relaxed. If I pass in an array of shape (2, 3, 4), I should get out an array of shape (*, 3, 4), but this constraint instead says the output is (*, ...) - it has lost the dimensions (and # of dimensions).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant