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

TYP,BUG: Reduce argument validation in C-based __class_getitem__ #22222

Merged
merged 1 commit into from Sep 7, 2022

Conversation

charris
Copy link
Member

@charris charris commented Sep 7, 2022

Backport of #22212.

Closes #22185

The __class_getitem__ implementations would previously perform basic validation of the passed value, i.e. it would check whether a tuple of the appropriate length was passed (e.g. np.dtype.__class_getitem__ would expect a single item or a length-1 tuple). As noted in aforementioned issue: this approach can cause issues when (a. 2 or more parameters are involved and (b. a subclasses is created one or more parameters are declared constant (e.g. a fixed dtype & variably shaped array).

This PR fixes aforementioned issue by removing any and all runtime argument validation, thus mimicking the behavior of the standard library. While we could alternatively fix this by adding more special casing (e.g. only disable validation when cls is not np.ndarray), I'm not convinced this would be worth the additional complexity, especially since the standard library also has zero runtime validation for all of its Py_GenericAlias-based implementations of __class_getitem__.

Examples

The issue prior to this PR:

In [1]: import numpy as np
   ...: from typing import TypeVar, Any

In [2]: ShapeType = TypeVar("ShapeType")

# Variable shaped & fixed dtype
In [3]: class FooArray(np.ndarray[ShapeType , np.dtype[np.int64]]): ...

# Uhoh, __class_getitem__ still expects both a shape and dtype parameter
In [4]: FooArray[Any]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 FooArray[Any]

TypeError: Too few arguments for FooArray

…umpy#22212)

Closes numpy#22185

The __class_getitem__ implementations would previously perform basic validation of the passed value, i.e. it would check whether a tuple of the appropriate length was passed (e.g. np.dtype.__class_getitem__ would expect a single item or a length-1 tuple). As noted in aforementioned issue: this approach can cause issues when (a. 2 or more parameters are involved and (b. a subclasses is created one or more parameters are declared constant (e.g. a fixed dtype & variably shaped array).

This PR fixes aforementioned issue by relaxing the runtime argument validation, thus mimicking the behavior of the standard library (more closely). While we could alternatively fix this by adding more special casing (e.g. only disable validation when cls is not np.ndarray), I'm not convinced this would be worth the additional complexity, especially since the standard library also has zero runtime validation for all of its Py_GenericAlias-based implementations of __class_getitem__.

(Some edits by seberg to the commit message)
@charris charris added this to the 1.23.3 release milestone Sep 7, 2022
@charris charris merged commit 754ec89 into numpy:maintenance/1.23.x Sep 7, 2022
@charris charris deleted the backport-22212 branch September 7, 2022 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants