Skip to content

Commit 7b28cc8

Browse files
committed
Raise TypeError on invalid init or call argument.
Do not apply keyword arguments unless they are all valid.
1 parent 2e1e8ff commit 7b28cc8

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

Diff for: diffpy/srreal/tests/testbondcalculator.py

+7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def test___init__(self):
3737
self.assertEqual(0, self.bdc.rmin)
3838
self.assertEqual(5, self.bdc.rmax)
3939
self.assertEqual(0, len(self.bdc.distances))
40+
bdc1 = BondCalculator(rmin=2, rmax=7)
41+
self.assertEqual(2, bdc1.rmin)
42+
self.assertEqual(7, bdc1.rmax)
43+
self.assertRaises(TypeError, BondCalculator, invalid=55)
4044
return
4145

4246

@@ -51,6 +55,9 @@ def test___call__(self):
5155
self.assertEqual(0, len(bdc(self.niprim)))
5256
bdc.rmax = 2.5
5357
self.assertEqual(12, len(bdc(self.niprim)))
58+
self.assertEqual(0, len(bdc(self.niprim, rmax=2)))
59+
self.assertRaises(TypeError, bdc, self.niprim, rmax=5, invalid=7)
60+
self.assertEqual(2, bdc.rmax)
5461
return
5562

5663

Diff for: diffpy/srreal/wraputils.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,20 @@ def fset(self, value):
4040

4141

4242
def setattrFromKeywordArguments(obj, **kwargs):
43-
'''Set attributes of the obj according to keywork arguments.
43+
'''Set attributes of the obj according to keyword arguments.
4444
For example: setattrFromKeywordArguments(obj, qmax=24, scale=2)
4545
This is a shared helper function used by __init__ and __call__.
4646
4747
kwargs -- one or more keyword arguments
4848
4949
No return value.
50-
Raise ValueError for invalid keyword argument.
50+
Raise TypeError for invalid keyword argument.
5151
'''
52-
for n, v in kwargs.iteritems():
52+
for n in kwargs:
5353
if not hasattr(obj, n):
54-
emsg = "Unknown attribute %r" % n
55-
raise ValueError(emsg)
54+
emsg = "Invalid keyword argument %r" % n
55+
raise TypeError(emsg)
56+
for n, v in kwargs.iteritems():
5657
setattr(obj, n, v)
5758
return
5859

0 commit comments

Comments
 (0)