@@ -490,7 +490,7 @@ def get(self, obj, cls=None):
490
490
value = obj ._trait_values [self .name ]
491
491
except KeyError :
492
492
# Check for a dynamic initializer.
493
- default = cls . _trait_default_generators [ self .name ]( obj )
493
+ default = obj . trait_defaults ( self .name )
494
494
if default is Undefined :
495
495
raise TraitError ("No default value found for "
496
496
"the '%s' trait named '%s' of %r" % (
@@ -776,11 +776,6 @@ class MetaHasTraits(MetaHasDescriptors):
776
776
def setup_class (cls , classdict ):
777
777
cls ._trait_default_generators = {}
778
778
super (MetaHasTraits , cls ).setup_class (classdict )
779
- new = {}
780
- for c in reversed (cls .mro ()):
781
- if hasattr (c , "_trait_default_generators" ):
782
- new .update (c ._trait_default_generators )
783
- cls ._trait_default_generators = new
784
779
785
780
786
781
@@ -1460,6 +1455,17 @@ def trait_values(self, **metadata):
1460
1455
"""
1461
1456
return {name : getattr (self , name ) for name in self .trait_names (** metadata )}
1462
1457
1458
+ @classmethod
1459
+ def _get_trait_default_generator (cls , name ):
1460
+ """Return default generator for a given trait
1461
+
1462
+ Walk the MRO to resolve the correct default generator according to inheritance.
1463
+ """
1464
+ for c in cls .mro ():
1465
+ if name in c .__dict__ .get ('_trait_default_generators' , {}):
1466
+ return c ._trait_default_generators [name ]
1467
+ raise KeyError ("No default generator for trait %r found in %r" % (name , cls .mro ()))
1468
+
1463
1469
def trait_defaults (self , * names , ** metadata ):
1464
1470
"""Return a trait's default value or a dictionary of them
1465
1471
@@ -1468,7 +1474,7 @@ def trait_defaults(self, *names, **metadata):
1468
1474
Dynamically generated default values may
1469
1475
depend on the current state of the object."""
1470
1476
if len (names ) == 1 and len (metadata ) == 0 :
1471
- return self ._trait_default_generators [ names [0 ]] (self )
1477
+ return self ._get_trait_default_generator ( names [0 ]) (self )
1472
1478
1473
1479
for n in names :
1474
1480
if not has_trait (self , n ):
@@ -1479,7 +1485,7 @@ def trait_defaults(self, *names, **metadata):
1479
1485
1480
1486
defaults = {}
1481
1487
for n in trait_names :
1482
- defaults [n ] = self ._trait_default_generators [ n ] (self )
1488
+ defaults [n ] = self ._get_trait_default_generator ( n ) (self )
1483
1489
return defaults
1484
1490
1485
1491
def trait_names (self , ** metadata ):
0 commit comments