From 784692eaeaaf5fd30945749446a48107521e4fd2 Mon Sep 17 00:00:00 2001 From: Ryan Morshead Date: Sun, 16 Sep 2018 23:14:53 -0700 Subject: [PATCH] preserve class repr after `add_traits` --- traitlets/traitlets.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/traitlets/traitlets.py b/traitlets/traitlets.py index be2a8f95..feff34ed 100644 --- a/traitlets/traitlets.py +++ b/traitlets/traitlets.py @@ -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)