Skip to content

Commit

Permalink
Support Pydantic v1 and v2 (#752)
Browse files Browse the repository at this point in the history
Co-authored-by: Co Quach <[email protected]>
  • Loading branch information
mattwthompson and daico007 authored Aug 11, 2023
1 parent 0b9e0c5 commit 525cfd3
Show file tree
Hide file tree
Showing 27 changed files with 127 additions and 35 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
matrix:
os: [macOS-latest, ubuntu-latest]
python-version: ["3.8", "3.9", "3.10", "3.11"]
pydantic-version: ["1", "2"]

defaults:
run:
Expand All @@ -33,7 +34,9 @@ jobs:
uses: mamba-org/setup-micromamba@v1
with:
environment-file: environment-dev.yml
python-version: python=${{ matrix.python-version }}
create-args: >-
python=${{ matrix.python-version }}
pydantic=${{ matrix.pydantic-version }}
- name: Install Package
run: python -m pip install -e .
Expand Down
2 changes: 1 addition & 1 deletion environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies:
- unyt<=2.9.2
- boltons
- lxml
- pydantic=1.10.11
- pydantic
- networkx
- pytest
- mbuild>=0.11.0
Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies:
- unyt<=2.9.2
- boltons
- lxml
- pydantic=1.10.11
- pydantic
- networkx
- ele>=0.2.0
- foyer>=0.11.3
Expand Down
7 changes: 5 additions & 2 deletions gmso/abc/abstract_connection.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from typing import Optional, Sequence

from pydantic import Field, root_validator

from gmso.abc.abstract_site import Site
from gmso.abc.gmso_base import GMSOBase
from gmso.exceptions import GMSOError

try:
from pydantic.v1 import Field, root_validator
except ImportError:
from pydantic import Field, root_validator


class Connection(GMSOBase):
__base_doc__ = """An abstract class that stores data about connections between sites.
Expand Down
7 changes: 5 additions & 2 deletions gmso/abc/abstract_potential.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
from abc import abstractmethod
from typing import Any, Dict, Iterator, List

from pydantic import Field, validator

from gmso.abc.gmso_base import GMSOBase
from gmso.utils.expression import PotentialExpression

try:
from pydantic.v1 import Field, validator
except ImportError:
from pydantic import Field, validator


class AbstractPotential(GMSOBase):
__base_doc__ = """An abstract potential class.
Expand Down
6 changes: 5 additions & 1 deletion gmso/abc/abstract_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

import numpy as np
import unyt as u
from pydantic import Field, StrictInt, StrictStr, validator
from unyt.exceptions import InvalidUnitOperation

from gmso.abc.gmso_base import GMSOBase
from gmso.exceptions import GMSOError

try:
from pydantic.v1 import Field, StrictInt, StrictStr, validator
except ImportError:
from pydantic import Field, StrictInt, StrictStr, validator

PositionType = Union[Sequence[float], np.ndarray, u.unyt_array]
MoleculeType = NamedTuple("Molecule", name=StrictStr, number=StrictInt)
ResidueType = NamedTuple("Residue", name=StrictStr, number=StrictInt)
Expand Down
5 changes: 4 additions & 1 deletion gmso/abc/auto_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
from copy import deepcopy
from typing import Any, Dict, List, Optional, Tuple, Type, Union

from pydantic import BaseModel
try:
from pydantic.v1 import BaseModel
except ImportError:
from pydantic import BaseModel

BASE_DOC_ATTR = "__base_doc__"
FIELDS_IN_DOCSTRING = "__alias_to_fields__"
Expand Down
10 changes: 7 additions & 3 deletions gmso/abc/gmso_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
from abc import ABC
from typing import Any, ClassVar, Type

from pydantic import BaseModel
from pydantic.validators import dict_validator

from gmso.abc import GMSOJSONHandler
from gmso.abc.auto_doc import apply_docs
from gmso.abc.serialization_utils import dict_to_unyt

try:
from pydantic.v1 import BaseModel
from pydantic.v1.validators import dict_validator
except ImportError:
from pydantic import BaseModel
from pydantic.validators import dict_validator


class GMSOBase(BaseModel, ABC):
"""A BaseClass to all abstract classes in GMSO."""
Expand Down
7 changes: 5 additions & 2 deletions gmso/core/angle.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""Support for 3-partner connections between gmso.core.Atoms."""
from typing import Callable, ClassVar, Optional, Tuple

from pydantic import Field

from gmso.abc.abstract_connection import Connection
from gmso.core.angle_type import AngleType
from gmso.core.atom import Atom

try:
from pydantic.v1 import Field
except ImportError:
from pydantic import Field


class Angle(Connection):
__base_doc__ = """A 3-partner connection between Atoms.
Expand Down
6 changes: 5 additions & 1 deletion gmso/core/angle_type.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from typing import Optional, Tuple

import unyt as u
from pydantic import Field

from gmso.core.parametric_potential import ParametricPotential
from gmso.utils.expression import PotentialExpression

try:
from pydantic.v1 import Field
except ImportError:
from pydantic import Field


class AngleType(ParametricPotential):
__base_doc__ = """A descripton of the interaction between 3 bonded partners.
Expand Down
6 changes: 5 additions & 1 deletion gmso/core/atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
from typing import Optional, Union

import unyt as u
from pydantic import Field, validator

from gmso.abc.abstract_site import Site
from gmso.core.atom_type import AtomType
from gmso.core.element import Element
from gmso.utils._constants import UNIT_WARNING_STRING
from gmso.utils.misc import ensure_valid_dimensions

try:
from pydantic.v1 import Field, validator
except ImportError:
from pydantic import Field, validator


class Atom(Site):
__base_doc__ = """An atom represents a single element association in a topology.
Expand Down
6 changes: 5 additions & 1 deletion gmso/core/atom_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Optional, Set

import unyt as u
from pydantic import Field, validator

from gmso.core.parametric_potential import ParametricPotential
from gmso.utils._constants import UNIT_WARNING_STRING
Expand All @@ -14,6 +13,11 @@
unyt_to_hashable,
)

