Skip to content

Commit

Permalink
De-stringify ParameterAttribute.unit (#1890)
Browse files Browse the repository at this point in the history
* Reproduce #1888

* De-stringify ParameterAttribute.unit

* Rename plugin class

* Update release history
  • Loading branch information
mattwthompson authored Jun 14, 2024
1 parent f74700a commit fd5a20b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
6 changes: 6 additions & 0 deletions docs/releasehistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ Releases follow the `major.minor.micro` scheme recommended by [PEP440](https://w
* #1861 Forbid looking up virtual site parameters by SMIRKS, since a valid SMIRNOFF force field
can have several virtual site parameters with identical SMIRKS.

## Current development

### Bugfixes

- [PR #1890](https://github.com/openforcefield/openff-toolkit/pull/1890): Fixes [#1888](https://github.com/openforcefield/openff-toolkit/pull/1888) in which `ParameterAttribute.unit` was sometimes not validated when stringified.

## 0.16.1


Expand Down
29 changes: 26 additions & 3 deletions openff/toolkit/_tests/test_parameter_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from openff.toolkit.typing.engines.smirnoff import ForceField
from openff.toolkit import ForceField, Quantity
from openff.toolkit.typing.engines.smirnoff.plugins import (
_load_handler_plugins,
load_handler_plugins,
Expand Down Expand Up @@ -50,8 +50,12 @@ def test_load_handler_plugins():

registered_plugins = load_handler_plugins()

assert len(registered_plugins) == 1
assert registered_plugins[0].__name__ == "CustomHandler"
assert len(registered_plugins) == 2

registered_plugin_names = [plugin.__name__ for plugin in registered_plugins]

assert "CustomHandler" in registered_plugin_names
assert "FOOBuckinghamHandler" in registered_plugin_names


def test_do_not_load_other_type():
Expand All @@ -67,3 +71,22 @@ def test_skip_wrong_subclass(caplog):
load_handler_plugins()

assert "does not inherit from ParameterHandler" in caplog.text, caplog.text


def test_buckingham_type():
"""Reproduce, in part, issue #1888."""
from custom_plugins.handler_plugins import FOOBuckinghamHandler

parameter = FOOBuckinghamHandler.FOOBuckinghamType(
smirks="[*:1]",
a="2 kilojoule_per_mole",
b="1/nanometer",
c="-0.5 kilojoule_per_mole * nanometer**6",
)

for param in ["a", "b", "c"]:
assert isinstance(getattr(parameter, param), Quantity)

assert str(parameter.a.units) == "kilojoule_per_mole"
assert str(parameter.b.units) == "1 / nanometer"
assert str(parameter.c.units) == "kilojoule_per_mole * nanometer ** 6"
6 changes: 5 additions & 1 deletion openff/toolkit/typing/engines/smirnoff/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,14 @@ class ParameterAttribute:
def __init__(
self,
default: Any = UNDEFINED,
unit: Optional[Unit] = None,
unit: Unit | str | None = None,
converter: Optional[Callable] = None,
docstring: str = "",
):
if isinstance(unit, str):
# be careful with module & variable names
unit = Unit(unit)

self.default = default
self._unit = unit
self._converter = converter
Expand Down
29 changes: 28 additions & 1 deletion utilities/test_plugins/custom_plugins/handler_plugins.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from openff.toolkit.typing.engines.smirnoff import ParameterHandler, ParameterIOHandler

from openff.toolkit.typing.engines.smirnoff.parameters import ParameterAttribute, ParameterType, _NonbondedHandler
from openff.toolkit import unit

class CustomHandler(ParameterHandler):
_TAGNAME = "CustomHandler"
Expand All @@ -11,3 +12,29 @@ class WrongSubclass(list):

class CustomIOHandler(ParameterIOHandler):
_FORMAT = "JSON"

class FOOBuckinghamHandler(_NonbondedHandler):
"""A custom parameter handler for buckingham interactions."""

class FOOBuckinghamType(ParameterType):
"""A custom parameter type for buckingham interactions."""

_ELEMENT_NAME = "Atom"

# Define unit as a Unit object
a = ParameterAttribute(default=None,
unit=unit.kilojoule_per_mole,
)

# Define using a string
b = ParameterAttribute(default=None,
unit="nanometer**-1",
)

c = ParameterAttribute(
default=None,
unit=unit.kilojoule_per_mole * unit.nanometer ** 6,
)

_TAGNAME = "FOOBuckingham"
_INFOTYPE = FOOBuckinghamType
1 change: 1 addition & 0 deletions utilities/test_plugins/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"openff.toolkit.plugins.handlers": [
"CustomHandler = custom_plugins.handler_plugins:CustomHandler",
"WrongSubclass = custom_plugins.handler_plugins:WrongSubclass",
"FOOBuckinghamHandler = custom_plugins.handler_plugins:FOOBuckinghamHandler",
]
},
)

0 comments on commit fd5a20b

Please sign in to comment.