From 90fdcaf84e2971cd46cd210b7d596762486167bf 2022-01-17 20:15:57 From: Jochen Ott Date: 2022-01-17 20:15:57 Subject: [PATCH] Add fallback for utils.ShimModule.__repr__ --- diff --git a/IPython/utils/shimmodule.py b/IPython/utils/shimmodule.py index ec243a0..8af44ca 100644 --- a/IPython/utils/shimmodule.py +++ b/IPython/utils/shimmodule.py @@ -79,3 +79,11 @@ class ShimModule(types.ModuleType): return import_item(name) except ImportError as e: raise AttributeError(key) from e + + def __repr__(self): + # repr on a module can be called during error handling; make sure + # it does not fail, even if the import fails + try: + return self.__getattr__("__repr__")() + except AttributeError: + return f"" diff --git a/IPython/utils/tests/test_shimmodule.py b/IPython/utils/tests/test_shimmodule.py new file mode 100644 index 0000000..6ea2629 --- /dev/null +++ b/IPython/utils/tests/test_shimmodule.py @@ -0,0 +1,12 @@ +from IPython.utils.shimmodule import ShimModule +import IPython + + +def test_shimmodule_repr_does_not_fail_on_import_error(): + shim_module = ShimModule("shim_module", mirror="mirrored_module_does_not_exist") + repr(shim_module) + + +def test_shimmodule_repr_forwards_to_module(): + shim_module = ShimModule("shim_module", mirror="IPython") + assert repr(shim_module) == repr(IPython)