Skip to content

Commit a056d78

Browse files
fix(rdkit): avoid deprecated explicit valence API (#969)
Problem - RDKit deprecated `Atom.GetExplicitValence()` in recent releases, which makes dpdata CI emit a deprecation warning. - `dpdata.rdkit.sanitize.get_explicit_valence()` still called the deprecated API directly. Change - Prefer `Atom.GetValence(Chem.ValenceType.EXPLICIT)` when available, with a fallback to `Atom.GetExplicitValence()` for compatibility with older RDKit versions. - Add a focused unit test that verifies both the new-API path and the legacy fallback path without requiring RDKit at test time. Notes - Fixes #968. - Tests run: `python3 -m unittest tests/test_rdkit_sanitize.py` Authored by OpenClaw (model: gpt-5.4) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **Chores** * Enhanced compatibility by adding automatic version detection and fallback support for different RDKit library versions to ensure consistent behavior. * **Tests** * Added comprehensive test coverage to verify valence calculations work correctly across different RDKit library versions. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 38e8763 commit a056d78

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

dpdata/rdkit/sanitize.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ def get_explicit_valence(atom, verbose=False):
1010
sum([bond.GetBondTypeAsDouble() for bond in atom.GetBonds()])
1111
)
1212
try:
13-
exp_val = atom.GetExplicitValence()
13+
try:
14+
from rdkit import Chem
15+
16+
exp_val = atom.GetValence(Chem.ValenceType.EXPLICIT)
17+
valence_method = "GetValence(Chem.ValenceType.EXPLICIT)"
18+
except (ImportError, AttributeError, TypeError):
19+
exp_val = atom.GetExplicitValence()
20+
valence_method = "GetExplicitValence()"
1421
if exp_val != exp_val_calculated_from_bonds:
1522
if verbose:
1623
print(
17-
f"Explicit valence given by GetExplicitValence() and sum of bond order are inconsistent on {atom.GetSymbol()}{atom.GetIdx() + 1}, using sum of bond order."
24+
f"Explicit valence given by {valence_method} and sum of bond order are inconsistent on {atom.GetSymbol()}{atom.GetIdx() + 1}, using sum of bond order."
1825
)
1926
return exp_val_calculated_from_bonds
2027
except Exception:

0 commit comments

Comments
 (0)