Skip to content

Commit ce4aab0

Browse files
committed
Fix is_interactive() method
1 parent d8645f1 commit ce4aab0

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

google/cloud/dataproc_spark_connect/environment.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,15 @@ def is_jetbrains_ide() -> bool:
4848

4949

5050
def is_interactive():
51-
return hasattr(sys, "ps1")
51+
try:
52+
from IPython import get_ipython
53+
54+
if get_ipython() is not None:
55+
return True
56+
except ImportError:
57+
pass
58+
59+
return hasattr(sys, "ps1") or sys.flags.interactive
5260

5361

5462
def is_terminal():

tests/unit/test_environment.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,42 @@ def test_get_client_environment_label_precedence(
222222
"colab-enterprise",
223223
)
224224

225+
@mock.patch("IPython.get_ipython", return_value=mock.MagicMock())
226+
def test_is_interactive_ipython_true(self, mock_get_ipython):
227+
self.assertTrue(environment.is_interactive())
228+
229+
@mock.patch("IPython.get_ipython", return_value=None)
230+
@mock.patch("google.cloud.dataproc_spark_connect.environment.sys")
231+
def test_is_interactive_ipython_false(self, mock_sys, mock_get_ipython):
232+
if hasattr(mock_sys, "ps1"):
233+
del mock_sys.ps1
234+
mock_sys.flags.interactive = 0
235+
self.assertFalse(environment.is_interactive())
236+
237+
@mock.patch("IPython.get_ipython", side_effect=ImportError)
225238
@mock.patch("google.cloud.dataproc_spark_connect.environment.sys")
226-
def test_is_interactive_true(self, mock_sys):
239+
def test_is_interactive_true_via_ps1(self, mock_sys, mock_get_ipython):
227240
# Simulate interactive environment by setting ps1
228241
mock_sys.ps1 = ">>>"
242+
mock_sys.flags.interactive = 0
229243
self.assertTrue(environment.is_interactive())
230244

245+
@mock.patch("IPython.get_ipython", side_effect=ImportError)
231246
@mock.patch("google.cloud.dataproc_spark_connect.environment.sys")
232-
def test_is_interactive_false(self, mock_sys):
233-
# Simulate non-interactive environment by removing ps1
247+
def test_is_interactive_true_via_flags(self, mock_sys, mock_get_ipython):
248+
# Simulate interactive environment via sys.flags.interactive
234249
if hasattr(mock_sys, "ps1"):
235250
del mock_sys.ps1
251+
mock_sys.flags.interactive = 1
252+
self.assertTrue(environment.is_interactive())
253+
254+
@mock.patch("IPython.get_ipython", side_effect=ImportError)
255+
@mock.patch("google.cloud.dataproc_spark_connect.environment.sys")
256+
def test_is_interactive_false(self, mock_sys, mock_get_ipython):
257+
# Simulate non-interactive environment
258+
if hasattr(mock_sys, "ps1"):
259+
del mock_sys.ps1
260+
mock_sys.flags.interactive = 0
236261
self.assertFalse(environment.is_interactive())
237262

238263
@mock.patch("sys.stdin")
@@ -254,9 +279,13 @@ def test_is_interactive_terminal_true(self, mock_sys, mock_stdin):
254279

255280
@mock.patch("sys.stdin")
256281
@mock.patch("google.cloud.dataproc_spark_connect.environment.sys")
257-
def test_is_interactive_terminal_true(self, mock_sys, mock_stdin):
282+
@mock.patch("IPython.get_ipython", side_effect=ImportError)
283+
def test_is_interactive_terminal_false(
284+
self, mock_get_ipython, mock_sys, mock_stdin
285+
):
258286
if hasattr(mock_sys, "ps1"):
259287
del mock_sys.ps1
288+
mock_sys.flags.interactive = 0
260289
mock_stdin.isatty.return_value = False
261290
self.assertFalse(environment.is_interactive_terminal())
262291

0 commit comments

Comments
 (0)