Skip to content

deprecate(ModelOutput.quantity): mark quantity field as deprecated#199

Open
HaoZeke wants to merge 1 commit intometatensor:mainfrom
HaoZeke:morfixes
Open

deprecate(ModelOutput.quantity): mark quantity field as deprecated#199
HaoZeke wants to merge 1 commit intometatensor:mainfrom
HaoZeke:morfixes

Conversation

@HaoZeke
Copy link
Copy Markdown
Member

@HaoZeke HaoZeke commented Apr 3, 2026

The quantity field is no longer required for unit conversion since the unit parser determines dimensions from the expression itself.

Changes:

  • Add deprecated documentation to C++ header
  • Add TORCH_WARN in C++ setter when quantity is non-empty
  • Add DeprecationWarning in Python when quantity validation runs
  • Update documentation with deprecation notice
  • Add deprecation entries to CHANGELOG

Contributor (creator of pull-request) checklist

  • Tests updated (for new features and bugfixes)?
  • Documentation updated (for new features)?
  • Issue referenced (for PRs that solve an issue)?

Reviewer checklist

  • CHANGELOG updated with public API or any other important changes?

@HaoZeke HaoZeke force-pushed the morfixes branch 3 times, most recently from 7b73a2a to 409b32c Compare April 5, 2026 00:50
@HaoZeke HaoZeke requested a review from Luthaf April 5, 2026 01:06
/// quantity of the output (e.g. energy, dipole, …). If this is an empty
/// string, no unit conversion will be performed.
/// @deprecated This field is no longer required for unit conversion.
/// The unit parser determines dimensions from the expression itself.
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.

Can you do a [[deprecated]] atribute instead, both here and on the setter?

Comment thread metatomic-torch/src/model.cpp Outdated

void ModelOutputHolder::set_quantity(std::string quantity) {
if (!quantity.empty()) {
TORCH_WARN(
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.

Should be a torch deprecation warning, there is a special macro for it

Comment thread python/metatomic_ase/tests/conftest.py Outdated
yield
captured = capfd.readouterr()
# the code should not print anything to stdout or stderr
# except for expected deprecation warnings
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.

These should be explicitly caught at the place where they are emitted in the test, by using the capfd fixture in the tests. I'm doing this in a couple of place already

Comment on lines +513 to +517
# Note: Deprecation warning for quantity is emitted from C++ when
# ModelOutput is constructed with non-empty quantity.
# We don't warn here because TorchScript can't handle
# DeprecationWarning.

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.

we should remove the code here that uses quantity

yield
captured = capfd.readouterr()
# the code should not print anything to stdout or stderr
# except for expected deprecation warnings
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.

Same as above

yield
captured = capfd.readouterr()
# the code should not print anything to stdout or stderr
# except for expected deprecation warnings
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.

Same as above

Comment on lines +50 to +53
# consume the once-per-process quantity deprecation warning from C++
captured = capfd.readouterr()
if captured.err:
assert "ModelOutput.quantity is deprecated" in captured.err
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.

We can change the code in the test LJ to remove the need to catch this warning

…-based unit conversion

The quantity field on ModelOutput is no longer required: the unit
expression parser determines dimensions directly, so unit conversion
no longer needs the quantity hint.

C++ (metatomic-torch)
- Mark `quantity()` getter and `set_quantity()` setter with
  `[[deprecated]]` so callers get a compile-time warning.
- In `set_quantity`, emit `TORCH_WARN_DEPRECATION` when a non-empty
  quantity is provided so JSON-deserialized models with `quantity` set
  surface the deprecation at runtime.

Python (metatomic-torch)
- Drop the quantity-vs-quantity gating and mismatch check in
  `model.py`; gate unit conversion on `unit` being non-empty instead.
  The unit parser already validates dimensional consistency.
- Add a `.. deprecated::` block to the `quantity` property docstring.

Tests / packaging
- conftest.py for all three subpackages: regex-strip the C++
  `TORCH_WARN_DEPRECATION` line from stderr in the autouse
  fail-on-output fixture, tolerating both Torch 2.3 (`[W model.cpp:N]`)
  and Torch 2.11+ (`[W<MMDD> HH:MM:SS.fractZ model.cpp:N]`) prefixes.
  Any other unexpected stderr still fails the test.
- `pyproject.toml` filterwarnings: ignore the matching Python
  `DeprecationWarning` so tests that exercise the field directly do
  not blow up under `-W error`.
- Update tests/examples that constructed `ModelOutput(quantity=...)`
  to drop the parameter where the test is not specifically about
  quantity validation.
- Update C++ models.cpp test to allow the deprecation warning.

Changelog: add a Deprecated entry for `ModelOutput.quantity`.
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.

2 participants