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_objects
modules regardless of whetheripywidgets
is installed. But whenFigureWidget
is lazily imported, an exception is raised if a supported version ofipywidgets
is 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
ipywidgets
whenplotly.graph_objs
is imported and only addFigureWidget
to__all__
if it is found. But this results in a performance hit on every import ofplotly.graph_objs
, whether or notFigureWidget
is used.Another option would be to always allow
FigureWidget
to be imported, but raise an exception when a user tries to construct aFigureWidget
ifipywidgets
is not found. Unfortunately, this isn't possible becauseipywidgets
is required in order to define theFigureWidget
class itself so we can't import it withoutipywidgets
.Instead, this PR introduces a new
plotly.missing_ipywidgets.FigureWidget
class. 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 installipywidgets
in order to useFigureWidget
. With this approach,"FigureWidget"
is still always added to__all__
, but on lazy import we check whetheripywidgets
is installed. If it is, the currentplotly.graph_objs._figurewidget.FigureWidget
class is returned, otherwise the newplotly.missing_ipywidgets.FigureWidget
class 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 thecodegen
files and not generated files.modified existing tests.