Fix FigureWidget attribute error on wildcard import with ipywidgets not installed #2445
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes the regression in 4.7.0 reported in #2443. Also takes care of #1111 by producing a more informative error message.
The problem is that with Python 3.7+, we add
"FigureWidget"to the__all__list in theplotly.graph_objs/plotly.graph_objectsmodules regardless of whetheripywidgetsis installed. But whenFigureWidgetis lazily imported, an exception is raised if a supported version ofipywidgetsis not installed. This results in the reported error when a user, or library, callsfrom plotly.graph_objs import *.One option would be to check for the proper version of
ipywidgetswhenplotly.graph_objsis imported and only addFigureWidgetto__all__if it is found. But this results in a performance hit on every import ofplotly.graph_objs, whether or notFigureWidgetis used.Another option would be to always allow
FigureWidgetto be imported, but raise an exception when a user tries to construct aFigureWidgetifipywidgetsis not found. Unfortunately, this isn't possible becauseipywidgetsis required in order to define theFigureWidgetclass itself so we can't import it withoutipywidgets.Instead, this PR introduces a new
plotly.missing_ipywidgets.FigureWidgetclass. This class is a subclass ofBaseFigure(just like the standardFigure), but all it does is raise an informative exception in the constructor informing the user that they need to installipywidgetsin order to useFigureWidget. With this approach,"FigureWidget"is still always added to__all__, but on lazy import we check whetheripywidgetsis installed. If it is, the currentplotly.graph_objs._figurewidget.FigureWidgetclass is returned, otherwise the newplotly.missing_ipywidgets.FigureWidgetclass is returned.Tests added in
plotly/tests/test_core/test_figure_widget_backend/test_missing_ipywigets.py.Code PR
plotly.graph_objects, my modifications concern thecodegenfiles and not generated files.modified existing tests.