From 7b048906d173a5ad872a7185c76c42a16f2136db Mon Sep 17 00:00:00 2001 From: Sebastian Ehlert <28669218+awvwgk@users.noreply.github.com> Date: Tue, 2 May 2023 19:47:19 +0200 Subject: [PATCH] Make sure extras is initialized as dict for Molecule (#305) * Make sure extras is initialized as dict for Molecule * Apply reformatting with black --- qcelemental/models/molecule.py | 2 ++ qcelemental/tests/test_molecule.py | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/qcelemental/models/molecule.py b/qcelemental/models/molecule.py index 1d704049..f014af75 100644 --- a/qcelemental/models/molecule.py +++ b/qcelemental/models/molecule.py @@ -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 diff --git a/qcelemental/tests/test_molecule.py b/qcelemental/tests/test_molecule.py index 5344b182..5d4c07d8 100644 --- a/qcelemental/tests/test_molecule.py +++ b/qcelemental/tests/test_molecule.py @@ -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) @@ -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"