Skip to content

Define Pydantic schema on Quantity - #178

Merged
mattwthompson merged 23 commits into
mainfrom
define-pydantic-schema
Jul 10, 2026
Merged

Define Pydantic schema on Quantity#178
mattwthompson merged 23 commits into
mainfrom
define-pydantic-schema

Conversation

@mattwthompson

@mattwthompson mattwthompson commented May 26, 2026

Copy link
Copy Markdown
Member

Description

This is a possible solution for #177
and openforcefield/openff-interchange#1346 and arguably a better design from the ground-up than what we had in openff-models

Todos

  • Handle different inputs
    • str
    • Quantity
    • list or similar iterable
    • dict
    • numpy.ndarray
  • Test different serialization pathways
    • Python
    • JSON
  • Test different de-serialization pathways
    • Python
    • JSON
  • Test unit/dimensionality edge cases?
  • Make Pydantic optional?
  • Test against other OpenFF libraries: Test against "new Pydantic" branches status#147

Status

  • Ready to go

@mattwthompson
mattwthompson force-pushed the define-pydantic-schema branch 2 times, most recently from a819b49 to d09b154 Compare May 26, 2026 17:30
Fixes

Add Pydantic depedency to Python project metadata

Update lockfile
@mattwthompson
mattwthompson force-pushed the define-pydantic-schema branch from a6b977c to 3b36116 Compare May 26, 2026 17:33
@codecov-commenter

codecov-commenter commented May 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.95918% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 91.37%. Comparing base (742d2bd) to head (c69d5d7).

Files with missing lines Patch % Lines
openff/units/units.py 97.95% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #178      +/-   ##
==========================================
+ Coverage   89.83%   91.37%   +1.53%     
==========================================
  Files           6        6              
  Lines         187      232      +45     
==========================================
+ Hits          168      212      +44     
- Misses         19       20       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@j-wags j-wags left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spoke with the OpenFE leads and concluded that we should move forward with this approach. Please let me know once this is ready for review!

@mattwthompson
mattwthompson marked this pull request as ready for review June 17, 2026 18:30

@j-wags j-wags left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mattwthompson - This looks good to me. A few comments but none blocking. Thanks for reviewing this with me live today.

  • (not blocking) If the previous version supported list of lists of uneven length (eg [[1,2],[3]]) it would be good to either ensure that works after these changes, or test that defining/serializing/deserializing such a value raises an error.

Comment thread openff/units/units.py
Comment on lines +67 to +69
# TODO: I think this is necessary for handling unit-wrapped arrays, but it is
# not so performant. Scalar quantities can be directly serialized to much
# shorter strings

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(not blocking) If performance is a concern, maybe add a test defining/serializing/deserializing a big array quantity and test performance with a timeout (even if the current performance is acceptable, this could prevent other changes in the future from exacerbating performance issues)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid concern but deferring this until it's diagnosed as an issue

Comment thread openff/units/units.py
# Quantity constructor, but "{"magnitude": 0.9, "units": "nanometer"}" cannot
# as it needs to be unwrapped. A better solution would require a better way
# of serializing unit-wrapped arrays to JSON
if "{" in v:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(not blocking) 👍 I was certain I was about to see an eval statement after this, nice work not needing that.

Comment thread openff/units/units.py

@requires_package("openmm")
def _to_openmm(self) -> "openmm.unit.Quantity":
def _to_openmm(self) -> openmm.unit.Quantity:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(not blocking) You'd asked me during synchronous review to flag this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was concerned this would break things if OpenMM isn't installed, but that's not the case because I turned on deferred annotations (from __future__ import annotations)

[openff-units] pixi run -e py312pint24 python         define-pydantic-schema  ✱
Python 3.12.13 | packaged by conda-forge | (main, Mar  5 2026, 17:06:14) [Clang 19.1.7 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import openmm
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'openmm'
>>> from openff.units import *
>>> Quantity("1 picosecond")
<Quantity(1, 'picosecond')>

@mattwthompson mattwthompson mentioned this pull request Jul 10, 2026
3 tasks
@mattwthompson
mattwthompson merged commit 17e8b6b into main Jul 10, 2026
80 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants