From 87dd1fb000308a5348ecef04400c89908156284a Mon Sep 17 00:00:00 2001 From: mhucka <1450019+mhucka@users.noreply.github.com> Date: Tue, 31 Mar 2026 21:36:10 +0000 Subject: [PATCH 1/9] Add test coverage for missing general_calculations in MolecularData.load This change removes the `pragma: nocover` and the associated TODO in `src/openfermion/chem/molecular_data.py` by adding a test case in `src/openfermion/chem/molecular_data_test.py` that triggers the branch where `general_calculations_keys` or `general_calculations_values` are missing from the HDF5 file. --- src/openfermion/chem/molecular_data.py | 5 +---- src/openfermion/chem/molecular_data_test.py | 22 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/openfermion/chem/molecular_data.py b/src/openfermion/chem/molecular_data.py index d95bf43a9..ea987ed27 100644 --- a/src/openfermion/chem/molecular_data.py +++ b/src/openfermion/chem/molecular_data.py @@ -910,10 +910,7 @@ def load(self): for key, value in zip(keys[...], values[...]) } else: - # TODO: test the no cover - # no coverage here because pathway is check on - # bad user generated file - self.general_calculations = None # pragma: nocover + self.general_calculations = None def get_from_file(self, property_name): """Helper routine to re-open HDF5 file and pull out single property. diff --git a/src/openfermion/chem/molecular_data_test.py b/src/openfermion/chem/molecular_data_test.py index efba40ea1..54930ef6d 100644 --- a/src/openfermion/chem/molecular_data_test.py +++ b/src/openfermion/chem/molecular_data_test.py @@ -13,6 +13,7 @@ import os import unittest +import h5py import numpy.random import scipy.linalg import numpy as np @@ -347,3 +348,24 @@ def test_missing_calcs_for_integrals(self): molecule.get_k() with self.assertRaises(MissingCalculationError): molecule.get_antisym() + + def test_load_no_general_calculations(self): + # Make fake molecule. + filename = os.path.join(DATA_DIRECTORY, 'dummy_molecule_no_gen_calc') + geometry = [('H', (0.0, 0.0, 0.0)), ('H', (0.0, 0.0, 0.7414))] + basis = '6-31g*' + multiplicity = 1 + molecule = MolecularData(geometry, basis, multiplicity, filename=filename) + molecule.save() + + # Manually remove the general_calculations_keys dataset. + with h5py.File(filename + '.hdf5', 'r+') as f: + del f['general_calculations_keys'] + + try: + # Load the molecule and check that general_calculations is None. + new_molecule = MolecularData(filename=filename) + new_molecule.load() + self.assertIsNone(new_molecule.general_calculations) + finally: + os.remove(filename + '.hdf5') From 6e6e541e56620684324aadb32c76e1ef44c1a12f Mon Sep 17 00:00:00 2001 From: Michael Hucka Date: Tue, 31 Mar 2026 15:19:29 -0700 Subject: [PATCH 2/9] Update src/openfermion/chem/molecular_data.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- src/openfermion/chem/molecular_data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openfermion/chem/molecular_data.py b/src/openfermion/chem/molecular_data.py index ea987ed27..6e70993f7 100644 --- a/src/openfermion/chem/molecular_data.py +++ b/src/openfermion/chem/molecular_data.py @@ -910,7 +910,7 @@ def load(self): for key, value in zip(keys[...], values[...]) } else: - self.general_calculations = None + self.general_calculations = {} def get_from_file(self, property_name): """Helper routine to re-open HDF5 file and pull out single property. From b84bae11d20519f28439c5df5b7311a9f9406765 Mon Sep 17 00:00:00 2001 From: Michael Hucka Date: Tue, 31 Mar 2026 15:20:33 -0700 Subject: [PATCH 3/9] Update src/openfermion/chem/molecular_data_test.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- src/openfermion/chem/molecular_data_test.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/openfermion/chem/molecular_data_test.py b/src/openfermion/chem/molecular_data_test.py index 54930ef6d..a1828041b 100644 --- a/src/openfermion/chem/molecular_data_test.py +++ b/src/openfermion/chem/molecular_data_test.py @@ -356,16 +356,15 @@ def test_load_no_general_calculations(self): basis = '6-31g*' multiplicity = 1 molecule = MolecularData(geometry, basis, multiplicity, filename=filename) - molecule.save() + try: + molecule.save() - # Manually remove the general_calculations_keys dataset. - with h5py.File(filename + '.hdf5', 'r+') as f: - del f['general_calculations_keys'] + # Manually remove the general_calculations_keys dataset. + with h5py.File(filename + '.hdf5', 'r+') as f: + del f['general_calculations_keys'] - try: - # Load the molecule and check that general_calculations is None. + # Load the molecule and check that general_calculations is empty. new_molecule = MolecularData(filename=filename) - new_molecule.load() - self.assertIsNone(new_molecule.general_calculations) + self.assertEqual(new_molecule.general_calculations, {}) finally: os.remove(filename + '.hdf5') From 44f7c5564b117b8eb9eb03f8e8e96150d4d1ffc8 Mon Sep 17 00:00:00 2001 From: Michael Hucka Date: Fri, 3 Apr 2026 22:33:10 -0700 Subject: [PATCH 4/9] Use NamedTemporaryFile --- src/openfermion/chem/molecular_data_test.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/openfermion/chem/molecular_data_test.py b/src/openfermion/chem/molecular_data_test.py index a1828041b..3a6049625 100644 --- a/src/openfermion/chem/molecular_data_test.py +++ b/src/openfermion/chem/molecular_data_test.py @@ -12,6 +12,7 @@ """Tests for molecular_data.""" import os +import tempfile import unittest import h5py import numpy.random @@ -351,20 +352,19 @@ def test_missing_calcs_for_integrals(self): def test_load_no_general_calculations(self): # Make fake molecule. - filename = os.path.join(DATA_DIRECTORY, 'dummy_molecule_no_gen_calc') - geometry = [('H', (0.0, 0.0, 0.0)), ('H', (0.0, 0.0, 0.7414))] - basis = '6-31g*' - multiplicity = 1 - molecule = MolecularData(geometry, basis, multiplicity, filename=filename) - try: + with tempfile.NamedTemporaryFile(suffix='.hdf5') as tmp: + filename = tmp.name + geometry = [('H', (0.0, 0.0, 0.0)), ('H', (0.0, 0.0, 0.7414))] + basis = '6-31g*' + multiplicity = 1 + molecule = MolecularData(geometry, basis, multiplicity, filename=filename) molecule.save() # Manually remove the general_calculations_keys dataset. - with h5py.File(filename + '.hdf5', 'r+') as f: + with h5py.File(filename, 'r+') as f: del f['general_calculations_keys'] # Load the molecule and check that general_calculations is empty. new_molecule = MolecularData(filename=filename) self.assertEqual(new_molecule.general_calculations, {}) - finally: - os.remove(filename + '.hdf5') + From ca33b65a5fed6acbca0dbb161558ea0da153a673 Mon Sep 17 00:00:00 2001 From: Michael Hucka Date: Fri, 3 Apr 2026 22:41:55 -0700 Subject: [PATCH 5/9] Format --- src/openfermion/chem/molecular_data_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/openfermion/chem/molecular_data_test.py b/src/openfermion/chem/molecular_data_test.py index 3a6049625..c7b2e86b4 100644 --- a/src/openfermion/chem/molecular_data_test.py +++ b/src/openfermion/chem/molecular_data_test.py @@ -368,3 +368,4 @@ def test_load_no_general_calculations(self): new_molecule = MolecularData(filename=filename) self.assertEqual(new_molecule.general_calculations, {}) + From 57c5f88cf8b7d58b52b0808cafef9fbf4cecc836 Mon Sep 17 00:00:00 2001 From: Michael Hucka Date: Fri, 3 Apr 2026 22:44:33 -0700 Subject: [PATCH 6/9] Update src/openfermion/chem/molecular_data_test.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- src/openfermion/chem/molecular_data_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/openfermion/chem/molecular_data_test.py b/src/openfermion/chem/molecular_data_test.py index c7b2e86b4..1f17bf1aa 100644 --- a/src/openfermion/chem/molecular_data_test.py +++ b/src/openfermion/chem/molecular_data_test.py @@ -352,8 +352,8 @@ def test_missing_calcs_for_integrals(self): def test_load_no_general_calculations(self): # Make fake molecule. - with tempfile.NamedTemporaryFile(suffix='.hdf5') as tmp: - filename = tmp.name + with tempfile.TemporaryDirectory() as tmp_dir: + filename = os.path.join(tmp_dir, 'test_molecule.hdf5') geometry = [('H', (0.0, 0.0, 0.0)), ('H', (0.0, 0.0, 0.7414))] basis = '6-31g*' multiplicity = 1 From aa7b08af48c102785ea1f7f5f3b23a8fde282126 Mon Sep 17 00:00:00 2001 From: Michael Hucka Date: Fri, 3 Apr 2026 22:52:28 -0700 Subject: [PATCH 7/9] Update molecular_data.py --- src/openfermion/chem/molecular_data.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/openfermion/chem/molecular_data.py b/src/openfermion/chem/molecular_data.py index 6e70993f7..9229f80ea 100644 --- a/src/openfermion/chem/molecular_data.py +++ b/src/openfermion/chem/molecular_data.py @@ -909,6 +909,8 @@ def load(self): key.tobytes().decode('utf-8'): value for key, value in zip(keys[...], values[...]) } + else: + self.general_calculations = {} else: self.general_calculations = {} From 92557d437e92b72c0f6e173acb5a44aef410a080 Mon Sep 17 00:00:00 2001 From: Michael Hucka Date: Fri, 3 Apr 2026 22:53:35 -0700 Subject: [PATCH 8/9] Delete blank lines --- src/openfermion/chem/molecular_data_test.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/openfermion/chem/molecular_data_test.py b/src/openfermion/chem/molecular_data_test.py index 1f17bf1aa..e94b6e9d1 100644 --- a/src/openfermion/chem/molecular_data_test.py +++ b/src/openfermion/chem/molecular_data_test.py @@ -367,5 +367,3 @@ def test_load_no_general_calculations(self): # Load the molecule and check that general_calculations is empty. new_molecule = MolecularData(filename=filename) self.assertEqual(new_molecule.general_calculations, {}) - - From 1248d8b42f97a82eef66d1220764a4e14b8c8129 Mon Sep 17 00:00:00 2001 From: mhucka Date: Sat, 4 Apr 2026 06:09:06 +0000 Subject: [PATCH 9/9] Use NamedTemporaryFile, not TemporaryDirectory --- src/openfermion/chem/molecular_data_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/openfermion/chem/molecular_data_test.py b/src/openfermion/chem/molecular_data_test.py index e94b6e9d1..7e12f328e 100644 --- a/src/openfermion/chem/molecular_data_test.py +++ b/src/openfermion/chem/molecular_data_test.py @@ -352,8 +352,8 @@ def test_missing_calcs_for_integrals(self): def test_load_no_general_calculations(self): # Make fake molecule. - with tempfile.TemporaryDirectory() as tmp_dir: - filename = os.path.join(tmp_dir, 'test_molecule.hdf5') + with tempfile.NamedTemporaryFile(suffix='.hdf5') as tmp_file: + filename = tmp_file.name geometry = [('H', (0.0, 0.0, 0.0)), ('H', (0.0, 0.0, 0.7414))] basis = '6-31g*' multiplicity = 1