try:
from pydantic.v1 import Field, validator
except ImportError:
from pydantic import Field, validator


class AtomType(ParametricPotential):
__base_doc__ = """A description of non-bonded interactions between sites.
Expand Down
7 changes: 5 additions & 2 deletions gmso/core/bond.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""Module for 2-partner connections between sites."""
from typing import Callable, ClassVar, Optional, Tuple

from pydantic import Field

from gmso.abc.abstract_connection import Connection
from gmso.core.atom import Atom
from gmso.core.bond_type import BondType

try:
from pydantic.v1 import Field
except ImportError:
from pydantic import Field


class Bond(Connection):
__base_doc__ = """A 2-partner connection between sites.
Expand Down
6 changes: 5 additions & 1 deletion gmso/core/bond_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
from typing import Optional, Tuple

import unyt as u
from pydantic import Field

from gmso.core.parametric_potential import ParametricPotential
from gmso.utils.expression import PotentialExpression

try:
from pydantic.v1 import Field
except ImportError:
from pydantic import Field


class BondType(ParametricPotential):
__base_doc__ = """A descripton of the interaction between 2 bonded partners.
Expand Down
7 changes: 5 additions & 2 deletions gmso/core/dihedral.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from typing import Callable, ClassVar, Optional, Tuple

from pydantic import Field

from gmso.abc.abstract_connection import Connection
from gmso.core.atom import Atom
from gmso.core.dihedral_type import DihedralType

try:
from pydantic.v1 import Field
except ImportError:
from pydantic import Field


