Skip to content

Commit

Permalink
refactor: typing module re-organization
Browse files Browse the repository at this point in the history
Type safe types are no longer called strict, but type_safe; typing namespace free of type files, but submodules, and the type files are now in the loading submodule.
  • Loading branch information
caniko committed Dec 24, 2024
1 parent aeccd66 commit 76aef06
Show file tree
Hide file tree
Showing 20 changed files with 846 additions and 773 deletions.
740 changes: 400 additions & 340 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion pydantic_numpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from pydantic_numpy.helper.annotation import np_array_pydantic_annotated_typing
from pydantic_numpy.typing.n_dimensional import *

__all__ = ["np_array_pydantic_annotated_typing", "model", "typing"]
12 changes: 6 additions & 6 deletions pydantic_numpy/helper/annotation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections.abc import Sequence
from pathlib import Path
from typing import Any, Callable, ClassVar, Iterable, Optional, Union
from typing import Any, Callable, ClassVar, Iterable, Optional, Union, cast

import numpy as np
import numpy.typing as npt
Expand Down Expand Up @@ -55,12 +55,12 @@ def pd_np_native_numpy_array_to_data_dict_serializer(array_like: npt.ArrayLike)
"""
array = np.array(array_like)

if issubclass(array.dtype.type, np.timedelta64) or issubclass(array.dtype.type, np.datetime64):
data = array.astype(int).tolist()
else:
data = array.astype(float).tolist()
data = array.astype(
int if issubclass(array.dtype.type, np.timedelta64) or issubclass(array.dtype.type, np.datetime64) else float
).tolist()
cast_data = cast(list, data)

return NumpyArrayTypeData(data_type=str(array.dtype), data=data)
return NumpyArrayTypeData(data_type=str(array.dtype), data=cast_data)


def pd_np_native_numpy_array_json_schema_from_type_data(
Expand Down
12 changes: 4 additions & 8 deletions pydantic_numpy/typing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
from pydantic_numpy.typing.i_dimensional import *
from pydantic_numpy.typing.ii_dimensional import *
from pydantic_numpy.typing.iii_dimensional import *
from pydantic_numpy.typing.n_dimensional import *
from pydantic_numpy.typing.strict_data_type.i_dimensional import *
from pydantic_numpy.typing.strict_data_type.ii_dimensional import *
from pydantic_numpy.typing.strict_data_type.iii_dimensional import *
from pydantic_numpy.typing.strict_data_type.n_dimensional import *
from pydantic_numpy.typing.type_safe.i_dimensional import *
from pydantic_numpy.typing.type_safe.ii_dimensional import *
from pydantic_numpy.typing.type_safe.iii_dimensional import *
from pydantic_numpy.typing.type_safe.n_dimensional import *
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,116 +1,122 @@
from typing import Annotated
from typing import Annotated, Any, TypeAlias

import numpy as np

from pydantic_numpy.helper.annotation import NpArrayPydanticAnnotation

NpStrict1DArrayInt64 = Annotated[
Np1DArray: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[Any]],
NpArrayPydanticAnnotation.factory(data_type=None, dimensions=1, strict_data_typing=False),
]

Np1DArrayInt64: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[np.int64]],
NpArrayPydanticAnnotation.factory(data_type=np.int64, dimensions=1, strict_data_typing=True),
]

NpStrict1DArrayInt32 = Annotated[
Np1DArrayInt32: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[np.int32]],
NpArrayPydanticAnnotation.factory(data_type=np.int32, dimensions=1, strict_data_typing=True),
]

NpStrict1DArrayInt16 = Annotated[
Np1DArrayInt16: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[np.int16]],
NpArrayPydanticAnnotation.factory(data_type=np.int16, dimensions=1, strict_data_typing=True),
]

NpStrict1DArrayInt8 = Annotated[
Np1DArrayInt8: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[np.int8]],
NpArrayPydanticAnnotation.factory(data_type=np.int8, dimensions=1, strict_data_typing=True),
]

NpStrict1DArrayUint64 = Annotated[
Np1DArrayUint64: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[np.uint64]],
NpArrayPydanticAnnotation.factory(data_type=np.uint64, dimensions=1, strict_data_typing=True),
]

NpStrict1DArrayUint32 = Annotated[
Np1DArrayUint32: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[np.uint32]],
NpArrayPydanticAnnotation.factory(data_type=np.uint32, dimensions=1, strict_data_typing=True),
]

NpStrict1DArrayUint16 = Annotated[
Np1DArrayUint16: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[np.uint16]],
NpArrayPydanticAnnotation.factory(data_type=np.uint16, dimensions=1, strict_data_typing=True),
]

NpStrict1DArrayUint8 = Annotated[
Np1DArrayUint8: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[np.uint8]],
NpArrayPydanticAnnotation.factory(data_type=np.uint8, dimensions=1, strict_data_typing=True),
]

NpStrict1DArrayFpLongDouble = Annotated[
Np1DArrayFpLongDouble: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[np.longdouble]],
NpArrayPydanticAnnotation.factory(data_type=np.longdouble, dimensions=1, strict_data_typing=True),
]

NpStrict1DArrayFp64 = Annotated[
Np1DArrayFp64: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[np.float64]],
NpArrayPydanticAnnotation.factory(data_type=np.float64, dimensions=1, strict_data_typing=True),
]

NpStrict1DArrayFp32 = Annotated[
Np1DArrayFp32: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[np.float32]],
NpArrayPydanticAnnotation.factory(data_type=np.float32, dimensions=1, strict_data_typing=True),
]

NpStrict1DArrayFp16 = Annotated[
Np1DArrayFp16: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[np.float16]],
NpArrayPydanticAnnotation.factory(data_type=np.float16, dimensions=1, strict_data_typing=True),
]

NpStrict1DArrayComplexLongDouble = Annotated[
Np1DArrayComplexLongDouble: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[np.clongdouble]],
NpArrayPydanticAnnotation.factory(data_type=np.clongdouble, dimensions=1, strict_data_typing=True),
]

NpStrict1DArrayComplex128 = Annotated[
Np1DArrayComplex128: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[np.complex128]],
NpArrayPydanticAnnotation.factory(data_type=np.complex128, dimensions=1, strict_data_typing=True),
]

NpStrict1DArrayComplex64 = Annotated[
Np1DArrayComplex64: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[np.complex64]],
NpArrayPydanticAnnotation.factory(data_type=np.complex64, dimensions=1, strict_data_typing=True),
]

NpStrict1DArrayBool = Annotated[
Np1DArrayBool: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[np.bool_]],
NpArrayPydanticAnnotation.factory(data_type=np.bool_, dimensions=1, strict_data_typing=True),
]

NpStrict1DArrayDatetime64 = Annotated[
Np1DArrayDatetime64: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[np.datetime64]],
NpArrayPydanticAnnotation.factory(data_type=np.datetime64, dimensions=1, strict_data_typing=True),
]

NpStrict1DArrayTimedelta64 = Annotated[
Np1DArrayTimedelta64: TypeAlias = Annotated[
np.ndarray[tuple[int], np.dtype[np.timedelta64]],
NpArrayPydanticAnnotation.factory(data_type=np.timedelta64, dimensions=1, strict_data_typing=True),
]

__all__ = [
"NpStrict1DArrayInt64",
"NpStrict1DArrayInt32",
"NpStrict1DArrayInt16",
"NpStrict1DArrayInt8",
"NpStrict1DArrayUint64",
"NpStrict1DArrayUint32",
"NpStrict1DArrayUint16",
"NpStrict1DArrayUint8",
"NpStrict1DArrayFpLongDouble",
"NpStrict1DArrayFp64",
"NpStrict1DArrayFp32",
"NpStrict1DArrayFp16",
"NpStrict1DArrayComplexLongDouble",
"NpStrict1DArrayComplex128",
"NpStrict1DArrayComplex64",
"NpStrict1DArrayBool",
"NpStrict1DArrayDatetime64",
"NpStrict1DArrayTimedelta64",
"Np1DArray",
"Np1DArrayInt64",
"Np1DArrayInt32",
"Np1DArrayInt16",
"Np1DArrayInt8",
"Np1DArrayUint64",
"Np1DArrayUint32",
"Np1DArrayUint16",
"Np1DArrayUint8",
"Np1DArrayFpLongDouble",
"Np1DArrayFp64",
"Np1DArrayFp32",
"Np1DArrayFp16",
"Np1DArrayComplexLongDouble",
"Np1DArrayComplex128",
"Np1DArrayComplex64",
"Np1DArrayBool",
"Np1DArrayDatetime64",
"Np1DArrayTimedelta64",
]
Original file line number Diff line number Diff line change
@@ -1,116 +1,122 @@
from typing import Annotated
from typing import Annotated, Any, TypeAlias

import numpy as np

from pydantic_numpy.helper.annotation import NpArrayPydanticAnnotation

NpStrict2DArrayInt64 = Annotated[
Np2DArray: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[Any]],
NpArrayPydanticAnnotation.factory(data_type=None, dimensions=2, strict_data_typing=False),
]

Np2DArrayInt64: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[np.int64]],
NpArrayPydanticAnnotation.factory(data_type=np.int64, dimensions=2, strict_data_typing=True),
]

NpStrict2DArrayInt32 = Annotated[
Np2DArrayInt32: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[np.int32]],
NpArrayPydanticAnnotation.factory(data_type=np.int32, dimensions=2, strict_data_typing=True),
]

NpStrict2DArrayInt16 = Annotated[
Np2DArrayInt16: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[np.int16]],
NpArrayPydanticAnnotation.factory(data_type=np.int16, dimensions=2, strict_data_typing=True),
]

NpStrict2DArrayInt8 = Annotated[
Np2DArrayInt8: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[np.int8]],
NpArrayPydanticAnnotation.factory(data_type=np.int8, dimensions=2, strict_data_typing=True),
]

NpStrict2DArrayUint64 = Annotated[
Np2DArrayUint64: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[np.uint64]],
NpArrayPydanticAnnotation.factory(data_type=np.uint64, dimensions=2, strict_data_typing=True),
]

NpStrict2DArrayUint32 = Annotated[
Np2DArrayUint32: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[np.uint32]],
NpArrayPydanticAnnotation.factory(data_type=np.uint32, dimensions=2, strict_data_typing=True),
]

NpStrict2DArrayUint16 = Annotated[
Np2DArrayUint16: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[np.uint16]],
NpArrayPydanticAnnotation.factory(data_type=np.uint16, dimensions=2, strict_data_typing=True),
]

NpStrict2DArrayUint8 = Annotated[
Np2DArrayUint8: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[np.uint8]],
NpArrayPydanticAnnotation.factory(data_type=np.uint8, dimensions=2, strict_data_typing=True),
]

NpStrict2DArrayFpLongDouble = Annotated[
Np2DArrayFpLongDouble: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[np.longdouble]],
NpArrayPydanticAnnotation.factory(data_type=np.longdouble, dimensions=2, strict_data_typing=True),
]

NpStrict2DArrayFp64 = Annotated[
Np2DArrayFp64: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[np.float64]],
NpArrayPydanticAnnotation.factory(data_type=np.float64, dimensions=2, strict_data_typing=True),
]

NpStrict2DArrayFp32 = Annotated[
Np2DArrayFp32: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[np.float32]],
NpArrayPydanticAnnotation.factory(data_type=np.float32, dimensions=2, strict_data_typing=True),
]

NpStrict2DArrayFp16 = Annotated[
Np2DArrayFp16: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[np.float16]],
NpArrayPydanticAnnotation.factory(data_type=np.float16, dimensions=2, strict_data_typing=True),
]

NpStrict2DArrayComplexLongDouble = Annotated[
Np2DArrayComplexLongDouble: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[np.clongdouble]],
NpArrayPydanticAnnotation.factory(data_type=np.clongdouble, dimensions=2, strict_data_typing=True),
]

NpStrict2DArrayComplex128 = Annotated[
Np2DArrayComplex128: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[np.complex128]],
NpArrayPydanticAnnotation.factory(data_type=np.complex128, dimensions=2, strict_data_typing=True),
]

NpStrict2DArrayComplex64 = Annotated[
Np2DArrayComplex64: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[np.complex64]],
NpArrayPydanticAnnotation.factory(data_type=np.complex64, dimensions=2, strict_data_typing=True),
]

NpStrict2DArrayBool = Annotated[
Np2DArrayBool: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[np.bool_]],
NpArrayPydanticAnnotation.factory(data_type=np.bool_, dimensions=2, strict_data_typing=True),
]

NpStrict2DArrayDatetime64 = Annotated[
Np2DArrayDatetime64: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[np.datetime64]],
NpArrayPydanticAnnotation.factory(data_type=np.datetime64, dimensions=2, strict_data_typing=True),
]

NpStrict2DArrayTimedelta64 = Annotated[
Np2DArrayTimedelta64: TypeAlias = Annotated[
np.ndarray[tuple[int, int], np.dtype[np.timedelta64]],
NpArrayPydanticAnnotation.factory(data_type=np.timedelta64, dimensions=2, strict_data_typing=True),
]

__all__ = [
"NpStrict2DArrayInt64",
"NpStrict2DArrayInt32",
"NpStrict2DArrayInt16",
"NpStrict2DArrayInt8",
"NpStrict2DArrayUint64",
"NpStrict2DArrayUint32",
"NpStrict2DArrayUint16",
"NpStrict2DArrayUint8",
"NpStrict2DArrayFpLongDouble",
"NpStrict2DArrayFp64",
"NpStrict2DArrayFp32",
"NpStrict2DArrayFp16",
"NpStrict2DArrayComplexLongDouble",
"NpStrict2DArrayComplex128",
"NpStrict2DArrayComplex64",
"NpStrict2DArrayBool",
"NpStrict2DArrayDatetime64",
"NpStrict2DArrayTimedelta64",
"Np2DArray",
"Np2DArrayInt64",
"Np2DArrayInt32",
"Np2DArrayInt16",
"Np2DArrayInt8",
"Np2DArrayUint64",
"Np2DArrayUint32",
"Np2DArrayUint16",
"Np2DArrayUint8",
"Np2DArrayFpLongDouble",
"Np2DArrayFp64",
"Np2DArrayFp32",
"Np2DArrayFp16",
"Np2DArrayComplexLongDouble",
"Np2DArrayComplex128",
"Np2DArrayComplex64",
"Np2DArrayBool",
"Np2DArrayDatetime64",
"Np2DArrayTimedelta64",
]
Loading

0 comments on commit 76aef06

Please sign in to comment.