Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Apr 2, 2024
1 parent 82622ab commit a23fd99
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pprint(atom.dict())
# {'charge': <Quantity(0.0, 'elementary_charge')>,
# 'mass': <Quantity(12.011, 'unified_atomic_mass_unit')>,
# 'some_array': <Quantity([ 4 -1 0], 'nanometer')>}
#
#

# Note that unit-bearing fields use custom serialization into a dict with separate key-val pairs for
# the unit (as a string) and unitless quantities (in whatever shape the data is)
Expand Down
25 changes: 12 additions & 13 deletions openff/models/dimension_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
from typing import Annotated, Any

from openff.units import Quantity
from pydantic import AfterValidator, BeforeValidator

from openff.models.types import json_loader

from pydantic import (
AfterValidator,
BaseModel,
BeforeValidator,
ValidationError,
ValidationInfo,
ValidatorFunctionWrapHandler,
)
from pydantic.functional_validators import WrapValidator

from openff.models.types import json_loader

try:
from openmm.unit import Quantity as OpenMMQuantity
except ImportError:
Expand All @@ -39,24 +39,24 @@ def has_compatible_dimensionality(quantity: Quantity, unit: str) -> Quantity:


def coerce_json_back_to_quantity(
v: Any,
handler: ValidatorFunctionWrapHandler,
info: ValidationInfo
v: Any, handler: ValidatorFunctionWrapHandler, info: ValidationInfo
) -> dict:
if info.mode == 'json':
if info.mode == "json":
if isinstance(v, str):

return Quantity(*json_loader(v).values())
else:
raise ValueError('In JSON mode the input must be a string!')
raise ValueError("In JSON mode the input must be a string!")

if info.mode == 'python':
if info.mode == "python":
if isinstance(v, dict):
return Quantity(*v.values())
elif isinstance(v, Quantity):
return v
else:
raise ValueError(f'In Python mode the input must be a dict! Found {type(v)}')
raise ValueError(
f"In Python mode the input must be a dict! Found {type(v)}"
)

# maybe what is currently to_quantity should simply be wrapped into here?

Expand All @@ -69,8 +69,7 @@ def build_dimension_type(unit: str) -> type[Quantity]:
Quantity,
BeforeValidator(to_quantity),
AfterValidator(partial(has_compatible_dimensionality, unit=unit)),
WrapValidator(coerce_json_back_to_quantity)

WrapValidator(coerce_json_back_to_quantity),
]


Expand Down

0 comments on commit a23fd99

Please sign in to comment.