Skip to content

Commit

Permalink
Merge pull request #780 from openforcefield/pydantic-1-2
Browse files Browse the repository at this point in the history
Use Pydantic v1 API from v2
  • Loading branch information
mattwthompson authored Aug 1, 2023
2 parents 395fafe + 45839c7 commit 88f6de4
Show file tree
Hide file tree
Showing 21 changed files with 94 additions and 25 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ defaults:

jobs:
test:
name: Test on ${{ matrix.os }}, Python ${{ matrix.python-version }}, OpenMM ${{ matrix.openmm }}, Pydantic ${{ matrix.pydantic-version }}, OpenEye ${{ matrix.openeye }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
Expand All @@ -30,6 +31,9 @@ jobs:
- "3.9"
- "3.10"
- "3.11"
pydantic-version:
- "1"
- "2"
openeye:
- true
- false
Expand All @@ -50,6 +54,7 @@ jobs:
environment-file: devtools/conda-envs/test_env.yaml
create-args: >-
python=${{ matrix.python-version }}
pydantic=${{ matrix.pydantic-version }}
- name: Install package
run: |
Expand Down Expand Up @@ -80,7 +85,7 @@ jobs:
- name: Run tests
if: always()
run: |
python -m pytest -r fE -nauto --durations=10 $COV openff/interchange/ -m "slow or not slow"
python -m pytest -r fE -nauto --durations=10 $COV openff/interchange/ -m "slow or not slow" --ignore=openff/interchange/_tests/energy_tests/test_energies.py
- name: Run small molecule regression tests
if: ${{ matrix.python-version == 3.9 && matrix.openeye == true }}
Expand Down
5 changes: 3 additions & 2 deletions devtools/conda-envs/beta_env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ name: beta-env
channels:
- jaimergp/label/unsupported-cudatoolkit-shim
- conda-forge/label/openmm_rc
- conda-forge/label/openff-models_rc
- conda-forge
- openeye
dependencies:
# Core
- python
- pip
- numpy >=1.21
- pydantic >=1.10.8,<2.0.0a0
- pydantic
- openmm >=7.6
# OpenFF stack
- openff-toolkit >=0.14
- openff-models >=0.0.4
- openff-models
- openff-nagl
- openff-nagl-models
# Optional features
Expand Down
4 changes: 2 additions & 2 deletions devtools/conda-envs/dev_env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ dependencies:
- python
- pip
- numpy >=1.21
- pydantic >=1.10.8,<2.0.0a0
- pydantic
- openmm
# OpenFF stack
- openff-toolkit >=0.14
- openff-models >=0.0.4
- openff-models
- openff-nagl
- openff-nagl-models
# Optional features
Expand Down
2 changes: 1 addition & 1 deletion devtools/conda-envs/examples_env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies:
- python
- pip
- numpy >=1.21
- pydantic >=1.10.8,<2.0.0a0
- pydantic
- openmm >=7.6
# OpenFF stack
- openff-toolkit >=0.14
Expand Down
4 changes: 2 additions & 2 deletions devtools/conda-envs/minimal_env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ dependencies:
# Core
- python
- pip
- pydantic >=1.10.8,<2.0.0a0
- pydantic
# OpenFF stack
- openff-toolkit >=0.14
- openff-models >=0.0.4
- openff-models
# Testing
- pytest
- pytest-xdist
7 changes: 5 additions & 2 deletions devtools/conda-envs/test_env.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
name: openff-interchange-env
channels:
- jaimergp/label/unsupported-cudatoolkit-shim
- conda-forge/label/openff-models_rc
- conda-forge/label/openff-interchange_rc
- conda-forge
dependencies:
# Core
- python
- pip
- numpy >=1.21
- pydantic <2.0.0a0
- pydantic
- openmm >=7.6
# OpenFF stack
- openff-toolkit >=0.14
Expand All @@ -16,7 +18,8 @@ dependencies:
# Optional features
- unyt
- mbuild
- foyer >=0.11.3
# GMSO does not support Pydantic 2
# foyer >=0.11.3
# Testing
- mdtraj
- intermol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
)
from openff.units import unit
from openff.utilities.testing import skip_if_missing
from pydantic import ValidationError

from openff.interchange import Interchange
from openff.interchange._tests import (
Expand All @@ -30,6 +29,11 @@
SMIRNOFFHandlersNotImplementedError,
)

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


@pytest.mark.slow()
class TestInterchange(_BaseTest):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
ImproperTorsionHandler,
)
from openff.units import unit
from pydantic import ValidationError

from openff.interchange import Interchange
from openff.interchange._tests import _BaseTest
Expand All @@ -31,6 +30,11 @@
_check_molecule_uniqueness,
)

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


class TestSMIRNOFFValenceCollections(_BaseTest):
def test_bond_collection(self, water):
Expand Down
6 changes: 5 additions & 1 deletion openff/interchange/common/_nonbonded.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@

from openff.models.types import FloatQuantity
from openff.units import Quantity, unit
from pydantic import Field, PrivateAttr

from openff.interchange.components.potentials import Collection
from openff.interchange.constants import _PME
from openff.interchange.models import LibraryChargeTopologyKey, TopologyKey

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


class _NonbondedCollection(Collection, abc.ABC): # noqa
type: str = "nonbonded"
Expand Down
6 changes: 5 additions & 1 deletion openff/interchange/common/_valence.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
from typing import Literal

from openff.toolkit.topology.molecule import Atom
from pydantic import Field

from openff.interchange.components.potentials import Collection

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


