Skip to content

Commit

Permalink
v0.4.0 - allowing a Params(*args, **kwargs) construction with overrid…
Browse files Browse the repository at this point in the history
…ing class defaults with args and kwargs.
  • Loading branch information
kpe committed Mar 22, 2019
1 parent c0992ba commit 0200b49
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion params/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

from .params import Params

__version__ = '0.3.1'
__version__ = '0.4.0'
3 changes: 2 additions & 1 deletion params/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ class MyParams(Params):
params = MyParams(another_param=2.0) # raises ValueError
"""

def __init__(self, **kwargs):
def __init__(self, *args, **kwargs):
super(Params, self).__init__()
self.update(self.__class__.defaults())
self.update(dict(*args))
self.update(kwargs)

def __getattribute__(self, attr):
Expand Down
10 changes: 10 additions & 0 deletions tests/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ def test_from_dict(self):
self.assertEqual({'param_a': 3, 'param_b': 4}, params)
self.assertIsInstance(params, dict)

def test_construct_dict(self):
params = MyParams(param_a=99)
self.assertEqual(99, params.param_a)
params = MyParams(params, param_b=101)
self.assertEqual(99, params.param_a)
self.assertEqual(101, params.param_b)
params = MyParams(params, param_a=101)
self.assertEqual(101, params.param_a)
self.assertEqual(101, params.param_b)


if __name__ == '__main__':
unittest.main()

0 comments on commit 0200b49

Please sign in to comment.