fix: raise KeyError (not ValueError) for missing custom property#12
Open
speckhard wants to merge 4 commits intoLeMaterial:mainfrom
Open
fix: raise KeyError (not ValueError) for missing custom property#12speckhard wants to merge 4 commits intoLeMaterial:mainfrom
speckhard wants to merge 4 commits intoLeMaterial:mainfrom
Conversation
mol.get_property("missing") now raises KeyError, matching mol["missing"]
which already raises KeyError. The two access patterns previously
disagreed on the exception type for the same condition.
Behavior change: callers catching ValueError specifically for
get_property will need to switch to KeyError. The exception message and
its str() are unchanged. Two in-repo tests that asserted ValueError have
been updated.
Independent review caught two follow-ups:
- _atompack_rs.pyi PyMolecule.get_property stub now documents the
KeyError it now raises.
- New test asserts mol["x"] and mol.get_property("x") raise the same
exception type so the previous drift can't recur silently.
The first commit on this branch updated test_atom_molecule.py and
test_database.py but missed two analogous assertions in test_from_ase.py
that exercise mol.get_property("stress") and mol.get_property("forces")
on absent keys. With the KeyError change to molecule.rs::get_property,
these tests would have failed at merge time.
The earlier ValueError → PyKeyError flip kept the original multi-line format. PyKeyError is shorter than PyValueError, so the format!() call now fits on one line under max_width=100, and rustfmt's canonical form demands the single-line variant. Caught by ci-rust (rust-fmt-check) on PR LeMaterial#12.
speckhard
added a commit
to speckhard/atompack
that referenced
this pull request
Apr 30, 2026
The new test_auto_max_size_caps_at_default_max test had two assert_eq! calls that exceeded max_width=100 once DEFAULT_MAX_DECOMPRESSED_SIZE was inlined. rustfmt's canonical form splits them across multiple lines. Caught proactively by mirroring the cargo fmt --all --check pass that PR LeMaterial#12 needed for its CI.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Make the two indexing patterns for missing custom properties agree on the exception type:
mol["missing"]KeyErrorKeyErrormol.get_property("missing")ValueErrorKeyErrorBoth now raise
KeyErrorwith the same message (Property 'X' not found).Why this is a behavior change worth shipping
KeyErroris the Python-native exception type for "key not found in a mapping-like object", and it's already whatmol[key]raised. Code that didtry: ... except KeyError:worked formol[key]but silently failed formol.get_property(key)because the latter slipped through asValueError.test_atom_molecule.py, one intest_database.py) — those are updated.Breaking-change scope
Callers who specifically caught
ValueErroronly forget_propertywill need to switch toKeyError. There is no in-repo caller doing that; the only places assertingValueErrorwere the two tests this PR updates.If broader impact is a concern, this can land in 0.3.0 instead of a 0.2.x patch — happy to defer.
Files
atompack-py/src/molecule.rs— twoPyValueError::new_err(...)→PyKeyError::new_err(...).PyKeyErrorwas already imported insrc/lib.rs.atompack-py/python/atompack/__init__.pyi—Raisessection in the user-facingget_propertydocstring.atompack-py/python/atompack/_atompack_rs.pyi—Raisessection also added to the internal stub for consistency.atompack-py/tests/test_atom_molecule.py— flip 2 assertions; newtest_missing_property_raises_keyerror_consistentlylocks in the symmetry so future drift fails loudly.atompack-py/tests/test_database.py— flip 1 assertion.Test plan
maturin develop --release:(124 = previous 123 + the new
test_missing_property_raises_keyerror_consistently.)