Skip to content

Commit

Permalink
Merge pull request #22 from SylvainCorlay/signals
Browse files Browse the repository at this point in the history
Create BaseDescriptor parent class to TraitType and add obj argument to instance_init
  • Loading branch information
minrk committed Jun 2, 2015
2 parents 605133a + 1daaacd commit ca01cd3
Showing 1 changed file with 49 additions and 41 deletions.
90 changes: 49 additions & 41 deletions traitlets/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,28 +286,48 @@ def unlink(self):


#-----------------------------------------------------------------------------
# Base TraitType for all traits
# Base Descriptor Class
#-----------------------------------------------------------------------------


class TraitType(object):
"""A base class for all trait descriptors.
class BaseDescriptor(object):
"""Base descriptor class
Notes
-----
Our implementation of traits is based on Python's descriptor
prototol. This class is the base class for all such descriptors. The
This implements Python's descriptor prototol.
This class is the base class for all such descriptors. The
only magic we use is a custom metaclass for the main :class:`HasTraits`
class that does the following:
1. Sets the :attr:`name` attribute of every :class:`TraitType`
1. Sets the :attr:`name` attribute of every :class:`BaseDescriptor`
instance in the class dict to the name of the attribute.
2. Sets the :attr:`this_class` attribute of every :class:`TraitType`
2. Sets the :attr:`this_class` attribute of every :class:`BaseDescriptor`
instance in the class dict to the *class* that declared the trait.
This is used by the :class:`This` trait to allow subclasses to
accept superclasses for :class:`This` values.
"""

name = None
this_class = None

def instance_init(self, obj):
"""Part of the initialization which may depend on the underlying
HasTraits instance.
It is typically overloaded for specific types.
This method is called by :meth:`HasTraits.__new__` and in the
:meth:`BaseDescriptor.instance_init` method of descriptors holding
other descriptors.
"""
pass


class TraitType(BaseDescriptor):
"""A base class for all trait types.
"""

metadata = {}
default_value = Undefined
allow_none = False
Expand Down Expand Up @@ -354,18 +374,6 @@ def get_default_value(self):
"""Create a new instance of the default value."""
return self.default_value

def instance_init(self):
"""Part of the initialization which may depends on the underlying
HasTraits instance.
It is typically overloaded for specific trait types.
This method is called by :meth:`HasTraits.__new__` and in the
:meth:`TraitType.instance_init` method of trait types holding
other trait types.
"""
pass

def init_default_value(self, obj):
"""Instantiate the default value for the trait type.
Expand Down Expand Up @@ -531,7 +539,7 @@ def __new__(mcls, name, bases, classdict):
# print "MetaHasTraitlets (bases): ", bases
# print "MetaHasTraitlets (classdict): ", classdict
for k,v in iteritems(classdict):
if isinstance(v, TraitType):
if isinstance(v, BaseDescriptor):
v.name = k
elif inspect.isclass(v):
if issubclass(v, TraitType):
Expand Down Expand Up @@ -579,9 +587,9 @@ def __new__(cls, *args, **kw):
except AttributeError:
pass
else:
if isinstance(value, TraitType):
value.instance_init()
if key not in kw:
if isinstance(value, BaseDescriptor):
value.instance_init(inst)
if isinstance(value, TraitType) and key not in kw:
value._set_default_value_at_instance_init(inst)
inst._cross_validation_lock = False
return inst
Expand Down Expand Up @@ -970,9 +978,9 @@ def info(self):
return result + ' or None'
return result

def instance_init(self):
def instance_init(self, obj):
self._resolve_classes()
super(Type, self).instance_init()
super(Type, self).instance_init(obj)

def _resolve_classes(self):
if isinstance(self.klass, py3compat.string_types):
Expand Down Expand Up @@ -1078,9 +1086,9 @@ def info(self):

return result

def instance_init(self):
def instance_init(self, obj):
self._resolve_classes()
super(Instance, self).instance_init()
super(Instance, self).instance_init(obj)

def _resolve_classes(self):
if isinstance(self.klass, py3compat.string_types):
Expand Down Expand Up @@ -1175,12 +1183,12 @@ def __init__(self, trait_types, **metadata):
self.default_value = self.trait_types[0].get_default_value()
super(Union, self).__init__(**metadata)

def instance_init(self):
def instance_init(self, obj):
for trait_type in self.trait_types:
trait_type.name = self.name
trait_type.this_class = self.this_class
trait_type.instance_init()
super(Union, self).instance_init()
trait_type.instance_init(obj)
super(Union, self).instance_init(obj)

def validate(self, obj, value):
for trait_type in self.trait_types:
Expand Down Expand Up @@ -1562,11 +1570,11 @@ def validate_elements(self, obj, value):
validated.append(v)
return self.klass(validated)

def instance_init(self):
def instance_init(self, obj):
if isinstance(self._trait, TraitType):
self._trait.this_class = self.this_class
self._trait.instance_init()
super(Container, self).instance_init()
self._trait.instance_init(obj)
super(Container, self).instance_init(obj)


class List(Container):
Expand Down Expand Up @@ -1748,12 +1756,12 @@ def validate_elements(self, obj, value):
validated.append(v)
return tuple(validated)

def instance_init(self):
def instance_init(self, obj):
for trait in self._traits:
if isinstance(trait, TraitType):
trait.this_class = self.this_class
trait.instance_init()
super(Container, self).instance_init()
trait.instance_init(obj)
super(Container, self).instance_init(obj)


class Dict(Instance):
Expand Down Expand Up @@ -1847,15 +1855,15 @@ def validate_elements(self, obj, value):
validated[key] = v
return self.klass(validated)

def instance_init(self):
def instance_init(self, obj):
if isinstance(self._trait, TraitType):
self._trait.this_class = self.this_class
self._trait.instance_init()
self._trait.instance_init(obj)
if self._traits is not None:
for trait in self._traits.values():
trait.this_class = self.this_class
trait.instance_init()
super(Dict, self).instance_init()
trait.instance_init(obj)
super(Dict, self).instance_init(obj)


class TCPAddress(TraitType):
Expand Down

0 comments on commit ca01cd3

Please sign in to comment.