Skip to content

Commit 78a7ff5

Browse files
committed
Device: raise an exception if there's unexpected arguments (issue #84)
Do not accept *args and **kwargs on the base Device class. These were unused arguments anyway. This will cause an exception to be raise if a device is constructed with arguments that are not used by any of its parent classes. This prevents the user from accidentally constructing a device with incorrect arguments because of a typo.
1 parent c0bc10d commit 78a7ff5

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

doc/examples.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Configurations
6161
from microscope.testsuite.devices import TestFilterWheel
6262
6363
DEVICES = [
64-
device(TestCamera, '127.0.0.1', 8005, {'otherargs' : 1}),
64+
device(TestCamera, '127.0.0.1', 8005, {'buffer_length' : 0}),
6565
device(TestLaser, '127.0.0.1', 8006),
6666
device(TestFilterWheel, '127.0.0.1', 8007,
6767
{'filters' : [(0, 'GFP', 525), (1, 'RFP'), (2, 'Cy5')]}),

microscope/devices.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class Device(object):
199199
"""
200200
__metaclass__ = abc.ABCMeta
201201

202-
def __init__(self, index=None, *args, **kwargs):
202+
def __init__(self, index=None):
203203
self.enabled = None
204204
# A list of settings. (Can't serialize OrderedDict, so use {}.)
205205
self.settings = OrderedDict()

microscope/testsuite/deviceserver_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def tearDown(self):
8080

8181
class TestStarting(BaseTestServeDevices):
8282
DEVICES = [
83-
device(TestCamera, '127.0.0.1', 8001, {'otherargs' : 1}),
83+
device(TestCamera, '127.0.0.1', 8001, {'buffer_length' : 0}),
8484
device(TestFilterWheel, '127.0.0.1', 8003,
8585
{'filters' : [(0, 'GFP', 525), (1, 'RFP'), (2, 'Cy5')]}),
8686
]

microscope/testsuite/test_devices.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import numpy
4343
import serial
4444

45+
import microscope.devices
4546
import microscope.testsuite.devices as dummies
4647
import microscope.testsuite.mock_devices as mocks
4748

@@ -488,5 +489,23 @@ def setUp(self):
488489
self.device = dummies.DummyDSP()
489490

490491

492+
class TestBaseDevice(unittest.TestCase):
493+
def test_unexpected_kwargs_raise_exception(self):
494+
"""Unexpected kwargs on constructor raise exception.
495+
496+
Test first that we can construct the device. Then test that
497+
it fails if there's a typo on the argument. See issue #84.
498+
"""
499+
filters = [(0, 'DAPI', '430')]
500+
dummies.TestFilterWheel(filters=filters)
501+
## XXX: Device.__del__ calls shutdown(). However, if __init__
502+
## failed the device is not complete and shutdown() fails
503+
## because the logger has not been created. See comments on
504+
## issue #69. patch __del__ to workaround this issue.
505+
with unittest.mock.patch('microscope.devices.Device.__del__'):
506+
with self.assertRaisesRegex(TypeError, "argument 'filteres'"):
507+
dummies.TestFilterWheel(filteres=filters)
508+
509+
491510
if __name__ == '__main__':
492511
unittest.main()

0 commit comments

Comments
 (0)