From 4f7656c065c74f223168908d44b87d6fd74dfcfc Mon Sep 17 00:00:00 2001 From: stevenhua0320 Date: Sat, 14 Mar 2026 01:13:13 -0400 Subject: [PATCH] fix: fix load_structure method with loading Path object. --- news/fix-load-structure.rst | 23 ++++++++++++++++++ src/diffpy/structure/__init__.py | 22 +++++++++++++++++- tests/test_loadstructure.py | 40 ++++++++++++++++++++++++++------ 3 files changed, 77 insertions(+), 8 deletions(-) create mode 100644 news/fix-load-structure.rst diff --git a/news/fix-load-structure.rst b/news/fix-load-structure.rst new file mode 100644 index 00000000..a324ad4f --- /dev/null +++ b/news/fix-load-structure.rst @@ -0,0 +1,23 @@ +**Added:** + +* Added method ``load_structure`` in ``__init__.py`` + +**Changed:** + +* + +**Deprecated:** + +* Deprecated method ``loadStructure`` in ``__init__.py`` for removal in version 4.0.0 + +**Removed:** + +* + +**Fixed:** + +* Fixed ``load_structure`` with successfully loading `Path` object + +**Security:** + +* diff --git a/src/diffpy/structure/__init__.py b/src/diffpy/structure/__init__.py index 969b7323..0b07bb8c 100644 --- a/src/diffpy/structure/__init__.py +++ b/src/diffpy/structure/__init__.py @@ -34,6 +34,7 @@ """ +import os import sys import diffpy.structure as _structure @@ -46,8 +47,17 @@ # package version from diffpy.structure.version import __version__ +from diffpy.utils._deprecator import build_deprecation_message, deprecated # Deprecations ------------------------------------------------------- +base = "diffpy.structure" +removal_version = "4.0.0" +loadStructure_deprecation_msg = build_deprecation_message( + base, + "loadStructure", + "load_structure", + removal_version, +) # @deprecated @@ -72,7 +82,17 @@ def __getattr__(self, name): # top level routines +@deprecated(loadStructure_deprecation_msg) def loadStructure(filename, fmt="auto", **kw): + """This function has been deprecated and will be removed in version + 4.0.0. + + Please use diffpy.structure.load_structure instead. + """ + return load_structure(filename, fmt, **kw) + + +def load_structure(filename, fmt="auto", **kw): """Load new structure object from the specified file. Parameters @@ -96,7 +116,7 @@ def loadStructure(filename, fmt="auto", **kw): Return a more specific PDFFitStructure type for 'pdffit' and 'discus' formats. """ - + filename = os.fspath(filename) p = get_parser(fmt, **kw) rv = p.parse_file(filename) return rv diff --git a/tests/test_loadstructure.py b/tests/test_loadstructure.py index 8bd1ec21..ae6979ba 100644 --- a/tests/test_loadstructure.py +++ b/tests/test_loadstructure.py @@ -6,7 +6,7 @@ import pytest -from diffpy.structure import PDFFitStructure, Structure, loadStructure +from diffpy.structure import PDFFitStructure, Structure, load_structure, loadStructure from diffpy.structure.structureerrors import StructureFormatError @@ -19,22 +19,22 @@ def prepare_fixture(self, datafile): def test_xcfg(self): """Check loading of atomeye xcfg format.""" f = self.datafile("BubbleRaftShort.xcfg") - stru = loadStructure(f) + stru = load_structure(f) self.assertTrue(type(stru) is Structure) - self.assertRaises(StructureFormatError, loadStructure, f, "xyz") + self.assertRaises(StructureFormatError, load_structure, f, "xyz") return def test_discus(self): """Check loading of discus file format.""" f = self.datafile("Ni-discus.stru") - stru = loadStructure(f) + stru = load_structure(f) self.assertTrue(type(stru) is PDFFitStructure) return def test_cif(self): """Check loading of CIF file format.""" f = self.datafile("PbTe.cif") - stru = loadStructure(f) + stru = load_structure(f) self.assertTrue(isinstance(stru, Structure)) self.assertFalse(isinstance(stru, PDFFitStructure)) return @@ -45,11 +45,17 @@ def test_badfile(self): self.assertRaises(StructureFormatError, loadStructure, f) return + def test_load_bad_file(self): + """Check loading of CIF file format.""" + f = self.datafile("Ni-bad.stru") + self.assertRaises(StructureFormatError, load_structure, f) + return + def test_goodkwarg(self): """Check loading of CIF file and passing of parser keyword argument.""" f = self.datafile("graphite.cif") - stru = loadStructure(f, eps=1e-10) + stru = load_structure(f, eps=1e-10) self.assertEqual(8, len(stru)) return @@ -57,13 +63,33 @@ def test_badkwarg(self): """Check loading of xyz file format with invalid keyword argument.""" f = self.datafile("bucky.xyz") - self.assertRaises(TypeError, loadStructure, f, eps=1e-10) + self.assertRaises(TypeError, load_structure, f, eps=1e-10) return # End of class TestLoadStructure + # ---------------------------------------------------------------------------- +@pytest.mark.parametrize( + "filename, expected", + [ # C1: Load the cif file in Path object, expected to load the Structure instance. + ("PbTe.cif", (True, False)), + ], +) +def test_load_structure_cif_in_path(datafile, filename, expected): + from pathlib import Path + + f = datafile(filename) + f_path = Path(f) + stru = load_structure(f_path) + actual = ( + isinstance(stru, Structure), + isinstance(stru, PDFFitStructure), + ) + + assert actual == expected + if __name__ == "__main__": unittest.main()