From ff5438cac986d1c0353321b7a3ae59905d2bae0d Mon Sep 17 00:00:00 2001 From: Zhi Ming Xu Date: Tue, 25 Nov 2025 00:11:09 -0500 Subject: [PATCH 1/3] chore: deprecate setScatteringFactorTableByType by emitting deprecation warning --- src/extensions/wrap_ScatteringFactorTable.cpp | 28 ++++++++++++++++--- tests/test_debyepdfcalculator.py | 3 +- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/extensions/wrap_ScatteringFactorTable.cpp b/src/extensions/wrap_ScatteringFactorTable.cpp index f11d5467..5b88e532 100644 --- a/src/extensions/wrap_ScatteringFactorTable.cpp +++ b/src/extensions/wrap_ScatteringFactorTable.cpp @@ -158,8 +158,11 @@ const char* doc_ScatteringFactorTableOwner_setScatteringFactorTableByType = "\ Set internal ScatteringFactorTable according to specified string type.\n\ \n\ tp -- string identifier of a registered ScatteringFactorTable type.\n\ - Use ScatteringFactorTable.getRegisteredTypes for the allowed values.\n\ + Use ScatteringFactorTable.getRegisteredTypes for the allowed values.\n\ \n\ +Deprecated: This method is deprecated and will be removed in a future release.\n\ +Use direct assignment to the `scatteringfactortable` property instead, for example:\n\ + obj.scatteringfactortable = SFTNeutron()\n\ No return value.\n\ "; @@ -399,10 +402,27 @@ void wrap_ScatteringFactorTable() getsftable, setsftable, doc_ScatteringFactorTableOwner_scatteringfactortable) + // deprecated: prefer assigning the `scatteringfactortable` property .def("setScatteringFactorTableByType", - &SFTOwner::setScatteringFactorTableByType, - bp::arg("tp"), - doc_ScatteringFactorTableOwner_setScatteringFactorTableByType) + +[](SFTOwner& obj, const std::string& tp) + { + namespace bp = boost::python; + try + { + bp::object warnings = bp::import("warnings"); + bp::object builtins = bp::import("builtins"); + bp::object DeprecationWarning = builtins.attr("DeprecationWarning"); + warnings.attr("warn")( + std::string("setScatteringFactorTableByType is deprecated; " + "assign the 'scatteringfactortable' property directly (for example, use SFTNeutron()/SFTXray())."), + DeprecationWarning, + 2); + } + catch (...) { /* don't let warnings break the binding */ } + obj.setScatteringFactorTableByType(tp); + }, + bp::arg("tp"), + doc_ScatteringFactorTableOwner_setScatteringFactorTableByType) .def("getRadiationType", &SFTOwner::getRadiationType, return_value_policy(), diff --git a/tests/test_debyepdfcalculator.py b/tests/test_debyepdfcalculator.py index ff160f88..a4b6d9c1 100644 --- a/tests/test_debyepdfcalculator.py +++ b/tests/test_debyepdfcalculator.py @@ -142,7 +142,8 @@ def test_partial_pdfs(self): def test_pickling(self): """Check pickling and unpickling of PDFCalculator.""" dpdfc = self.dpdfc - dpdfc.setScatteringFactorTableByType("N") + with self.assertWarns(DeprecationWarning): + dpdfc.setScatteringFactorTableByType("N") dpdfc.scatteringfactortable.setCustomAs("Na", "Na", 7) dpdfc.addEnvelope("sphericalshape") dpdfc.debyeprecision = 0.001 From b766404c39b69adab124de2bbd19ff3605f855c6 Mon Sep 17 00:00:00 2001 From: Zhi Ming Xu Date: Tue, 25 Nov 2025 00:15:49 -0500 Subject: [PATCH 2/3] skpkg: add news file --- news/deprecate-setscattering.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 news/deprecate-setscattering.rst diff --git a/news/deprecate-setscattering.rst b/news/deprecate-setscattering.rst new file mode 100644 index 00000000..a010e6b4 --- /dev/null +++ b/news/deprecate-setscattering.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* + +**Deprecated:** + +* Deprecated setScatteringFactorTableByType. + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* From 80fa50186e8764474becf62d0d50faa62fa8ae22 Mon Sep 17 00:00:00 2001 From: Zhi Ming Xu Date: Fri, 28 Nov 2025 15:56:13 -0500 Subject: [PATCH 3/3] chore: write tests for old syntax and specify future release deprecation --- src/extensions/wrap_ScatteringFactorTable.cpp | 5 +++-- tests/test_debyepdfcalculator.py | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/extensions/wrap_ScatteringFactorTable.cpp b/src/extensions/wrap_ScatteringFactorTable.cpp index 5b88e532..d7e1875c 100644 --- a/src/extensions/wrap_ScatteringFactorTable.cpp +++ b/src/extensions/wrap_ScatteringFactorTable.cpp @@ -160,7 +160,7 @@ Set internal ScatteringFactorTable according to specified string type.\n\ tp -- string identifier of a registered ScatteringFactorTable type.\n\ Use ScatteringFactorTable.getRegisteredTypes for the allowed values.\n\ \n\ -Deprecated: This method is deprecated and will be removed in a future release.\n\ +Deprecated: This method is deprecated and will be removed in the 2.0.0 release.\n\ Use direct assignment to the `scatteringfactortable` property instead, for example:\n\ obj.scatteringfactortable = SFTNeutron()\n\ No return value.\n\ @@ -414,7 +414,8 @@ void wrap_ScatteringFactorTable() bp::object DeprecationWarning = builtins.attr("DeprecationWarning"); warnings.attr("warn")( std::string("setScatteringFactorTableByType is deprecated; " - "assign the 'scatteringfactortable' property directly (for example, use SFTNeutron()/SFTXray())."), + "assign the 'scatteringfactortable' property directly, for example:\n" + "obj.scatteringfactortable = SFTNeutron()"), DeprecationWarning, 2); } diff --git a/tests/test_debyepdfcalculator.py b/tests/test_debyepdfcalculator.py index a4b6d9c1..7a41f66d 100644 --- a/tests/test_debyepdfcalculator.py +++ b/tests/test_debyepdfcalculator.py @@ -5,11 +5,13 @@ import pickle import unittest +import warnings import numpy from testutils import _maxNormDiff, loadDiffPyStructure, pickle_with_attr from diffpy.srreal.pdfcalculator import DebyePDFCalculator, PDFCalculator +from diffpy.srreal.scatteringfactortable import SFTNeutron ############################################################################## @@ -141,6 +143,25 @@ def test_partial_pdfs(self): def test_pickling(self): """Check pickling and unpickling of PDFCalculator.""" + # New syntax: assign an SFT instance to the property (should not warn) + dpdfc = self.dpdfc + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + dpdfc.scatteringfactortable = SFTNeutron() + self.assertFalse( + any(isinstance(x.message, DeprecationWarning) for x in w) + ) + + dpdfc.scatteringfactortable.setCustomAs("Na", "Na", 7) + spkl = pickle.dumps(dpdfc) + dpdfc1_new = pickle.loads(spkl) + self.assertEqual( + dpdfc.scatteringfactortable.type(), + dpdfc1_new.scatteringfactortable.type(), + ) + self.assertEqual(7.0, dpdfc1_new.scatteringfactortable.lookup("Na")) + + # Old syntax: call the deprecated method (should warn) dpdfc = self.dpdfc with self.assertWarns(DeprecationWarning): dpdfc.setScatteringFactorTableByType("N")