"""Functions to monkeypatch dependent packages - most notably nptyping"""# ruff: noqa: ANN001
[docs]defpatch_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 """importinspectfromtypesimportFrameTypefromnptypingimportbase_meta_classes,ndarray,recarrayfromnptyping.pandas_importdataframe# make a new __module__ methods for the affected classesdefnew_module_ndarray(cls)->str:# pragma: no coverreturncls._get_module(inspect.currentframe(),"nptyping.ndarray")defnew_module_recarray(cls)->str:# pragma: no coverreturncls._get_module(inspect.currentframe(),"nptyping.recarray")defnew_module_dataframe(cls)->str:# pragma: no coverreturncls._get_module(inspect.currentframe(),"nptyping.pandas_.dataframe")# and a new _get_module method for the parent classdefnew_get_module(cls,stack:FrameType,module:str)->str:# pragma: no coverreturn("typing"ifinspect.getframeinfo(stack.f_back).function=="formatannotation"elsemodule)# now apply the patchesndarray.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
[docs]defpatch_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 """importwarningswarnings.filterwarnings("ignore",category=DeprecationWarning,module="nptyping.*")
[docs]defapply_patches()->None:"""Apply all monkeypatches!"""patch_npytyping_perf()patch_nptyping_warnings()