decrease deprecation warnings

This commit is contained in:
sneakers-the-rat 2023-10-04 22:49:23 -07:00
parent 15d1f0d7b1
commit 24e0b704ff
2 changed files with 31 additions and 10 deletions

View file

@ -56,7 +56,7 @@ class LinkML_Meta(BaseModel):
tree_root: bool = False tree_root: bool = False
def default_template(pydantic_ver: str = "1", extra_classes:Optional[List[Type[BaseModel]]] = None) -> str: def default_template(pydantic_ver: str = "2", extra_classes:Optional[List[Type[BaseModel]]] = None) -> str:
"""Constructs a default template for pydantic classes based on the version of pydantic""" """Constructs a default template for pydantic classes based on the version of pydantic"""
### HEADER ### ### HEADER ###
template = """ template = """
@ -68,7 +68,12 @@ from __future__ import annotations
from datetime import datetime, date from datetime import datetime, date
from enum import Enum from enum import Enum
from typing import List, Dict, Optional, Any, Union, ClassVar from typing import List, Dict, Optional, Any, Union, ClassVar
from pydantic import BaseModel as BaseModel, Field from pydantic import BaseModel as BaseModel, Field"""
if pydantic_ver == '2':
template += """
from pydantic import ConfigDict
"""
template +="""
from nptyping import Shape, Float, Float32, Double, Float64, LongLong, Int64, Int, Int32, Int16, Short, Int8, UInt, UInt32, UInt16, UInt8, UInt64, Number, String, Unicode, Unicode, Unicode, String, Bool, Datetime64 from nptyping import Shape, Float, Float32, Double, Float64, LongLong, Int64, Int, Int32, Int16, Short, Int8, UInt, UInt32, UInt16, UInt8, UInt64, Number, String, Unicode, Unicode, Unicode, String, Bool, Datetime64
from nwb_linkml.types import NDArray from nwb_linkml.types import NDArray
import sys import sys
@ -102,12 +107,14 @@ class ConfiguredBaseModel(WeakRefShimBaseModel,
""" """
else: else:
template += """ template += """
class ConfiguredBaseModel(BaseModel, class ConfiguredBaseModel(BaseModel):
model_config = ConfigDict(
validate_assignment = True, validate_assignment = True,
validate_default = True, validate_default = True,
extra = {% if allow_extra %}'allow'{% else %}'forbid'{% endif %}, extra = {% if allow_extra %}'allow'{% else %}'forbid'{% endif %},
arbitrary_types_allowed = True, arbitrary_types_allowed = True,
use_enum_values = True): use_enum_values = True
)
""" """
### Injected Fields ### Injected Fields
template += """ template += """

View file

@ -2,7 +2,7 @@
Monkeypatches to external modules Monkeypatches to external modules
""" """
def patch_npytyping(): def patch_npytyping_perf():
""" """
npytyping makes an expensive call to inspect.stack() npytyping makes an expensive call to inspect.stack()
that makes imports of pydantic models take ~200x longer than that makes imports of pydantic models take ~200x longer than
@ -38,6 +38,19 @@ def patch_npytyping():
dataframe.DataFrameMeta.__module__ = property(new_module_dataframe) dataframe.DataFrameMeta.__module__ = property(new_module_dataframe)
base_meta_classes.SubscriptableMeta._get_module = new_get_module base_meta_classes.SubscriptableMeta._get_module = new_get_module
def patch_nptyping_warnings():
"""
nptyping shits out a bunch of numpy deprecation warnings from using
olde aliases
"""
import warnings
warnings.filterwarnings(
'ignore',
category=DeprecationWarning,
module='nptyping.*'
)
def patch_schemaview(): def patch_schemaview():
""" """
Patch schemaview to correctly resolve multiple layers of relative imports. Patch schemaview to correctly resolve multiple layers of relative imports.
@ -96,5 +109,6 @@ def patch_schemaview():
SchemaView.imports_closure = imports_closure SchemaView.imports_closure = imports_closure
def apply_patches(): def apply_patches():
patch_npytyping() patch_npytyping_perf()
patch_nptyping_warnings()
patch_schemaview() patch_schemaview()