diff --git a/gmso/abc/__init__.py b/gmso/abc/__init__.py index dece032fd..5049d4ce7 100644 --- a/gmso/abc/__init__.py +++ b/gmso/abc/__init__.py @@ -1 +1 @@ -from gmso.abc.serialization_utils import GMSOJSONHandler, unyt_to_dict +from gmso.abc.serialization_utils import unyt_to_dict diff --git a/gmso/abc/abstract_potential.py b/gmso/abc/abstract_potential.py index e5234ce4a..d56e82939 100644 --- a/gmso/abc/abstract_potential.py +++ b/gmso/abc/abstract_potential.py @@ -28,10 +28,10 @@ class AbstractPotential(GMSOBase): PotentialExpression(expression="a*x+b", independent_variables={"x"}), description="The mathematical expression for the potential", ) - # expected_parameters_dimensions: Dict[str, Any] = Field( - # dict(), - # description="The expected unit dimensions of all the parameters", - # ) + expected_parameters_dimensions: Dict[str, Any] = Field( + dict(), + description="The expected unit dimensions of all the parameters", + ) tags: Dict[str, Any] = Field( {}, description="Tags associated with the potential" ) diff --git a/gmso/abc/abstract_site.py b/gmso/abc/abstract_site.py index b7ee32c22..a7d0881cf 100644 --- a/gmso/abc/abstract_site.py +++ b/gmso/abc/abstract_site.py @@ -33,10 +33,12 @@ def default_position(): class Site(GMSOBase): __iterable_attributes__: ClassVar[set] = { - "label", + "name" "label", "group", "molecule", "residue", + "position", + "model_config", } __base_doc__: ClassVar[ @@ -57,9 +59,9 @@ class Site(GMSOBase): name: str = Field( "", + validate_default=True, description="Name of the site, defaults to class name", ) - label: str = Field("", description="Label to be assigned to the site") group: Optional[StrictStr] = Field( @@ -86,36 +88,6 @@ class Site(GMSOBase): validate_assignment=True, ) - @property - def name(self) -> str: - """Return the name of the site.""" - return self.__dict__.get("name_") - - @property - def position(self) -> u.unyt_array: - """Return the 3D Cartesian coordinates of the site.""" - return self.__dict__.get("position_") - - @property - def label(self) -> str: - """Return the label assigned to the site.""" - return self.__dict__.get("label_") - - @property - def group(self) -> str: - """Return the group of the site.""" - return self.__dict__.get("group_") - - @property - def molecule(self) -> tuple: - """Return the molecule of the site.""" - return self.__dict__.get("molecule_") - - @property - def residue(self): - """Return the residue assigned to the site.""" - return self.__dict__.get("residue_") - def __repr__(self): """Return the formatted representation of the site.""" return ( diff --git a/gmso/abc/auto_doc.py b/gmso/abc/auto_doc.py index 3a9634ed6..325f08cf5 100644 --- a/gmso/abc/auto_doc.py +++ b/gmso/abc/auto_doc.py @@ -4,10 +4,7 @@ from copy import deepcopy from typing import Any, Dict, List, Optional, Tuple, Type, Union -try: - from pydantic.v1 import BaseModel -except ImportError: - from pydantic import BaseModel +from pydantic import BaseModel BASE_DOC_ATTR = "__base_doc__" FIELDS_IN_DOCSTRING = "__alias_to_fields__" diff --git a/gmso/abc/gmso_base.py b/gmso/abc/gmso_base.py index 73d40d8b0..a7ee160d6 100644 --- a/gmso/abc/gmso_base.py +++ b/gmso/abc/gmso_base.py @@ -6,7 +6,6 @@ from pydantic import BaseModel, ConfigDict, validators -from gmso.abc import GMSOJSONHandler from gmso.abc.auto_doc import apply_docs from gmso.abc.serialization_utils import dict_to_unyt @@ -26,9 +25,7 @@ class GMSOBase(BaseModel, ABC): # Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-config for more information. model_config = ConfigDict( arbitrary_types_allowed=True, - alias_to_fields=dict(), extra="forbid", - json_encoders=GMSOJSONHandler.json_encoders, populate_by_name=True, ) @@ -90,18 +87,6 @@ def _iter(self, **kwargs) -> "TupleGenerator": yield from super()._iter(**kwargs) - def json(self, **kwargs): - kwargs["by_alias"] = True - # FIXME: Pydantic>1.8 doesn't recognize json_encoders without this update - self.__config__.json_encoders.update(GMSOJSONHandler.json_encoders) - - return super(GMSOBase, self).json(**kwargs) - - def json_dict(self, **kwargs): - """Return a JSON serializable dictionary from the object""" - raw_json = self.json(**kwargs) - return json.loads(raw_json) - @classmethod def validate(cls, value): """Ensure that the object is validated before use.""" diff --git a/gmso/abc/serialization_utils.py b/gmso/abc/serialization_utils.py index 683343192..89f31de1e 100644 --- a/gmso/abc/serialization_utils.py +++ b/gmso/abc/serialization_utils.py @@ -33,21 +33,3 @@ def dict_to_unyt(dict_obj) -> None: unyt_func = u.unyt_array dict_obj[key] = unyt_func(np_array, value["unit"]) - - -class JSONHandler: - def __init__(self): - self.json_encoders = {} - - def register(self, type_, callable_, override=False): - """Register a new JSON encoder for an object for serialization in GMSO""" - if type_ not in self.json_encoders: - self.json_encoders[type_] = callable_ - else: - if override: - warn(f"Overriding json serializer for {type_}") - self.json_encoders[type_] = callable_ - - -GMSOJSONHandler = JSONHandler() -GMSOJSONHandler.register(u.unyt_array, unyt_to_dict) diff --git a/gmso/utils/decorators.py b/gmso/utils/decorators.py index 7c0314386..6df98bee0 100644 --- a/gmso/utils/decorators.py +++ b/gmso/utils/decorators.py @@ -2,21 +2,6 @@ import functools import warnings -from gmso.abc import GMSOJSONHandler - - -class register_pydantic_json(object): - """Provides a way to register json encoders for a non-JSON serializable class.""" - - def __init__(self, method="json"): - self.method = method - - def __call__(self, cls): - """Register this class's json encoder method to GMSOJSONHandler.""" - json_method = getattr(cls, self.method) - GMSOJSONHandler.register(cls, json_method) - return cls - def deprecate_kwargs(deprecated_kwargs=None): """Decorate functions with deprecated/deprecating kwargs.""" diff --git a/gmso/utils/expression.py b/gmso/utils/expression.py index 590040c70..73b492346 100644 --- a/gmso/utils/expression.py +++ b/gmso/utils/expression.py @@ -7,8 +7,6 @@ import sympy import unyt as u -from gmso.utils.decorators import register_pydantic_json - __all__ = ["PotentialExpression"] @@ -30,7 +28,6 @@ def _are_equal_parameters(u1, u2): return True -@register_pydantic_json(method="json") class PotentialExpression: """A general Expression class with parameters.