class Dihedral(Connection):
__base_doc__ = """A 4-partner connection between sites.
Expand Down
6 changes: 5 additions & 1 deletion gmso/core/dihedral_type.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from typing import Optional, Tuple

import unyt as u
from pydantic import Field

from gmso.core.parametric_potential import ParametricPotential
from gmso.utils.expression import PotentialExpression

try:
from pydantic.v1 import Field
except ImportError:
from pydantic import Field


class DihedralType(ParametricPotential):
__base_doc__ = """A descripton of the interaction between 4 bonded partners.
Expand Down
6 changes: 5 additions & 1 deletion gmso/core/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
import numpy as np
import unyt as u
from pkg_resources import resource_filename
from pydantic import Field

from gmso.abc.gmso_base import GMSOBase
from gmso.exceptions import GMSOError
from gmso.utils.misc import unyt_to_hashable

try:
from pydantic.v1 import Field
except ImportError:
from pydantic import Field

exported = [
"element_by_mass",
"element_by_symbol",
Expand Down
7 changes: 5 additions & 2 deletions gmso/core/improper.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""Support for improper style connections (4-member connection)."""
from typing import Callable, ClassVar, Optional, Tuple

from pydantic import Field

from gmso.abc.abstract_connection import Connection
from gmso.core.atom import Atom
from gmso.core.improper_type import ImproperType

try:
from pydantic.v1 import Field
except ImportError:
from pydantic import Field


class Improper(Connection):
__base_doc__ = """sA 4-partner connection between sites.
Expand Down
6 changes: 5 additions & 1 deletion gmso/core/improper_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
from typing import Optional, Tuple

import unyt as u
from pydantic import Field

from gmso.core.parametric_potential import ParametricPotential
from gmso.utils.expression import PotentialExpression

try:
from pydantic.v1 import Field
except ImportError:
from pydantic import Field


class ImproperType(ParametricPotential):
__base_doc__ = """A description of the interaction between 4 bonded partners.
Expand Down
6 changes: 5 additions & 1 deletion gmso/core/pairpotential_type.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from typing import Optional, Tuple

import unyt as u
from pydantic import Field

from gmso.core.parametric_potential import ParametricPotential
from gmso.utils.expression import PotentialExpression

try:
from pydantic.v1 import Field
except ImportError:
from pydantic import Field


class PairPotentialType(ParametricPotential):
__base_doc__ = """A description of custom pairwise potential between 2 AtomTypes that does not follow combination rule.
Expand Down
6 changes: 5 additions & 1 deletion gmso/lib/potential_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import sympy
import unyt as u
from pydantic import Field, validator

from gmso.abc.abstract_potential import AbstractPotential
from gmso.exceptions import (
Expand All @@ -16,6 +15,11 @@
from gmso.utils.expression import PotentialExpression
from gmso.utils.singleton import Singleton

try:
from pydantic.v1 import Field, validator
except ImportError:
from pydantic import Field, validator

POTENTIAL_JSONS = list(Path(__file__).parent.glob("jsons/*.json"))
JSON_DIR = Path.joinpath(Path(__file__).parent, "jsons")

Expand Down
6 changes: 5 additions & 1 deletion gmso/parameterization/topology_parameterizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import networkx as nx
from boltons.setutils import IndexedSet
from pydantic import Field

from gmso.abc.gmso_base import GMSOBase
from gmso.core.forcefield import ForceField
Expand All @@ -29,6 +28,11 @@
)
from gmso.parameterization.utils import POTENTIAL_GROUPS

try:
from pydantic.v1 import Field
except ImportError:
from pydantic import Field


class ParameterizationError(GMSOError):
"""Raise when parameterization fails."""
Expand Down
6 changes: 5 additions & 1 deletion gmso/tests/test_angle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from pydantic import ValidationError

from gmso.core.angle import Angle
from gmso.core.angle_type import AngleType
Expand All @@ -8,6 +7,11 @@
from gmso.core.topology import Topology
from gmso.tests.base_test import BaseTest

try:
from pydantic.v1 import ValidationError
except ImportError:
from pydantic import ValidationError


class TestAngle(BaseTest):
def test_angle_nonparametrized(self):
Expand Down
Loading

0 comments on commit 525cfd3

Please sign in to comment.