Skip to content

Commit

Permalink
feat: drop use of sys.version_info and TYPE_CHECKING
Browse files Browse the repository at this point in the history
Removing the check for Python < 3.7 using `sys.version_info` and as a
backup checking `typing.TYPE_CHECKING`; this saves us a little space
and also cleans up the code. Proposing this as an enhancement beyond
what's in the `codegen2` branch / PR #4978.
  • Loading branch information
gvwilson committed Jan 24, 2025
1 parent 2b3d47b commit 98a2985
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 38 deletions.
44 changes: 16 additions & 28 deletions packages/python/plotly/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,36 +267,24 @@ def perform_codegen(reformat=True):
root_datatype_imports.append(f"._deprecations.{dep_clas}")

optional_figure_widget_import = f"""
if sys.version_info < (3, 7) or TYPE_CHECKING:
try:
import ipywidgets as _ipywidgets
from packaging.version import Version as _Version
if _Version(_ipywidgets.__version__) >= _Version("7.0.0"):
from ..graph_objs._figurewidget import FigureWidget
else:
raise ImportError()
except Exception:
from ..missing_anywidget import FigureWidget
else:
__all__.append("FigureWidget")
orig_getattr = __getattr__
def __getattr__(import_name):
if import_name == "FigureWidget":
try:
import ipywidgets
from packaging.version import Version
if Version(ipywidgets.__version__) >= Version("7.0.0"):
from ..graph_objs._figurewidget import FigureWidget
return FigureWidget
else:
raise ImportError()
except Exception:
from ..missing_anywidget import FigureWidget
__all__.append("FigureWidget")
orig_getattr = __getattr__
def __getattr__(import_name):
if import_name == "FigureWidget":
try:
import ipywidgets
from packaging.version import Version
if Version(ipywidgets.__version__) >= Version("7.0.0"):
from ..graph_objs._figurewidget import FigureWidget
return FigureWidget
else:
raise ImportError()
except Exception:
from ..missing_anywidget import FigureWidget
return FigureWidget
return orig_getattr(import_name)
return orig_getattr(import_name)
"""
# ### __all__ ###
for path_parts, class_names in alls.items():
Expand Down
16 changes: 6 additions & 10 deletions packages/python/plotly/codegen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,12 @@ def build_from_imports_py(rel_modules=(), rel_classes=(), init_extra=""):

result = f"""\
import sys
from typing import TYPE_CHECKING
if sys.version_info < (3, 7) or TYPE_CHECKING:
{imports_str}
else:
from _plotly_utils.importers import relative_import
__all__, __getattr__, __dir__ = relative_import(
__name__,
{repr(rel_modules)},
{repr(rel_classes)}
)
from _plotly_utils.importers import relative_import
__all__, __getattr__, __dir__ = relative_import(
__name__,
{repr(rel_modules)},
{repr(rel_classes)}
)
{init_extra}
"""
Expand Down

0 comments on commit 98a2985

Please sign in to comment.