Skip to content

Commit

Permalink
Make sure extras is initialized as dict for Molecule (#305)
Browse files Browse the repository at this point in the history
* Make sure extras is initialized as dict for Molecule

* Apply reformatting with black
  • Loading branch information
awvwgk committed May 2, 2023
1 parent f27c924 commit 7b04890
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions qcelemental/models/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ def __init__(self, orient: bool = False, validate: Optional[bool] = None, **kwar
kwargs = {**kwargs, **schema} # Allow any extra fields
validate = True

if "extras" not in kwargs:
kwargs["extras"] = {}
super().__init__(**kwargs)

# We are pulling out the values *explicitly* so that the pydantic skip_defaults works as expected
Expand Down
9 changes: 9 additions & 0 deletions qcelemental/tests/test_molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ def test_sparse_molecule_fields(mol_string, extra_keys):
"fix_com",
"fix_orientation",
"provenance",
"extras",
}
mol = Molecule.from_data(mol_string)

Expand Down Expand Up @@ -724,3 +725,11 @@ def test_nonphysical_spec():
assert compare_values([100.0], mol.masses, "nonphysical mass")

print(mol.to_string(dtype="psi4"))


def test_extras():
mol = qcel.models.Molecule(symbols=["He"], geometry=[0, 0, 0])
assert mol.extras is not None

mol = qcel.models.Molecule(symbols=["He"], geometry=[0, 0, 0], extras={"foo": "bar"})
assert mol.extras["foo"] == "bar"

0 comments on commit 7b04890

Please sign in to comment.