Skip to content

Commit

Permalink
Defer ipython import (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericpre authored Jun 15, 2023
1 parent fe96376 commit e82a416
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions box/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
except ImportError:
from collections.abc import Callable, Iterable, Mapping

try:
from IPython import get_ipython
except ImportError:
ipython = False
else:
ipython = True if get_ipython() else False

import box
from box.converters import (
Expand Down Expand Up @@ -56,6 +50,17 @@
NO_NAMESPACE = object()


def _is_ipython():
try:
from IPython import get_ipython
except ImportError:
ipython = False
else:
ipython = True if get_ipython() else False

return ipython


def _exception_cause(e):
"""
Unwrap BoxKeyError and BoxValueError errors to their cause.
Expand Down Expand Up @@ -483,7 +488,7 @@ def __setstate__(self, state):
self.__dict__.update(state)

def __get_default(self, item, attr=False):
if ipython and item in ("getdoc", "shape"):
if item in ("getdoc", "shape") and _is_ipython():
return None
default_value = self._box_config["default_box_attr"]
if default_value in (self._box_config["box_class"], dict):
Expand Down

0 comments on commit e82a416

Please sign in to comment.