Skip to content

Commit

Permalink
Duck-type serialize by default
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwthompson committed Jun 6, 2024
1 parent 1b597f7 commit bbc2416
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 8 additions & 0 deletions openff/models/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any

from openff.units import Quantity

from openff.models._pydantic import BaseModel
Expand All @@ -14,3 +16,9 @@ class DefaultModel(BaseModel):
"validate_assignment": True,
"arbitrary_types_allowed": True,
}

def model_dump(self, **kwargs) -> dict[str, Any]:
return super().model_dump(serialize_as_any=True, **kwargs)

def model_dump_json(self, **kwargs) -> str:
return super().model_dump_json(serialize_as_any=True, **kwargs)
4 changes: 3 additions & 1 deletion openff/models/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ def coerce_json_back_to_quantity(
try:
return Quantity(*json_loader(v).values())
except Exception as error:
raise ValueError("str types need to be of the structure that can be imported back from JSON") from error
raise ValueError(
"str types need to be of the structure that can be imported back from JSON"
) from error

else:
raise ValueError(
Expand Down

0 comments on commit bbc2416

Please sign in to comment.