Skip to content

numpy.typing 2.2 breaks compatibility with type annotations for pydantic_numpy #59

@futurewasfree

Description

@futurewasfree

Not sure where to report really, but I'll start the discussion here.

So, prior to 2.2. we've had the following definition of numpy.typing.NDArray:

NDArray = ndarray[Any, dtype[_ScalarType_co]]

which is quite handy to use in our codebase to type return values of the functions,
e.g. npt.NDArray[np.uint8], but as you could see it doesn't carry any knowledge on number of dimensions used by this array.

in 2.2 they've changed the definition of NDArray to:

NDArray: TypeAlias = ndarray[_Shape, dtype[_ScalarType_co]]

where _Shape

_Shape: TypeAlias = tuple[int, ...]

and I can't see any concrete 1D, 2D, 3D array counterparts in numpy.typing

so then in 2.2 you get the following static analysis (pyright) error when you pass NDArray[np.uint8] to
numpy field in pydantic model:

[redacted] - error: Argument of type "NDArray[uint8]" cannot be assigned to parameter "img" of type "NpStrict3DArrayUint8" in function "__init__"
    "ndarray[_Shape, dtype[uint8]]" is not assignable to "ndarray[tuple[int, int, int], dtype[uint8]]"
      Type parameter "_ShapeT_co@ndarray" is covariant, but "_Shape" is not a subtype of "tuple[int, int, int]"
        "tuple[int, ...]" is not assignable to "tuple[int, int, int]"
          Tuple size mismatch; expected 3 but received indeterminate (reportArgumentType)

which is kinda make sense, as we're narrowing type to concrete dimesions.
and to me type definition prior to 2.2 should also trigger the same array (narrowing Any to tuple[int, int, int]), but it's not :P

So I'm basically looking for any words of wisdom here.
Is that something to report to pyright even?
Or we should have our own definitions of NDArray2D, NDArray3D?
Or we should relax static types in the lib itself?

Metadata

Metadata

Assignees

No one assigned

    Labels

    BreakingChangeUpstreamChanges made to numpy that broke the package

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions