Skip to content

Commit

Permalink
Merge pull request #1276 from jasongrout/register-deprecate
Browse files Browse the repository at this point in the history
Make @register backwards-compatible
  • Loading branch information
jasongrout authored Apr 11, 2017
2 parents c9d8088 + 46e6e35 commit 099d3d0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
5 changes: 5 additions & 0 deletions docs/source/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/1228), [#1276](https://github.com/jupyter-widgets/ipywidgets/pull/1276)

6.0
---

Expand Down
30 changes: 19 additions & 11 deletions ipywidgets/widgets/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
#-------------------------------------------------------------------------
Expand Down

0 comments on commit 099d3d0

Please sign in to comment.