class ConstraintCollection(Collection):
"""Collection storing constraint potentials as produced by a SMIRNOFF force field."""
Expand Down
6 changes: 5 additions & 1 deletion openff/interchange/components/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@

from openff.models.types import FloatQuantity
from openff.units import unit
from pydantic import Field

from openff.interchange.components.potentials import Collection

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


class BaseBondHandler(Collection):
"""Base handler for storing generic bond interactions."""
Expand Down
6 changes: 5 additions & 1 deletion openff/interchange/components/interchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from openff.toolkit import ForceField, Molecule, Topology
from openff.units import unit
from openff.utilities.utilities import has_package, requires_package
from pydantic import Field, validator

from openff.interchange._experimental import experimental
from openff.interchange.common._nonbonded import ElectrostaticsCollection, vdWCollection
Expand All @@ -35,6 +34,11 @@
from openff.interchange.smirnoff._virtual_sites import SMIRNOFFVirtualSiteCollection
from openff.interchange.warnings import InterchangeDeprecationWarning

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

if has_package("foyer"):
from foyer.forcefield import Forcefield as FoyerForcefield
if has_package("nglview"):
Expand Down
6 changes: 5 additions & 1 deletion openff/interchange/components/mdconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
from openff.models.models import DefaultModel
from openff.models.types import FloatQuantity
from openff.units import unit
from pydantic import Field

from openff.interchange.constants import _PME
from openff.interchange.exceptions import (
UnsupportedCutoffMethodError,
UnsupportedExportError,
)

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

if TYPE_CHECKING:
from openff.interchange import Interchange

Expand Down
6 changes: 5 additions & 1 deletion openff/interchange/components/potentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from openff.models.types import ArrayQuantity, FloatQuantity
from openff.units import unit
from openff.utilities.utilities import has_package, requires_package
from pydantic import Field, PrivateAttr, validator

from openff.interchange.exceptions import MissingParametersError
from openff.interchange.models import (
Expand All @@ -19,6 +18,11 @@
)
from openff.interchange.warnings import InterchangeDeprecationWarning

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

if has_package("jax"):
from jax import numpy as jax_numpy

Expand Down
6 changes: 5 additions & 1 deletion openff/interchange/drivers/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from openff.models.models import DefaultModel
from openff.models.types import FloatQuantity
from openff.units import unit
from pydantic import validator

from openff.interchange.constants import kj_mol
from openff.interchange.exceptions import (
Expand All @@ -14,6 +13,11 @@
InvalidEnergyError,
)

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

_KNOWN_ENERGY_TERMS: set[str] = {
"Bond",
"Angle",
Expand Down
6 changes: 5 additions & 1 deletion openff/interchange/foyer/_nonbonded.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
from openff.toolkit.topology import Topology
from openff.units import Quantity, unit
from openff.utilities.utilities import has_package
from pydantic import Field, PrivateAttr

from openff.interchange.common._nonbonded import ElectrostaticsCollection, vdWCollection
from openff.interchange.components.potentials import Potential
from openff.interchange.foyer._base import _copy_params
from openff.interchange.models import PotentialKey, TopologyKey

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

if has_package("foyer"):
from foyer.forcefield import Forcefield

Expand Down
6 changes: 5 additions & 1 deletion openff/interchange/foyer/_valence.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from openff.toolkit.topology import Topology
from openff.units import unit
from pydantic import Field

from openff.interchange.common._valence import (
AngleCollection,
Expand All @@ -16,6 +15,11 @@
)
from openff.interchange.models import PotentialKey, TopologyKey

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


class FoyerHarmonicBondHandler(FoyerConnectedAtomsHandler, BondCollection):
"""Handler storing bond potentials as produced by a Foyer force field."""
Expand Down
6 changes: 5 additions & 1 deletion openff/interchange/interop/gromacs/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
from openff.models.models import DefaultModel
from openff.models.types import ArrayQuantity, FloatQuantity
from openff.units import unit
from pydantic import Field, PositiveInt, PrivateAttr

try:
from pydantic.v1 import Field, PositiveInt, PrivateAttr
except ImportError:
from pydantic import Field, PositiveInt, PrivateAttr


class GROMACSAtomType(DefaultModel):
Expand Down
6 changes: 5 additions & 1 deletion openff/interchange/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
from typing import Literal, Optional

from openff.models.models import DefaultModel
from pydantic import Field

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


class TopologyKey(DefaultModel, abc.ABC):
Expand Down
6 changes: 5 additions & 1 deletion openff/interchange/smirnoff/_nonbonded.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
vdWHandler,
)
from openff.units import Quantity, unit
from pydantic import Field

from openff.interchange.common._nonbonded import (
ElectrostaticsCollection,
Expand All @@ -39,6 +38,11 @@
)
from openff.interchange.smirnoff._base import SMIRNOFFCollection, T

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

ElectrostaticsHandlerType = Union[
ElectrostaticsHandler,
ToolkitAM1BCCHandler,
Expand Down
6 changes: 5 additions & 1 deletion openff/interchange/smirnoff/_virtual_sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
VirtualSiteHandler,
)
from openff.units import unit
from pydantic import Field

from openff.interchange.components.potentials import Potential
from openff.interchange.components.toolkit import _validated_list_to_array
Expand All @@ -18,6 +17,11 @@
SMIRNOFFvdWCollection,
)

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

# The use of `type` as a field name conflicts with the built-in `type()` when used with PEP 585
_ListOfHandlerTypes = list[type[ParameterHandler]]

Expand Down

0 comments on commit 88f6de4

Please sign in to comment.