Skip to content

Commit 495c27e

Browse files
authored
Merge pull request #78 from zmx27/deprecate-setscattering
chore: deprecate setScatteringFactorTableByType
2 parents 4d757a2 + 80fa501 commit 495c27e

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

news/deprecate-setscattering.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* <news item>
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* Deprecated setScatteringFactorTableByType.
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

src/extensions/wrap_ScatteringFactorTable.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,11 @@ const char* doc_ScatteringFactorTableOwner_setScatteringFactorTableByType = "\
158158
Set internal ScatteringFactorTable according to specified string type.\n\
159159
\n\
160160
tp -- string identifier of a registered ScatteringFactorTable type.\n\
161-
Use ScatteringFactorTable.getRegisteredTypes for the allowed values.\n\
161+
Use ScatteringFactorTable.getRegisteredTypes for the allowed values.\n\
162162
\n\
163+
Deprecated: This method is deprecated and will be removed in the 2.0.0 release.\n\
164+
Use direct assignment to the `scatteringfactortable` property instead, for example:\n\
165+
obj.scatteringfactortable = SFTNeutron()\n\
163166
No return value.\n\
164167
";
165168

@@ -399,10 +402,28 @@ void wrap_ScatteringFactorTable()
399402
getsftable,
400403
setsftable<ScatteringFactorTableOwner,ScatteringFactorTable>,
401404
doc_ScatteringFactorTableOwner_scatteringfactortable)
405+
// deprecated: prefer assigning the `scatteringfactortable` property
402406
.def("setScatteringFactorTableByType",
403-
&SFTOwner::setScatteringFactorTableByType,
404-
bp::arg("tp"),
405-
doc_ScatteringFactorTableOwner_setScatteringFactorTableByType)
407+
+[](SFTOwner& obj, const std::string& tp)
408+
{
409+
namespace bp = boost::python;
410+
try
411+
{
412+
bp::object warnings = bp::import("warnings");
413+
bp::object builtins = bp::import("builtins");
414+
bp::object DeprecationWarning = builtins.attr("DeprecationWarning");
415+
warnings.attr("warn")(
416+
std::string("setScatteringFactorTableByType is deprecated; "
417+
"assign the 'scatteringfactortable' property directly, for example:\n"
418+
"obj.scatteringfactortable = SFTNeutron()"),
419+
DeprecationWarning,
420+
2);
421+
}
422+
catch (...) { /* don't let warnings break the binding */ }
423+
obj.setScatteringFactorTableByType(tp);
424+
},
425+
bp::arg("tp"),
426+
doc_ScatteringFactorTableOwner_setScatteringFactorTableByType)
406427
.def("getRadiationType",
407428
&SFTOwner::getRadiationType,
408429
return_value_policy<copy_const_reference>(),

tests/test_debyepdfcalculator.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
import pickle
77
import unittest
8+
import warnings
89

910
import numpy
1011
from testutils import _maxNormDiff, loadDiffPyStructure, pickle_with_attr
1112

1213
from diffpy.srreal.pdfcalculator import DebyePDFCalculator, PDFCalculator
14+
from diffpy.srreal.scatteringfactortable import SFTNeutron
1315

1416

1517
##############################################################################
@@ -141,8 +143,28 @@ def test_partial_pdfs(self):
141143

142144
def test_pickling(self):
143145
"""Check pickling and unpickling of PDFCalculator."""
146+
# New syntax: assign an SFT instance to the property (should not warn)
144147
dpdfc = self.dpdfc
145-
dpdfc.setScatteringFactorTableByType("N")
148+
with warnings.catch_warnings(record=True) as w:
149+
warnings.simplefilter("always")
150+
dpdfc.scatteringfactortable = SFTNeutron()
151+
self.assertFalse(
152+
any(isinstance(x.message, DeprecationWarning) for x in w)
153+
)
154+
155+
dpdfc.scatteringfactortable.setCustomAs("Na", "Na", 7)
156+
spkl = pickle.dumps(dpdfc)
157+
dpdfc1_new = pickle.loads(spkl)
158+
self.assertEqual(
159+
dpdfc.scatteringfactortable.type(),
160+
dpdfc1_new.scatteringfactortable.type(),
161+
)
162+
self.assertEqual(7.0, dpdfc1_new.scatteringfactortable.lookup("Na"))
163+
164+
# Old syntax: call the deprecated method (should warn)
165+
dpdfc = self.dpdfc
166+
with self.assertWarns(DeprecationWarning):
167+
dpdfc.setScatteringFactorTableByType("N")
146168
dpdfc.scatteringfactortable.setCustomAs("Na", "Na", 7)
147169
dpdfc.addEnvelope("sphericalshape")
148170
dpdfc.debyeprecision = 0.001

0 commit comments

Comments
 (0)