Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FigureWidget attribute error on wildcard import with ipywidgets not installed #2445

Merged
merged 2 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [4.7.1] - ???

### Fixed

- Fix `AttributeError: module 'plotly.graph_objs' has no attribute 'FigureWidget'` exception on `from plotly.graph_objs import *` when `ipywidgets` is not installed. Error also occurred when importing `plotly.figure_factor`. It is now possible to import `plotly.graph_objs.FigureWidget` when `ipywidgets` is not installed, and an informative `ImportError` exception will be raised in the `FigureWidget` constructor ([#2443](https://github.com/plotly/plotly.py/issues/2443), [#1111](https://github.com/plotly/plotly.py/issues/1111)).


## [4.7.0] - 2020-05-06

### Updated
Expand Down
27 changes: 16 additions & 11 deletions packages/python/plotly/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,14 @@ def perform_codegen():
optional_figure_widget_import = f"""
if sys.version_info < (3, 7):
try:
import ipywidgets
from distutils.version import LooseVersion
if LooseVersion(ipywidgets.__version__) >= LooseVersion('7.0.0'):
import ipywidgets as _ipywidgets
from distutils.version import LooseVersion as _LooseVersion
if _LooseVersion(_ipywidgets.__version__) >= _LooseVersion("7.0.0"):
from ..graph_objs._figurewidget import FigureWidget
del LooseVersion
del ipywidgets
except ImportError:
pass
else:
raise ImportError()
except Exception:
from ..missing_ipywidgets import FigureWidget
else:
__all__.append("FigureWidget")
orig_getattr = __getattr__
Expand All @@ -285,12 +285,17 @@ def __getattr__(import_name):
try:
import ipywidgets
from distutils.version import LooseVersion
if LooseVersion(ipywidgets.__version__) >= LooseVersion('7.0.0'):

if LooseVersion(ipywidgets.__version__) >= LooseVersion("7.0.0"):
from ..graph_objs._figurewidget import FigureWidget

return FigureWidget
except ImportError:
pass

else:
raise ImportError()
except Exception:
from ..missing_ipywidgets import FigureWidget
return FigureWidget

return orig_getattr(import_name)
"""
# ### __all__ ###
Expand Down
22 changes: 13 additions & 9 deletions packages/python/plotly/plotly/graph_objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,15 @@

if sys.version_info < (3, 7):
try:
import ipywidgets
from distutils.version import LooseVersion
import ipywidgets as _ipywidgets
from distutils.version import LooseVersion as _LooseVersion

if LooseVersion(ipywidgets.__version__) >= LooseVersion("7.0.0"):
if _LooseVersion(_ipywidgets.__version__) >= _LooseVersion("7.0.0"):
from ..graph_objs._figurewidget import FigureWidget
del LooseVersion
del ipywidgets
except ImportError:
pass
else:
raise ImportError()
except Exception:
from ..missing_ipywidgets import FigureWidget
else:
__all__.append("FigureWidget")
orig_getattr = __getattr__
Expand All @@ -284,7 +284,11 @@ def __getattr__(import_name):
from ..graph_objs._figurewidget import FigureWidget

return FigureWidget
except ImportError:
pass
else:
raise ImportError()
except Exception:
from ..missing_ipywidgets import FigureWidget

return FigureWidget

return orig_getattr(import_name)
22 changes: 13 additions & 9 deletions packages/python/plotly/plotly/graph_objs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,15 @@

if sys.version_info < (3, 7):
try:
import ipywidgets
from distutils.version import LooseVersion
import ipywidgets as _ipywidgets
from distutils.version import LooseVersion as _LooseVersion

if LooseVersion(ipywidgets.__version__) >= LooseVersion("7.0.0"):
if _LooseVersion(_ipywidgets.__version__) >= _LooseVersion("7.0.0"):
from ..graph_objs._figurewidget import FigureWidget
del LooseVersion
del ipywidgets
except ImportError:
pass
else:
raise ImportError()
except Exception:
from ..missing_ipywidgets import FigureWidget
else:
__all__.append("FigureWidget")
orig_getattr = __getattr__
Expand All @@ -284,7 +284,11 @@ def __getattr__(import_name):
from ..graph_objs._figurewidget import FigureWidget

return FigureWidget
except ImportError:
pass
else:
raise ImportError()
except Exception:
from ..missing_ipywidgets import FigureWidget

return FigureWidget

return orig_getattr(import_name)
15 changes: 15 additions & 0 deletions packages/python/plotly/plotly/missing_ipywidgets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from .basedatatypes import BaseFigure


class FigureWidget(BaseFigure):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why even bother subclassing BaseFigure here? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seemed safer to me for it to still pass isinstance(fig, BaseFigure) checks, but it probably doesn't make a difference.

"""
FigureWidget stand-in for use when ipywidgets is not installed. The only purpose
of this class is to provide something to import as
`plotly.graph_objs.FigureWidget` when ipywidgets is not installed. This class
simply raises an informative error message when the constructor is called
"""

def __init__(self, *args, **kwargs):
raise ImportError(
"Please install ipywidgets>=7.0.0 to use the FigureWidget class"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import pytest

# Use wildcard import to make sure FigureWidget is always included
from plotly.graph_objects import *
from plotly.missing_ipywidgets import FigureWidget as FigureWidgetMissingIPywidgets

try:
import ipywidgets as _ipywidgets
from distutils.version import LooseVersion as _LooseVersion

if _LooseVersion(_ipywidgets.__version__) >= _LooseVersion("7.0.0"):
missing_ipywidgets = False
else:
raise ImportError()
except Exception:
missing_ipywidgets = True


if missing_ipywidgets:

def test_import_figurewidget_without_ipywidgets():
assert FigureWidget is FigureWidgetMissingIPywidgets

with pytest.raises(ImportError):
# ipywidgets import error raised on construction, not import
FigureWidget()


else:

def test_import_figurewidget_with_ipywidgets():
from plotly.graph_objs._figurewidget import (
FigureWidget as FigureWidgetWithIPywidgets,
)

assert FigureWidget is FigureWidgetWithIPywidgets
fig = FigureWidget()
assert isinstance(fig, FigureWidgetWithIPywidgets)
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
import plotly.graph_objs as go
import pytest

try:
go.FigureWidget()
figure_widget_available = True
except ImportError:
figure_widget_available = False


class TestNoFrames(TestCase):
if "FigureWidget" in go.__dict__.keys():
if figure_widget_available:

def test_no_frames_in_constructor_kwarg(self):
with pytest.raises(ValueError):
Expand Down