From daf42d2a60b1821cfb12290076a0223c715810a7 Mon Sep 17 00:00:00 2001 From: Matteo Giantomassi Date: Fri, 23 Feb 2024 11:31:08 +0100 Subject: [PATCH] =?UTF-8?q?Remove=20properties=20from=20abivars=20dict=20a?= =?UTF-8?q?s=20this=20breaks=20the=20interface=20with=E2=80=A6=20(#3642)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Remove properties from abivars dict as this breaks the interface with Abinit * Fix test_to_from_abivars * Fix typo * tweak comments --------- Co-authored-by: Janosh Riebesell --- pymatgen/io/abinit/abiobjects.py | 2 -- pymatgen/io/abinit/netcdf.py | 4 ++++ tests/core/test_structure.py | 9 +++++++-- tests/io/abinit/test_abiobjects.py | 1 + tests/io/abinit/test_netcdf.py | 2 ++ 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pymatgen/io/abinit/abiobjects.py b/pymatgen/io/abinit/abiobjects.py index 9a559deabe9..07fcf39732a 100644 --- a/pymatgen/io/abinit/abiobjects.py +++ b/pymatgen/io/abinit/abiobjects.py @@ -158,7 +158,6 @@ def structure_from_abivars(cls=None, *args, **kwargs) -> Structure: validate_proximity=False, to_unit_cell=False, coords_are_cartesian=coords_are_cartesian, - properties=kwargs.get("properties"), ) @@ -251,7 +250,6 @@ def structure_to_abivars( "typat": typat, "znucl": znucl_type, "xred": x_red, - "properties": structure.properties, } # Add info on the lattice. diff --git a/pymatgen/io/abinit/netcdf.py b/pymatgen/io/abinit/netcdf.py index 6848fbf9d86..f1dc71a8f20 100644 --- a/pymatgen/io/abinit/netcdf.py +++ b/pymatgen/io/abinit/netcdf.py @@ -471,3 +471,7 @@ def to_str(self, verbose=0, title=None, **kwargs): if title is not None: return "\n".join([marquee(title, mark="="), header_str]) return header_str + + # to_string alias required for backwards compatibility + # PLEASE DO NOT REMOVE THIS LINE AS THIS API HAS BEEN AROUND FOR SEVERAL YEARS + to_string = to_str diff --git a/tests/core/test_structure.py b/tests/core/test_structure.py index 92fbd3e8766..41f9d102405 100644 --- a/tests/core/test_structure.py +++ b/tests/core/test_structure.py @@ -1307,9 +1307,14 @@ def test_default_dict_attrs(self): def test_to_from_abivars(self): """Test as_dict, from_dict with fmt == abivars.""" - dct = self.struct.as_dict(fmt="abivars") + # Properties are not supported if fmt="abivars" as its not a serialization protocol + # but a format that allows one to get a dict with the abinit variables defining the structure. + struct = self.struct.copy() + struct.properties = {} + dct = struct.as_dict(fmt="abivars") + assert "properties" not in dct s2 = Structure.from_dict(dct, fmt="abivars") - assert s2 == self.struct + assert s2 == struct assert isinstance(s2, Structure) def test_to_from_file_str(self): diff --git a/tests/io/abinit/test_abiobjects.py b/tests/io/abinit/test_abiobjects.py index ccd427b10fe..90ad69acd72 100644 --- a/tests/io/abinit/test_abiobjects.py +++ b/tests/io/abinit/test_abiobjects.py @@ -97,6 +97,7 @@ def test_znucl_typat(self): assert_array_equal(enf_vars["znucl"], enforce_znucl) assert_array_equal(enf_vars["typat"], enforce_typat) assert_array_equal(def_vars["xred"], enf_vars["xred"]) + assert "properties" not in enf_vars assert [s.symbol for s in species_by_znucl(gan)] == ["Ga", "N"] diff --git a/tests/io/abinit/test_netcdf.py b/tests/io/abinit/test_netcdf.py index e9f78d801ff..8a954d79a86 100644 --- a/tests/io/abinit/test_netcdf.py +++ b/tests/io/abinit/test_netcdf.py @@ -93,3 +93,5 @@ def test_api(self): assert head.foo == 1 assert str(head) assert head.to_str(verbose=2, title="title") + # PLEASE DO NOT REMOVE THIS LINE AS THIS API HAS BEEN AROUND FOR SEVERAL YEARS, + assert head.to_string(verbose=2, title="title")