From f1056ad031742f0a5f35fa7a2fac88279bbeff39 Mon Sep 17 00:00:00 2001 From: Jason Grout Date: Tue, 11 Apr 2017 11:38:37 -0400 Subject: [PATCH] Make the new register decorator backwards compatible, with a deprecation warning. --- docs/source/changelog.md | 5 +++++ ipywidgets/widgets/widget.py | 30 +++++++++++++++++++----------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/docs/source/changelog.md b/docs/source/changelog.md index 7e3b277b5a9..533e8172c5a 100644 --- a/docs/source/changelog.md +++ b/docs/source/changelog.md @@ -10,6 +10,11 @@ Major user-visible changes in ipywidgets 7.0 include: - The `Label` widget is now right-aligned and has no width restriction: [#1269](https://github.com/jupyter-widgets/ipywidgets/pull/1269) + +Major changes developers should be aware of include: + +- The python `@register` decorator for widget classes no longer takes a string argument, but registers a widget class using the `_model_*` and `_view_*` traits in the class. Using the decorator as `@register('name')` is deprecated and should be changed to just `@register`. [#1228](https://github.com/jupyter-widgets/ipywidgets/pull/1269) + 6.0 --- diff --git a/ipywidgets/widgets/widget.py b/ipywidgets/widgets/widget.py index bb801335786..bae3e41fd7c 100644 --- a/ipywidgets/widgets/widget.py +++ b/ipywidgets/widgets/widget.py @@ -213,17 +213,25 @@ def get(self, model_module, model_module_version, model_name, view_module, view_ widget_class = view_names[view_name] return widget_class -def register(widget): - """A decorator registering a widget class in the widget registry.""" - w = widget.class_traits() - Widget.widget_types.register(w['_model_module'].default_value, - w['_model_module_version'].default_value, - w['_model_name'].default_value, - w['_view_module'].default_value, - w['_view_module_version'].default_value, - w['_view_name'].default_value, - widget) - return widget +def register(name=''): + "For backwards compatibility, we support @register(name) syntax." + def reg(widget): + """A decorator registering a widget class in the widget registry.""" + w = widget.class_traits() + Widget.widget_types.register(w['_model_module'].default_value, + w['_model_module_version'].default_value, + w['_model_name'].default_value, + w['_view_module'].default_value, + w['_view_module_version'].default_value, + w['_view_name'].default_value, + widget) + return widget + if isinstance(name, string_types): + import warnings + warnings.warn("Widget registration using a string name has been deprecated. Widget registration now uses a plain `@register` decorator.", DeprecationWarning) + return reg + else: + return reg(name) class Widget(LoggingHasTraits): #-------------------------------------------------------------------------