Skip to content

Commit 8d4c8f2

Browse files
Improve some typing (#2000)
* Improve some typing * Fix type annotation for OMMQuantity * Revert formatting changes * Restore support for py3.10 * Fix mypy errors * Type annotate return of _SimpleMolecule bond and atom methods * Type annotate arg of _SimpleMolecule bond and atom methods * Make HierarchyScheme.identifier variadic * Fix fstring in _detect_undefined_stereo * Fixes to get mypy passing * Hacks for Pint compatibility * Molecule.to_rdkit(): support residue number strings that aren't decimal ints * Update changelog * More type hints and more formatting reversions * fix missing annotation referent * Apply suggestions from code review * Replace typeignores with type annotation * Add type annotation for is_isomorphic_with(**kwargs) --------- Co-authored-by: Matthew W. Thompson <[email protected]>
1 parent 25570d0 commit 8d4c8f2

File tree

7 files changed

+142
-113
lines changed

7 files changed

+142
-113
lines changed

docs/releasehistory.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Releases follow the `major.minor.micro` scheme recommended by [PEP440](https://w
2323
### Bugfixes
2424

2525
- [PR #2052](https://github.com/openforcefield/openff-toolkit/pull/2052): Fixes bug where `Topology.from_pdb` couldn't load NH4+ ([Issue #2051](https://github.com/openforcefield/openff-toolkit/issues/2051))
26+
- [PR #2000](https://github.com/openforcefield/openff-toolkit/pull/2000): Assorted type annotation fixes
27+
- [PR #2000](https://github.com/openforcefield/openff-toolkit/pull/2000): `Molecule.to_rdkit()` no longer raises an exception when converting a molecule with non-decimal residue numbers to RDKit; instead, the RDKit residue number is simply silently left unset.
2628

2729
### Miscellaneous
2830

openff/toolkit/_tests/test_toolkits.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3508,6 +3508,26 @@ def test_to_rdkit_losing_aromaticity_(self):
35083508
for offatom, rdatom in zip(mol.atoms, rdmol.GetAtoms()):
35093509
assert offatom.is_aromatic is rdatom.GetIsAromatic()
35103510

3511+
def test_to_rdkit_str_resnum(self):
3512+
smiles = ("O")
3513+
3514+
mol = Molecule.from_smiles(smiles)
3515+
3516+
# Test an int, a string that can convert to an int, and a non-intable str
3517+
atom_resnums = [9998, "9999", "A000"]
3518+
# RDKit's default residue number is 0
3519+
expected_atom_resnums = [9998, 9999, 0]
3520+
3521+
for atom, resnum in zip(mol.atoms, atom_resnums):
3522+
atom.metadata["residue_number"] = resnum
3523+
atom.metadata["residue_name"] = "HOH"
3524+
3525+
rdmol = mol.to_rdkit()
3526+
3527+
# now make sure the residue number matches for each atom
3528+
for resnum, rdatom in zip(expected_atom_resnums, rdmol.GetAtoms()):
3529+
assert rdatom.GetPDBResidueInfo().GetResidueNumber() == resnum
3530+
35113531
@pytest.mark.slow
35123532
def test_max_substructure_matches_can_handle_large_molecule(self):
35133533
"""Test RDKitToolkitWrapper substructure search handles more than the default of maxMatches = 1000

openff/toolkit/topology/_mm_molecule.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ def n_bonds(self) -> int:
8282
def n_conformers(self) -> int:
8383
return 0 if self._conformers is None else len(self._conformers)
8484

85-
def atom(self, index):
85+
def atom(self, index: int) -> "_SimpleAtom":
8686
return self.atoms[index]
8787

8888
def atom_index(self, atom) -> int:
8989
return self.atoms.index(atom)
9090

91-
def bond(self, index):
91+
def bond(self, index: int) -> "_SimpleBond":
9292
return self.bonds[index]
9393

9494
def get_bond_between(self, atom1_index, atom2_index):
@@ -369,7 +369,8 @@ def from_dict(cls, molecule_dict):
369369
atom1_index = bond_dict["atom1_index"]
370370
atom2_index = bond_dict["atom2_index"]
371371
molecule.add_bond(
372-
atom1=molecule.atom(atom1_index), atom2=molecule.atom(atom2_index)
372+
atom1=molecule.atom(atom1_index),
373+
atom2=molecule.atom(atom2_index)
373374
)
374375

375376
conformers = molecule_dict.pop("conformers")

0 commit comments

Comments
 (0)