Skip to content

Commit

Permalink
Merge pull request #1 from caniko/main
Browse files Browse the repository at this point in the history
Feat: __modify_schema__ for pydantic.schema_of support
  • Loading branch information
cheind authored May 9, 2022
2 parents 54055e0 + e0aa5c4 commit 88c1288
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pydantic_numpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@
T = TypeVar("T", bound=np.generic)


class BaseNDArray:
@classmethod
def __modify_schema__(cls, field_schema):
# __modify_schema__ should mutate the dict it receives in place,
# the returned value will be ignored
field_schema.update({
"type": "np.ndarray"
})


class NPFileDesc(BaseModel):
path: Path
key: Optional[str]


if NumpyVersion(np.__version__) < "1.22.0":

class NDArray(Generic[T], np.ndarray):
class NDArray(Generic[T], np.ndarray, BaseNDArray):
@classmethod
def __get_validators__(cls):
yield cls.validate
Expand All @@ -25,7 +35,7 @@ def __get_validators__(cls):
def validate(cls, val: Any, field: ModelField) -> np.ndarray:
return _validate(cls, val, field)

class PotentialNDArray(Generic[T], np.ndarray):
class PotentialNDArray(Generic[T], np.ndarray, BaseNDArray):
"""Like NDArray, but validation errors result in None."""

@classmethod
Expand All @@ -42,7 +52,7 @@ def validate(cls, val: Any, field: ModelField) -> Optional[np.ndarray]:

else:

class NDArray(Generic[T], np.ndarray[Any, T]):
class NDArray(Generic[T], np.ndarray[Any, T], BaseNDArray):
@classmethod
def __get_validators__(cls):
yield cls.validate
Expand All @@ -51,7 +61,7 @@ def __get_validators__(cls):
def validate(cls, val: Any, field: ModelField) -> Optional[np.ndarray]:
return _validate(cls, val, field)

class PotentialNDArray(Generic[T], np.ndarray[Any, T]):
class PotentialNDArray(Generic[T], np.ndarray[Any, T], BaseNDArray):
@classmethod
def __get_validators__(cls):
yield cls.validate
Expand Down

0 comments on commit 88c1288

Please sign in to comment.