Skip to content

Commit

Permalink
Merge pull request #67 from openforcefield/pint-versions
Browse files Browse the repository at this point in the history
Test on multiple Pint versions
  • Loading branch information
mattwthompson authored Jun 14, 2023
2 parents b5af837 + 8f655d6 commit f2f27a7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
os: [macOS-latest, ubuntu-latest]
openmm: ["true", "false"]
python-version: ["3.9", "3.10", "3.11"]
pint-version: ["0.21", "0.22"]

env:
CI_OS: ${{ matrix.os }}
Expand All @@ -38,6 +39,7 @@ jobs:
environment-file: devtools/conda-envs/test_env.yaml
create-args: >-
python=${{ matrix.python-version }}
pint=${{ matrix.pint-version }}
- name: Optionally install OpenMM
if: ${{ matrix.openmm == 'true' }}
Expand Down
2 changes: 1 addition & 1 deletion devtools/conda-envs/test_env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dependencies:
- python
- pip
- numpy
- pint =0.21 # https://github.com/conda-forge/pint-feedstock/pull/57
- pint
- openff-utilities >=0.1.3

# Tests
Expand Down
3 changes: 2 additions & 1 deletion openff/units/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
]

MASSES: Dict[int, Quantity] = {
index + 1: Quantity(mass, unit.dalton)
# https://github.com/hgrecco/pint/issues/1804
index + 1: Quantity(mass, unit.dalton) # type: ignore[call-overload]
for index, mass in enumerate(
[
1.007947,
Expand Down
3 changes: 2 additions & 1 deletion openff/units/openmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ def _ensure_openff_quantity(
)
else:
try:
return unit.Quantity(
# https://github.com/hgrecco/pint/issues/1804
return unit.Quantity( # type: ignore[call-overload]
unknown_quantity,
unit.dimensionless,
)
Expand Down
34 changes: 18 additions & 16 deletions openff/units/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from pint import Measurement as _Measurement
from pint import Quantity as _Quantity
from pint import Unit as _Unit
from pint import UnitRegistry as _UnitRegistry

from openff.units.utilities import get_defaults_path

Expand All @@ -25,13 +24,13 @@
]


class Unit(_Unit):
class Unit(pint.UnitRegistry.Unit):
"""A unit of measure."""

pass


class Quantity(_Quantity):
class Quantity(pint.UnitRegistry.Quantity):
"""A value with associated units."""

def __dask_tokenize__(self):
Expand All @@ -42,21 +41,22 @@ def _dask_finalize(results, func, args, units):
values = func(results, *args)
return Quantity(values, units)

@requires_package("openmm")
def to_openmm(self) -> "OpenMMQuantity":
"""Convert the quantity to an ``openmm.unit.Quantity``.

Returns
-------
openmm_quantity : openmm.unit.quantity.Quantity
The OpenMM compatible quantity.
"""
from openff.units.openmm import to_openmm
@requires_package("openmm")
def _to_openmm(self) -> "OpenMMQuantity":
"""Convert the quantity to an ``openmm.unit.Quantity``.
return to_openmm(self)
Returns
-------
openmm_quantity : openmm.unit.quantity.Quantity
The OpenMM compatible quantity.
"""
from openff.units.openmm import to_openmm

return to_openmm(self)

class Measurement(_Measurement):

class Measurement(pint.UnitRegistry.Measurement): # type: ignore
"""A value with associated units and uncertainty."""

def __dask_tokenize__(self):
Expand All @@ -68,7 +68,7 @@ def _dask_finalize(results, func, args, units):
return Measurement(values, units)


class UnitRegistry(_UnitRegistry):
class UnitRegistry(pint.UnitRegistry):
_quantity_class = Quantity
_unit_class = Unit
_measurement_class = Measurement
Expand All @@ -78,10 +78,12 @@ class UnitRegistry(_UnitRegistry):

Unit: _Unit = DEFAULT_UNIT_REGISTRY.Unit # type: ignore[no-redef]
Quantity: _Quantity = DEFAULT_UNIT_REGISTRY.Quantity # type: ignore[no-redef]
Measurement: _Measurement = DEFAULT_UNIT_REGISTRY.Measurement # type: ignore[no-redef]
Measurement: _Measurement = DEFAULT_UNIT_REGISTRY.Measurement # type: ignore

pint.set_application_registry(DEFAULT_UNIT_REGISTRY)

Quantity.to_openmm = _to_openmm # type: ignore[attr-defined]

with warnings.catch_warnings():
warnings.simplefilter("ignore")
Quantity([])

0 comments on commit f2f27a7

Please sign in to comment.