Skip to content

Commit

Permalink
Merge pull request #501 from ipython/rmorshea-add_traits-repr
Browse files Browse the repository at this point in the history
Preserve Class Repr After `add_traits`
  • Loading branch information
minrk authored Apr 5, 2019
2 parents c3abe9a + 784692e commit 7b48947
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions traitlets/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1399,8 +1399,13 @@ def _register_validator(self, handler, names):

def add_traits(self, **traits):
"""Dynamically add trait attributes to the HasTraits instance."""
self.__class__ = type(self.__class__.__name__, (self.__class__,),
traits)
cls = self.__class__
attrs = {"__module__": cls.__module__}
if hasattr(cls, "__qualname__"):
# __qualname__ introduced in Python 3.3 (see PEP 3155)
attrs["__qualname__"] = cls.__qualname__
attrs.update(traits)
self.__class__ = type(cls.__name__, (cls,), attrs)
for trait in traits.values():
trait.instance_init(self)

Expand Down

0 comments on commit 7b48947

Please sign in to comment.