p2p-ld / numpydantic

Type annotations for specifying, validating, and serializing arrays with arbitrary backends in Pydantic (and beyond)
https://numpydantic.readthedocs.io/
MIT License
66 stars 1 forks source link

Support pydantic annotated/constrained value types #18

Open sneakers-the-rat opened 2 months ago

sneakers-the-rat commented 2 months ago

From: https://github.com/LiberTEM/LiberTEM-schema/pull/7

The example given was:

from typing_extensions import Annotated
from pydantic import FiniteFloat, BaseModel
from numpydantic import Shape, NDArray

class T(BaseModel):
    t: NDArray[Shape['2, 2'], FiniteFloat]

We should refactor the dtype check out into a separate class with classmethods to be able to expand dtype checks for the future, the current method is likely to end up with an enormous brittle if/else chain otherwise.

We could allow generic support for all pydantic types here: https://docs.pydantic.dev/latest/api/types/

Some of these checks will have a faster vectorized method, like eg. Positive* shouldn't need to iterate over every value but just use np.all(array > 0). For any that don't have specific vectorized operations, we should just map that check across values of the array.

In pydantic implementation, these are all annotated types (eg. Annotated[int, Le(0)]), so that's what we should target for support.