From dba550f41b45b19936ed6ddccab535de36014643 Mon Sep 17 00:00:00 2001 From: sneakers-the-rat Date: Wed, 31 Jul 2024 13:15:17 -0700 Subject: [PATCH] lint and format nptyping --- src/numpydantic/vendor/__init__.py | 10 ++++++++++ src/numpydantic/vendor/nptyping/__init__.py | 1 + .../vendor/nptyping/assert_isinstance.py | 1 + .../vendor/nptyping/base_meta_classes.py | 15 +++++++------- src/numpydantic/vendor/nptyping/ndarray.py | 2 +- .../vendor/nptyping/nptyping_type.py | 3 ++- .../vendor/nptyping/package_info.py | 1 + .../vendor/nptyping/pandas_/__init__.py | 3 +++ .../vendor/nptyping/pandas_/dataframe.py | 4 ++-- .../vendor/nptyping/pandas_/typing_.py | 1 + src/numpydantic/vendor/nptyping/recarray.py | 2 +- src/numpydantic/vendor/nptyping/shape.py | 1 + src/numpydantic/vendor/nptyping/shape.pyi | 1 + .../vendor/nptyping/shape_expression.py | 3 ++- src/numpydantic/vendor/nptyping/structure.py | 1 + src/numpydantic/vendor/nptyping/structure.pyi | 1 + .../vendor/nptyping/structure_expression.py | 5 ++--- src/numpydantic/vendor/nptyping/typing_.py | 20 +++++-------------- 18 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/numpydantic/vendor/__init__.py b/src/numpydantic/vendor/__init__.py index e69de29..9af2c76 100644 --- a/src/numpydantic/vendor/__init__.py +++ b/src/numpydantic/vendor/__init__.py @@ -0,0 +1,10 @@ +""" +Vendored modules - see licenses in /licenses. + +Currently consists just of nptyping, as it is no longer maintained +and pins a version of numpy<2, and we have to do an increasing +number of awkward monkeypatches for perf and customization reasons. + +This vendored module will slowly be worked out of the code and +all its functionality replaced. +""" diff --git a/src/numpydantic/vendor/nptyping/__init__.py b/src/numpydantic/vendor/nptyping/__init__.py index 5fd5b2c..35e11cc 100644 --- a/src/numpydantic/vendor/nptyping/__init__.py +++ b/src/numpydantic/vendor/nptyping/__init__.py @@ -21,6 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ + from nptyping.assert_isinstance import assert_isinstance from nptyping.error import ( InvalidArgumentsError, diff --git a/src/numpydantic/vendor/nptyping/assert_isinstance.py b/src/numpydantic/vendor/nptyping/assert_isinstance.py index 5ae4eb1..e6d4187 100644 --- a/src/numpydantic/vendor/nptyping/assert_isinstance.py +++ b/src/numpydantic/vendor/nptyping/assert_isinstance.py @@ -21,6 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ + from typing import ( Any, Optional, diff --git a/src/numpydantic/vendor/nptyping/base_meta_classes.py b/src/numpydantic/vendor/nptyping/base_meta_classes.py index b9ca13a..c2ca1e3 100644 --- a/src/numpydantic/vendor/nptyping/base_meta_classes.py +++ b/src/numpydantic/vendor/nptyping/base_meta_classes.py @@ -21,6 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ + from abc import ABCMeta, abstractmethod from inspect import FrameInfo from typing import ( @@ -44,6 +45,7 @@ class InconstructableMeta(ABCMeta): """ def __call__(cls, *_: Any, **__: Any) -> None: + """Raise an error if constructed""" raise NPTypingError( f"Cannot instantiate nptyping.{cls.__name__}. Did you mean to use [ ] ?" ) @@ -76,6 +78,7 @@ class FinalMeta(ABCMeta): cls._name_per_meta_cls[cls] = implementation def __new__(cls, name: str, *args: Any, **kwargs: Any) -> type: + """Prevent subclasses, return from internal dict instead""" if name == cls._name_per_meta_cls[cls]: assert name, "cls_name not set" return type.__new__(cls, name, *args, **kwargs) @@ -105,8 +108,7 @@ class PrintableMeta(ABCMeta): """ @abstractmethod - def __str__(cls) -> str: - ... # pragma: no cover + def __str__(cls) -> str: ... # pragma: no cover def __repr__(cls) -> str: return str(cls) @@ -122,8 +124,7 @@ class SubscriptableMeta(ABCMeta): _parameterized: bool = False @abstractmethod - def _get_item(cls, item: Any) -> Tuple[Any, ...]: - ... # pragma: no cover + def _get_item(cls, item: Any) -> Tuple[Any, ...]: ... # pragma: no cover def _get_module(cls, stack: List[FrameInfo], module: str) -> str: # The magic below makes Python's help function display a meaningful @@ -201,12 +202,10 @@ class ContainerMeta( __args__: Tuple[str, ...] @abstractmethod - def _validate_expression(cls, item: str) -> None: - ... # pragma: no cover + def _validate_expression(cls, item: str) -> None: ... # pragma: no cover @abstractmethod - def _normalize_expression(cls, item: str) -> str: - ... # pragma: no cover + def _normalize_expression(cls, item: str) -> str: ... # pragma: no cover def _get_item(cls, item: Any) -> Tuple[Any, ...]: if not isinstance(item, str): diff --git a/src/numpydantic/vendor/nptyping/ndarray.py b/src/numpydantic/vendor/nptyping/ndarray.py index adeea12..a3a8daf 100644 --- a/src/numpydantic/vendor/nptyping/ndarray.py +++ b/src/numpydantic/vendor/nptyping/ndarray.py @@ -21,12 +21,12 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ + import inspect from abc import ABC from typing import Any, Tuple import numpy as np - from nptyping.base_meta_classes import ( FinalMeta, ImmutableMeta, diff --git a/src/numpydantic/vendor/nptyping/nptyping_type.py b/src/numpydantic/vendor/nptyping/nptyping_type.py index 35d4897..f8d90f0 100644 --- a/src/numpydantic/vendor/nptyping/nptyping_type.py +++ b/src/numpydantic/vendor/nptyping/nptyping_type.py @@ -21,10 +21,11 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ + from abc import ABC -class NPTypingType(ABC): +class NPTypingType(ABC): # noqa: B024 """ Baseclass for all nptyping types. """ diff --git a/src/numpydantic/vendor/nptyping/package_info.py b/src/numpydantic/vendor/nptyping/package_info.py index ee15ac8..d72b631 100644 --- a/src/numpydantic/vendor/nptyping/package_info.py +++ b/src/numpydantic/vendor/nptyping/package_info.py @@ -21,6 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ + __title__ = "nptyping" __version__ = "2.5.0" __author__ = "Ramon Hagenaars" diff --git a/src/numpydantic/vendor/nptyping/pandas_/__init__.py b/src/numpydantic/vendor/nptyping/pandas_/__init__.py index e69de29..c74bb5a 100644 --- a/src/numpydantic/vendor/nptyping/pandas_/__init__.py +++ b/src/numpydantic/vendor/nptyping/pandas_/__init__.py @@ -0,0 +1,3 @@ +""" +Pandas dataframe types +""" diff --git a/src/numpydantic/vendor/nptyping/pandas_/dataframe.py b/src/numpydantic/vendor/nptyping/pandas_/dataframe.py index e108b27..9dd5ee7 100644 --- a/src/numpydantic/vendor/nptyping/pandas_/dataframe.py +++ b/src/numpydantic/vendor/nptyping/pandas_/dataframe.py @@ -21,12 +21,12 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ + import inspect from abc import ABC from typing import Any, Tuple import numpy as np - from nptyping import InvalidArgumentsError from nptyping.base_meta_classes import ( FinalMeta, @@ -91,7 +91,7 @@ class DataFrameMeta( if item is Any: return (Any,) cls._check_item(item) - return (Structure[getattr(item, "__args__")[0]],) + return (Structure[item.__args__[0]],) def __str__(cls) -> str: structure = cls.__args__[0] diff --git a/src/numpydantic/vendor/nptyping/pandas_/typing_.py b/src/numpydantic/vendor/nptyping/pandas_/typing_.py index 29f2188..37dfdbe 100644 --- a/src/numpydantic/vendor/nptyping/pandas_/typing_.py +++ b/src/numpydantic/vendor/nptyping/pandas_/typing_.py @@ -21,6 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ + from nptyping.typing_ import Object from nptyping.typing_ import dtype_per_name as dtype_per_name_default diff --git a/src/numpydantic/vendor/nptyping/recarray.py b/src/numpydantic/vendor/nptyping/recarray.py index b3bbbc5..a90b13b 100644 --- a/src/numpydantic/vendor/nptyping/recarray.py +++ b/src/numpydantic/vendor/nptyping/recarray.py @@ -21,11 +21,11 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ + import inspect from typing import Any, Tuple import numpy as np - from nptyping.error import InvalidArgumentsError from nptyping.ndarray import NDArray, NDArrayMeta from nptyping.structure import Structure diff --git a/src/numpydantic/vendor/nptyping/shape.py b/src/numpydantic/vendor/nptyping/shape.py index 5dc05f5..c30aefc 100644 --- a/src/numpydantic/vendor/nptyping/shape.py +++ b/src/numpydantic/vendor/nptyping/shape.py @@ -21,6 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ + from abc import ABC from typing import Any, Dict diff --git a/src/numpydantic/vendor/nptyping/shape.pyi b/src/numpydantic/vendor/nptyping/shape.pyi index 96d2eb6..eeb64ed 100644 --- a/src/numpydantic/vendor/nptyping/shape.pyi +++ b/src/numpydantic/vendor/nptyping/shape.pyi @@ -21,6 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ + try: from typing import Literal # type: ignore[attr-defined] except ImportError: diff --git a/src/numpydantic/vendor/nptyping/shape_expression.py b/src/numpydantic/vendor/nptyping/shape_expression.py index bcaf79b..8d55a56 100644 --- a/src/numpydantic/vendor/nptyping/shape_expression.py +++ b/src/numpydantic/vendor/nptyping/shape_expression.py @@ -21,6 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ + import re import string from functools import lru_cache @@ -39,7 +40,7 @@ if TYPE_CHECKING: from nptyping.shape import Shape # pragma: no cover -@lru_cache() +@lru_cache def check_shape(shape: ShapeTuple, target: "Shape") -> bool: """ Check whether the given shape corresponds to the given shape_expression. diff --git a/src/numpydantic/vendor/nptyping/structure.py b/src/numpydantic/vendor/nptyping/structure.py index 0b95dfb..71da5ef 100644 --- a/src/numpydantic/vendor/nptyping/structure.py +++ b/src/numpydantic/vendor/nptyping/structure.py @@ -21,6 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ + from abc import ABC from typing import ( Any, diff --git a/src/numpydantic/vendor/nptyping/structure.pyi b/src/numpydantic/vendor/nptyping/structure.pyi index 415cf4e..608c094 100644 --- a/src/numpydantic/vendor/nptyping/structure.pyi +++ b/src/numpydantic/vendor/nptyping/structure.pyi @@ -21,6 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ + try: from typing import Literal # type: ignore[attr-defined] except ImportError: diff --git a/src/numpydantic/vendor/nptyping/structure_expression.py b/src/numpydantic/vendor/nptyping/structure_expression.py index ee1236c..f175381 100644 --- a/src/numpydantic/vendor/nptyping/structure_expression.py +++ b/src/numpydantic/vendor/nptyping/structure_expression.py @@ -21,6 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ + import re from collections import Counter, defaultdict from difflib import get_close_matches @@ -37,7 +38,6 @@ from typing import ( ) import numpy as np - from nptyping.error import InvalidShapeError, InvalidStructureError from nptyping.shape import Shape from nptyping.shape_expression import ( @@ -109,8 +109,7 @@ def check_structure( return False def iterator() -> Generator[Tuple[str, Tuple[np.dtype, int]], None, None]: # type: ignore[type-arg] # pylint: disable=line-too-long - for name_, dtype_tuple_ in fields.items(): - yield name_, dtype_tuple_ # type: ignore[misc] + yield from fields.items() for name, dtype_tuple in iterator(): field_in_target_not_in_subject = dtype_tuple is None diff --git a/src/numpydantic/vendor/nptyping/typing_.py b/src/numpydantic/vendor/nptyping/typing_.py index 2639e07..8f86e33 100644 --- a/src/numpydantic/vendor/nptyping/typing_.py +++ b/src/numpydantic/vendor/nptyping/typing_.py @@ -22,22 +22,12 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -try: - from typing import ( # type: ignore[attr-defined,misc] # pylint: disable=unused-import - Literal, - TypeAlias, - TypeGuard, - final, - ) -except ImportError: # pragma: no cover - from typing_extensions import ( # type: ignore[attr-defined,misc] - Literal, - TypeAlias, - TypeGuard, - final, - ) -from typing import Tuple, Union +from typing import ( # type: ignore[attr-defined,misc] # pylint: disable=unused-import + Tuple, + TypeAlias, + Union, +) import numpy as np