def add_trait(self, name, trait, refresh=True):
        """Overrides HasTraits definition of *add_trait* in order to
        keep track of dynamically added traits for serialization.
        """
        # When a trait with sub-traits is added (like a List or Dict),
        # HasTraits calls add_trait AGAIN for the sub-trait, so we
        # just detect it here and pass it through
        if name.endswith('_items') and trait.type == 'event':
            super(Container, self).add_trait(name, trait)
            return

        # Try to keep from clobbering framework stuff.
        bases = [self.__class__]
        bases.extend(self.__class__.__bases__)
        for base in bases:
            if name in base.__dict__:
                raise NameError('Would override attribute %r of %s'
                                % (name, base.__name__))

        _check_bad_default(name, trait, self)

        #FIXME: saving our own list of added traits shouldn't be necessary...
        if name not in self._added_traits:
            self._added_traits[name] = trait
        super(Container, self).add_trait(name, trait)
        if self._cached_traits_ is not None:
            self._cached_traits_[name] = self.trait(name)

        if name in self._trait_metadata:
            del self._trait_metadata[name]  # Invalidate.

        if refresh:
            getattr(self, name)  # For VariableTree subtree/leaf update in GUI.
dataset/ETHPy150Open OpenMDAO/OpenMDAO-Framework/openmdao.main/src/openmdao/main/container.py/Container.add_trait/original