@@ -715,19 +715,21 @@ def test_is_jupyter_notebook_detection():
715715 assert isinstance (result , bool )
716716
717717 # Test when IPython is available and returns ZMQInteractiveShell
718+ # Store the original import before patching to avoid recursion
719+ original_import = __import__
720+
718721 mock_ipython = MagicMock ()
719722 mock_ipython .__class__ .__name__ = "ZMQInteractiveShell"
720-
723+
721724 # Mock the import inside the function
722725 def mock_import (name , globals = None , locals = None , fromlist = (), level = 0 ):
723726 if name == "IPython" :
724727 mock_ipython_module = MagicMock ()
725728 mock_ipython_module .get_ipython = MagicMock (return_value = mock_ipython )
726729 return mock_ipython_module
727730 # For other imports, use real import
728- import builtins
729- return builtins .__import__ (name , globals , locals , fromlist , level )
730-
731+ return original_import (name , globals , locals , fromlist , level )
732+
731733 with patch ("builtins.__import__" , side_effect = mock_import ):
732734 # Re-import to get fresh function that will use the mocked import
733735 import importlib
@@ -739,16 +741,15 @@ def mock_import(name, globals=None, locals=None, fromlist=(), level=0):
739741 # Test when IPython is available and returns TerminalInteractiveShell
740742 mock_ipython = MagicMock ()
741743 mock_ipython .__class__ .__name__ = "TerminalInteractiveShell"
742-
744+
743745 def mock_import2 (name , globals = None , locals = None , fromlist = (), level = 0 ):
744746 if name == "IPython" :
745747 mock_ipython_module = MagicMock ()
746748 mock_ipython_module .get_ipython = MagicMock (return_value = mock_ipython )
747749 return mock_ipython_module
748750 # For other imports, use real import
749- import builtins
750- return builtins .__import__ (name , globals , locals , fromlist , level )
751-
751+ return original_import (name , globals , locals , fromlist , level )
752+
752753 with patch ("builtins.__import__" , side_effect = mock_import2 ):
753754 import importlib
754755 import mcp .client .stdio
0 commit comments