Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

passthrough unused args to super.__init__ #175

Merged
merged 1 commit into from
Feb 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion traitlets/tests/test_traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ class Tree(HasTraits):

tree = Tree(
value='foo',
leaves=[Tree('bar'), Tree('buzz')]
leaves=[Tree(value='bar'), Tree(value='buzz')]
)

with self.assertRaises(TraitError):
Expand Down Expand Up @@ -2163,3 +2163,19 @@ def test_subclass_add_observer():
obj.trait = 5
nt.assert_true(obj.child_called)
nt.assert_true(obj.parent_called)

def test_super_args():
class SuperRecorder(object):
def __init__(self, *args, **kwargs):
self.super_args = args
self.super_kwargs = kwargs

class SuperHasTraits(HasTraits, SuperRecorder):
i = Integer()

obj = SuperHasTraits('a1', 'a2', b=10, i=5, c='x')
nt.assert_equal(obj.i, 5)
assert not hasattr(obj, 'b')
assert not hasattr(obj, 'c')
nt.assert_equal(obj.super_args, ('a1', 'a2'))
nt.assert_equal(obj.super_kwargs, {'b': 10, 'c': 'x'})
10 changes: 8 additions & 2 deletions traitlets/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,10 +940,16 @@ def __init__(self, *args, **kwargs):
# Allow trait values to be set using keyword arguments.
# We need to use setattr for this to trigger validation and
# notifications.
super_args = args
super_kwargs = {}
with self.hold_trait_notifications():
for key, value in iteritems(kwargs):
setattr(self, key, value)
super(HasTraits, self).__init__()
if self.has_trait(key):
setattr(self, key, value)
else:
# passthrough args that don't set traits to super
super_kwargs[key] = value
super(HasTraits, self).__init__(*super_args, **super_kwargs)

def __getstate__(self):
d = self.__dict__.copy()
Expand Down