From 14964a7041684122124c44959cc066926d48f012 Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Sat, 3 Jun 2023 11:26:39 +0100 Subject: [PATCH] Defer ipython import --- box/box.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/box/box.py b/box/box.py index 322b313..a6b2222 100644 --- a/box/box.py +++ b/box/box.py @@ -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 ( @@ -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. @@ -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):