Skip to content

Commit ee5a17f

Browse files
committed
Simplify deferred creation of default values
1 parent 02d1ccc commit ee5a17f

File tree

2 files changed

+15
-56
lines changed

2 files changed

+15
-56
lines changed

traitlets/tests/test_traitlets.py

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ class A(HasTraits):
8484
a = A()
8585
self.assertEqual(a.tt, 10)
8686

87-
# Defaults are validated when the HasTraits is instantiated
88-
class B(HasTraits):
89-
tt = MyIntTT('bad default')
90-
self.assertRaises(TraitError, B)
91-
9287
def test_info(self):
9388
class A(HasTraits):
9489
tt = TraitType
@@ -118,7 +113,6 @@ def _x_default(self):
118113
self.assertEqual(a.x, 11)
119114
self.assertEqual(a._trait_values, {'x': 11})
120115
b = B()
121-
self.assertEqual(b._trait_values, {'x': 20})
122116
self.assertEqual(list(a._trait_dyn_inits.keys()), ['x'])
123117
self.assertEqual(b.x, 20)
124118
c = C()
@@ -200,9 +194,9 @@ class A(HasTraits):
200194
a = A()
201195
a.on_trait_change(self.notify1)
202196
a.a = 0
203-
self.assertEqual(len(self._notify1),0)
197+
self.assertEqual(len(self._notify1), 1)
204198
a.b = 0.0
205-
self.assertEqual(len(self._notify1),0)
199+
self.assertEqual(len(self._notify1), 2)
206200
a.a = 10
207201
self.assertTrue(('a',0,10) in self._notify1)
208202
a.b = 10.0
@@ -213,7 +207,7 @@ class A(HasTraits):
213207
a.on_trait_change(self.notify1,remove=True)
214208
a.a = 20
215209
a.b = 20.0
216-
self.assertEqual(len(self._notify1),0)
210+
self.assertEqual(len(self._notify1), 0)
217211

218212
def test_notify_one(self):
219213

@@ -224,7 +218,7 @@ class A(HasTraits):
224218
a = A()
225219
a.on_trait_change(self.notify1, 'a')
226220
a.a = 0
227-
self.assertEqual(len(self._notify1),0)
221+
self.assertEqual(len(self._notify1), 1)
228222
a.a = 10
229223
self.assertTrue(('a',0,10) in self._notify1)
230224
self.assertRaises(TraitError,setattr,a,'a','bad string')
@@ -258,8 +252,8 @@ class B(A):
258252
b.on_trait_change(self.notify2, 'b')
259253
b.a = 0
260254
b.b = 0.0
261-
self.assertEqual(len(self._notify1),0)
262-
self.assertEqual(len(self._notify2),0)
255+
self.assertEqual(len(self._notify1), 1)
256+
self.assertEqual(len(self._notify2), 1)
263257
b.a = 10
264258
b.b = 10.0
265259
self.assertTrue(('a',0,10) in self._notify1)
@@ -276,7 +270,7 @@ def _a_changed(self, name, old, new):
276270
a = A()
277271
a.a = 0
278272
# This is broken!!!
279-
self.assertEqual(len(a._notify1),0)
273+
self.assertEqual(len(a._notify1), 1)
280274
a.a = 10
281275
self.assertTrue(('a',0,10) in a._notify1)
282276

@@ -290,7 +284,7 @@ def _b_changed(self, name, old, new):
290284
b.a = 10
291285
b.b = 10.0
292286
self.assertTrue(('a',0,10) in b._notify1)
293-
self.assertTrue(('b',0.0,10.0) in b._notify2)
287+
self.assertTrue(('b',Undefined,10.0) in b._notify2)
294288

295289
def test_notify_args(self):
296290

@@ -500,11 +494,6 @@ class A(HasTraits):
500494

501495
self.assertRaises(ImportError, A)
502496

503-
class C(HasTraits):
504-
klass = Type(None, B)
505-
506-
self.assertRaises(TraitError, C)
507-
508497
def test_str_klass(self):
509498

510499
class A(HasTraits):
@@ -600,23 +589,6 @@ class C(HasTraits):
600589
c = C()
601590
self.assertTrue(c.inst is None)
602591

603-
def test_bad_default(self):
604-
class Foo(object): pass
605-
606-
class A(HasTraits):
607-
inst = Instance(Foo)
608-
609-
self.assertRaises(TraitError, A)
610-
611-
def test_instance(self):
612-
class Foo(object): pass
613-
614-
def inner():
615-
class A(HasTraits):
616-
inst = Instance(Foo())
617-
618-
self.assertRaises(TraitError, inner)
619-
620592

621593
class TestThis(TestCase):
622594

@@ -1368,7 +1340,7 @@ def _b_validate(self, value, trait):
13681340
nt.assert_equal(t.a, 4)
13691341
nt.assert_equal(changes, [])
13701342

1371-
nt.assert_equal(changes, [(0, 4)])
1343+
nt.assert_equal(changes, [(Undefined, 4)])
13721344
# Test roll-back
13731345
try:
13741346
with t.hold_trait_notifications():

traitlets/traitlets.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,9 @@ def __init__(self, default_value=NoDefaultSpecified, allow_none=None, **metadata
365365
else:
366366
self._metadata = self.metadata
367367

368-
self.init()
369-
370-
def init(self):
371-
pass
368+
def instance_init(self, obj):
369+
self._setup_dynamic_initializer(obj)
370+
super(TraitType, self).instance_init(obj)
372371

373372
def get_default_value(self):
374373
"""Create a new instance of the default value."""
@@ -390,22 +389,12 @@ def _setup_dynamic_initializer(self, obj):
390389
# trait declaration or above.
391390
mro = type(obj).mro()
392391
meth_name = '_%s_default' % self.name
393-
for cls in mro[:mro.index(self.this_class)+1]:
392+
for cls in mro[:mro.index(self.this_class) + 1]:
394393
if meth_name in cls.__dict__:
395394
break
396395
else:
397-
return False
398-
# Complete the dynamic initialization.
396+
return
399397
obj._trait_dyn_inits[self.name] = meth_name
400-
return True
401-
402-
def _set_default_value_at_instance_init(self, obj):
403-
# As above, but if no default was specified, don't try to set it.
404-
# If the trait is accessed before it is given a value, init_default_value
405-
# will be called at that point.
406-
if (not self._setup_dynamic_initializer(obj)) \
407-
and (self.default_value is not Undefined):
408-
self.init_default_value(obj)
409398

410399
def __get__(self, obj, cls=None):
411400
"""Get the value of the trait by self.name for the instance.
@@ -569,8 +558,6 @@ def __new__(cls, *args, **kw):
569558
else:
570559
if isinstance(value, BaseDescriptor):
571560
value.instance_init(inst)
572-
if isinstance(value, TraitType) and key not in kw:
573-
value._set_default_value_at_instance_init(inst)
574561
inst._cross_validation_lock = False
575562
return inst
576563

@@ -1142,7 +1129,7 @@ class Union(TraitType):
11421129
"""A trait type representing a Union type."""
11431130

11441131
def __init__(self, trait_types, **metadata):
1145-
"""Construct a Union trait.
1132+
"""Construct a Union trait.
11461133
11471134
This trait allows values that are allowed by at least one of the
11481135
specified trait types. A Union traitlet cannot have metadata on

0 commit comments

Comments
 (0)