From 33067037413704ef8a6ddc75f6cfaccaa06160f7 Mon Sep 17 00:00:00 2001 From: sneakers-the-rat Date: Wed, 31 Jul 2024 14:15:24 -0700 Subject: [PATCH] update old numpy dtypes, remove them, lint --- pyproject.toml | 4 +++ src/numpydantic/dtype.py | 14 ++------ src/numpydantic/interface/video.py | 6 ++-- src/numpydantic/maps.py | 2 +- src/numpydantic/ndarray.py | 16 ++++----- src/numpydantic/schema.py | 2 +- src/numpydantic/types.py | 2 +- .../vendor/nptyping/base_meta_classes.py | 3 +- src/numpydantic/vendor/nptyping/ndarray.py | 1 + .../vendor/nptyping/pandas_/dataframe.py | 1 + .../vendor/nptyping/structure_expression.py | 1 + src/numpydantic/vendor/nptyping/typing_.py | 33 +++++-------------- 12 files changed, 33 insertions(+), 52 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 71d6d5e..05b97ea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -101,6 +101,10 @@ select = [ "I", # annotations "ANN", + # perf + "PERF", + # numpy + "NPY", ## ---------- # pydocstyle # undocumented public objects diff --git a/src/numpydantic/dtype.py b/src/numpydantic/dtype.py index 55bcca6..4c0cc80 100644 --- a/src/numpydantic/dtype.py +++ b/src/numpydantic/dtype.py @@ -69,9 +69,7 @@ Half = np.half Single = np.single Double = np.double LongDouble = np.longdouble -LongFloat = np.longfloat Float = ( - np.float_, np.float16, np.float32, np.float64, @@ -84,32 +82,24 @@ ComplexFloating = np.complexfloating Complex64 = np.complex64 Complex128 = np.complex128 CSingle = np.csingle -SingleComplex = np.singlecomplex CDouble = np.cdouble -CFloat = np.cfloat CLongDouble = np.clongdouble -CLongFloat = np.clongfloat Complex = ( - np.complex_, np.complexfloating, np.complex64, np.complex128, np.csingle, - np.singlecomplex, np.cdouble, - np.cfloat, np.clongdouble, - np.clongfloat, ) -LongComplex = np.longcomplex Flexible = np.flexible Void = np.void Character = np.character Bytes = np.bytes_ Str = np.str_ -String = np.string_ -Unicode = np.unicode_ +String = np.str_ +Unicode = np.str_ Number = tuple( [ diff --git a/src/numpydantic/interface/video.py b/src/numpydantic/interface/video.py index 0e1a50d..4f2048d 100644 --- a/src/numpydantic/interface/video.py +++ b/src/numpydantic/interface/video.py @@ -144,9 +144,9 @@ class VideoProxy: elif isinstance(item, slice): # slice of frames item = self._complete_slice(item) - frames = [] - for i in range(item.start, item.stop, item.step): - frames.append(self._get_frame(i)) + frames = [ + self._get_frame(i) for i in range(item.start, item.stop, item.step) + ] return np.stack(frames) else: # slices are passed as tuples diff --git a/src/numpydantic/maps.py b/src/numpydantic/maps.py index 38e4925..d80d33e 100644 --- a/src/numpydantic/maps.py +++ b/src/numpydantic/maps.py @@ -21,7 +21,7 @@ np_to_python = { **{n: int for n in dt.Integer}, **{n: float for n in dt.Float}, **{n: complex for n in dt.Complex}, - **{n: str for n in (np.character, np.str_, np.string_, np.unicode_)}, + **{n: str for n in (np.character, np.str_, np.bytes_, np.str_)}, } """Map from python types to numpy""" diff --git a/src/numpydantic/ndarray.py b/src/numpydantic/ndarray.py index 81efb7f..42fc3f8 100644 --- a/src/numpydantic/ndarray.py +++ b/src/numpydantic/ndarray.py @@ -16,14 +16,6 @@ Extension of nptyping NDArray for pydantic that allows for JSON-Schema serializa from typing import TYPE_CHECKING, Any, Tuple import numpy as np -from numpydantic.vendor.nptyping.error import InvalidArgumentsError -from numpydantic.vendor.nptyping.ndarray import NDArrayMeta as _NDArrayMeta -from numpydantic.vendor.nptyping.nptyping_type import NPTypingType -from numpydantic.vendor.nptyping.structure import Structure -from numpydantic.vendor.nptyping.structure_expression import check_type_names -from numpydantic.vendor.nptyping.typing_ import ( - dtype_per_name, -) from pydantic import GetJsonSchemaHandler from pydantic_core import core_schema @@ -38,6 +30,14 @@ from numpydantic.schema import ( make_json_schema, ) from numpydantic.types import DtypeType, ShapeType +from numpydantic.vendor.nptyping.error import InvalidArgumentsError +from numpydantic.vendor.nptyping.ndarray import NDArrayMeta as _NDArrayMeta +from numpydantic.vendor.nptyping.nptyping_type import NPTypingType +from numpydantic.vendor.nptyping.structure import Structure +from numpydantic.vendor.nptyping.structure_expression import check_type_names +from numpydantic.vendor.nptyping.typing_ import ( + dtype_per_name, +) if TYPE_CHECKING: # pragma: no cover from nptyping.base_meta_classes import SubscriptableMeta diff --git a/src/numpydantic/schema.py b/src/numpydantic/schema.py index 0a6c568..8e7e1bf 100644 --- a/src/numpydantic/schema.py +++ b/src/numpydantic/schema.py @@ -7,7 +7,6 @@ import hashlib import json from typing import TYPE_CHECKING, Any, Callable, Optional, Union -from numpydantic.vendor.nptyping.structure import StructureMeta import numpy as np from pydantic import SerializationInfo from pydantic_core import CoreSchema, core_schema @@ -17,6 +16,7 @@ from numpydantic import dtype as dt from numpydantic.interface import Interface from numpydantic.maps import np_to_python from numpydantic.types import DtypeType, NDArrayType, ShapeType +from numpydantic.vendor.nptyping.structure import StructureMeta if TYPE_CHECKING: from numpydantic import Shape diff --git a/src/numpydantic/types.py b/src/numpydantic/types.py index c629fbf..ef01cee 100644 --- a/src/numpydantic/types.py +++ b/src/numpydantic/types.py @@ -8,7 +8,7 @@ Note that these are types as in python typing types, not classes. from typing import Any, Protocol, Tuple, Union, runtime_checkable -from nptyping import DType +from numpydantic.vendor.nptyping import DType ShapeType = Union[Tuple[int, ...], Any] DtypeType = Union[str, type, Any, DType] diff --git a/src/numpydantic/vendor/nptyping/base_meta_classes.py b/src/numpydantic/vendor/nptyping/base_meta_classes.py index 01b5c12..d943e20 100644 --- a/src/numpydantic/vendor/nptyping/base_meta_classes.py +++ b/src/numpydantic/vendor/nptyping/base_meta_classes.py @@ -23,12 +23,11 @@ SOFTWARE. """ from abc import ABCMeta, abstractmethod -from inspect import FrameInfo, getframeinfo +from inspect import getframeinfo from types import FrameType from typing import ( Any, Dict, - List, Optional, Set, Tuple, diff --git a/src/numpydantic/vendor/nptyping/ndarray.py b/src/numpydantic/vendor/nptyping/ndarray.py index c891af5..90a4793 100644 --- a/src/numpydantic/vendor/nptyping/ndarray.py +++ b/src/numpydantic/vendor/nptyping/ndarray.py @@ -27,6 +27,7 @@ from abc import ABC from typing import Any, Tuple import numpy as np + from numpydantic.vendor.nptyping.base_meta_classes import ( FinalMeta, ImmutableMeta, diff --git a/src/numpydantic/vendor/nptyping/pandas_/dataframe.py b/src/numpydantic/vendor/nptyping/pandas_/dataframe.py index dd270cf..f734acb 100644 --- a/src/numpydantic/vendor/nptyping/pandas_/dataframe.py +++ b/src/numpydantic/vendor/nptyping/pandas_/dataframe.py @@ -27,6 +27,7 @@ from abc import ABC from typing import Any, Tuple import numpy as np + from numpydantic.vendor.nptyping import InvalidArgumentsError from numpydantic.vendor.nptyping.base_meta_classes import ( FinalMeta, diff --git a/src/numpydantic/vendor/nptyping/structure_expression.py b/src/numpydantic/vendor/nptyping/structure_expression.py index dd9d8f1..625f770 100644 --- a/src/numpydantic/vendor/nptyping/structure_expression.py +++ b/src/numpydantic/vendor/nptyping/structure_expression.py @@ -38,6 +38,7 @@ from typing import ( ) import numpy as np + from numpydantic.vendor.nptyping.error import InvalidShapeError, InvalidStructureError from numpydantic.vendor.nptyping.shape import Shape from numpydantic.vendor.nptyping.shape_expression import ( diff --git a/src/numpydantic/vendor/nptyping/typing_.py b/src/numpydantic/vendor/nptyping/typing_.py index 8f86e33..6b05eb6 100644 --- a/src/numpydantic/vendor/nptyping/typing_.py +++ b/src/numpydantic/vendor/nptyping/typing_.py @@ -22,7 +22,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ - from typing import ( # type: ignore[attr-defined,misc] # pylint: disable=unused-import Tuple, TypeAlias, @@ -38,10 +37,8 @@ ShapeTuple: TypeAlias = Tuple[int, ...] Number = np.number Bool = np.bool_ -Bool8 = np.bool8 Obj = np.object_ # Obj is a common abbreviation and should be usable. Object = np.object_ -Object0 = np.object0 Datetime64 = np.datetime64 Integer = np.integer SignedInteger = np.signedinteger @@ -53,7 +50,6 @@ Byte = np.byte Short = np.short IntC = np.intc IntP = np.intp -Int0 = np.int0 Int = np.integer # Int should translate to the "generic" int type. Int_ = np.int_ LongLong = np.longlong @@ -67,7 +63,6 @@ UByte = np.ubyte UShort = np.ushort UIntC = np.uintc UIntP = np.uintp -UInt0 = np.uint0 UInt = np.uint ULongLong = np.ulonglong Inexact = np.inexact @@ -78,38 +73,33 @@ Float64 = np.float64 Half = np.half Single = np.single Double = np.double -Float = np.float_ +Float = np.float64 LongDouble = np.longdouble -LongFloat = np.longfloat +LongFloat = np.longdouble ComplexFloating = np.complexfloating Complex64 = np.complex64 Complex128 = np.complex128 CSingle = np.csingle -SingleComplex = np.singlecomplex +SingleComplex = np.complex64 CDouble = np.cdouble -Complex = np.complex_ -CFloat = np.cfloat +Complex = np.complex128 +CFloat = np.complex128 CLongDouble = np.clongdouble -CLongFloat = np.clongfloat -LongComplex = np.longcomplex +CLongFloat = np.clongdouble +LongComplex = np.clongdouble Flexible = np.flexible Void = np.void -Void0 = np.void0 Character = np.character Bytes = np.bytes_ Str = np.str_ -String = np.string_ -Bytes0 = np.bytes0 -Unicode = np.unicode_ -Str0 = np.str0 +String = np.str_ +Unicode = np.str_ dtypes = [ (Number, "Number"), (Bool, "Bool"), - (Bool8, "Bool8"), (Obj, "Obj"), (Object, "Object"), - (Object0, "Object0"), (Datetime64, "Datetime64"), (Integer, "Integer"), (SignedInteger, "SignedInteger"), @@ -121,7 +111,6 @@ dtypes = [ (Short, "Short"), (IntC, "IntC"), (IntP, "IntP"), - (Int0, "Int0"), (Int, "Int"), (LongLong, "LongLong"), (Timedelta64, "Timedelta64"), @@ -134,7 +123,6 @@ dtypes = [ (UShort, "UShort"), (UIntC, "UIntC"), (UIntP, "UIntP"), - (UInt0, "UInt0"), (UInt, "UInt"), (ULongLong, "ULongLong"), (Inexact, "Inexact"), @@ -161,14 +149,11 @@ dtypes = [ (LongComplex, "LongComplex"), (Flexible, "Flexible"), (Void, "Void"), - (Void0, "Void0"), (Character, "Character"), (Bytes, "Bytes"), (String, "String"), (Str, "Str"), - (Bytes0, "Bytes0"), (Unicode, "Unicode"), - (Str0, "Str0"), ] name_per_dtype = dict(dtypes)