From 24e0b704ffdfeaaa6aeb95c2ad97cf0319145ad2 Mon Sep 17 00:00:00 2001 From: sneakers-the-rat Date: Wed, 4 Oct 2023 22:49:23 -0700 Subject: [PATCH] decrease deprecation warnings --- .../src/nwb_linkml/generators/pydantic.py | 23 ++++++++++++------- nwb_linkml/src/nwb_linkml/monkeypatch.py | 18 +++++++++++++-- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/nwb_linkml/src/nwb_linkml/generators/pydantic.py b/nwb_linkml/src/nwb_linkml/generators/pydantic.py index f85fd04..4513178 100644 --- a/nwb_linkml/src/nwb_linkml/generators/pydantic.py +++ b/nwb_linkml/src/nwb_linkml/generators/pydantic.py @@ -56,7 +56,7 @@ class LinkML_Meta(BaseModel): 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""" ### HEADER ### template = """ @@ -68,7 +68,12 @@ from __future__ import annotations from datetime import datetime, date from enum import Enum 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 nwb_linkml.types import NDArray import sys @@ -102,12 +107,14 @@ class ConfiguredBaseModel(WeakRefShimBaseModel, """ else: template += """ -class ConfiguredBaseModel(BaseModel, - validate_assignment = True, - validate_default = True, - extra = {% if allow_extra %}'allow'{% else %}'forbid'{% endif %}, - arbitrary_types_allowed = True, - use_enum_values = True): +class ConfiguredBaseModel(BaseModel): + model_config = ConfigDict( + validate_assignment = True, + validate_default = True, + extra = {% if allow_extra %}'allow'{% else %}'forbid'{% endif %}, + arbitrary_types_allowed = True, + use_enum_values = True + ) """ ### Injected Fields template += """ diff --git a/nwb_linkml/src/nwb_linkml/monkeypatch.py b/nwb_linkml/src/nwb_linkml/monkeypatch.py index 443f1e6..c37d18b 100644 --- a/nwb_linkml/src/nwb_linkml/monkeypatch.py +++ b/nwb_linkml/src/nwb_linkml/monkeypatch.py @@ -2,7 +2,7 @@ Monkeypatches to external modules """ -def patch_npytyping(): +def patch_npytyping_perf(): """ npytyping makes an expensive call to inspect.stack() that makes imports of pydantic models take ~200x longer than @@ -38,6 +38,19 @@ def patch_npytyping(): dataframe.DataFrameMeta.__module__ = property(new_module_dataframe) 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(): """ Patch schemaview to correctly resolve multiple layers of relative imports. @@ -96,5 +109,6 @@ def patch_schemaview(): SchemaView.imports_closure = imports_closure def apply_patches(): - patch_npytyping() + patch_npytyping_perf() + patch_nptyping_warnings() patch_schemaview() \ No newline at end of file