Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 90fdcaf

Browse files
jochen-ott-byCarreau
authored andcommitted
Add fallback for utils.ShimModule.__repr__
1 parent 34d45e0 commit 90fdcaf

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

IPython/utils/shimmodule.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,11 @@ def __getattr__(self, key):
7979
return import_item(name)
8080
except ImportError as e:
8181
raise AttributeError(key) from e
82+
83+
def __repr__(self):
84+
# repr on a module can be called during error handling; make sure
85+
# it does not fail, even if the import fails
86+
try:
87+
return self.__getattr__("__repr__")()
88+
except AttributeError:
89+
return f"<ShimModule for {self._mirror!r}>"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from IPython.utils.shimmodule import ShimModule
2+
import IPython
3+
4+
5+
def test_shimmodule_repr_does_not_fail_on_import_error():
6+
shim_module = ShimModule("shim_module", mirror="mirrored_module_does_not_exist")
7+
repr(shim_module)
8+
9+
10+
def test_shimmodule_repr_forwards_to_module():
11+
shim_module = ShimModule("shim_module", mirror="IPython")
12+
assert repr(shim_module) == repr(IPython)

0 commit comments

Comments
 (0)