From 4604e7e7bbee2dea4270482986fbc68eb3cd0a59 Mon Sep 17 00:00:00 2001 From: sneakers-the-rat Date: Wed, 31 Jul 2024 14:08:26 -0700 Subject: [PATCH] apply monkeypatches in vendored nptyping, remove monkeypatch module and call --- src/numpydantic/__init__.py | 4 --- src/numpydantic/monkeypatch.py | 66 ---------------------------------- 2 files changed, 70 deletions(-) delete mode 100644 src/numpydantic/monkeypatch.py diff --git a/src/numpydantic/__init__.py b/src/numpydantic/__init__.py index d251f8d..803d8d2 100644 --- a/src/numpydantic/__init__.py +++ b/src/numpydantic/__init__.py @@ -1,10 +1,6 @@ -# ruff: noqa: E402 # ruff: noqa: F401 # ruff: noqa: I001 # ruff: noqa: D104 -from numpydantic.monkeypatch import apply_patches - -apply_patches() from numpydantic.ndarray import NDArray from numpydantic.meta import update_ndarray_stub diff --git a/src/numpydantic/monkeypatch.py b/src/numpydantic/monkeypatch.py deleted file mode 100644 index 05607e5..0000000 --- a/src/numpydantic/monkeypatch.py +++ /dev/null @@ -1,66 +0,0 @@ -""" -Functions to monkeypatch dependent packages - most notably nptyping -""" - -# ruff: noqa: ANN001 - - -def patch_npytyping_perf() -> None: - """ - npytyping makes an expensive call to inspect.stack() - that makes imports of pydantic models take ~200x longer than - they should: - - References: - - https://github.com/ramonhagenaars/nptyping/issues/110 - """ - import inspect - from types import FrameType - - from nptyping import base_meta_classes, ndarray, recarray - from nptyping.pandas_ import dataframe - - # make a new __module__ methods for the affected classes - - def new_module_ndarray(cls) -> str: # pragma: no cover - return cls._get_module(inspect.currentframe(), "nptyping.ndarray") - - def new_module_recarray(cls) -> str: # pragma: no cover - return cls._get_module(inspect.currentframe(), "nptyping.recarray") - - def new_module_dataframe(cls) -> str: # pragma: no cover - return cls._get_module(inspect.currentframe(), "nptyping.pandas_.dataframe") - - # and a new _get_module method for the parent class - def new_get_module(cls, stack: FrameType, module: str) -> str: # pragma: no cover - return ( - "typing" - if inspect.getframeinfo(stack.f_back).function == "formatannotation" - else module - ) - - # now apply the patches - ndarray.NDArrayMeta.__module__ = property(new_module_ndarray) - recarray.RecArrayMeta.__module__ = property(new_module_recarray) - dataframe.DataFrameMeta.__module__ = property(new_module_dataframe) - base_meta_classes.SubscriptableMeta._get_module = new_get_module - - -def patch_nptyping_warnings() -> None: - """ - nptyping shits out a bunch of numpy deprecation warnings from using - olde aliases - - References: - - https://github.com/ramonhagenaars/nptyping/issues/113 - - https://github.com/ramonhagenaars/nptyping/issues/102 - """ - import warnings - - warnings.filterwarnings("ignore", category=DeprecationWarning, module="nptyping.*") - - -def apply_patches() -> None: - """Apply all monkeypatches!""" - patch_npytyping_perf() - patch_nptyping_warnings()