Skip to content

Commit 1299434

Browse files
author
Roja Reddy Sareddy
committed
fix(core): make rich console and traceback opt-in
Importing the SDK ran enable_textual_rich_console_and_traceback() at module import (sagemaker/core/__init__.py, and again via the module-level logger in utils.py), which called rich.traceback.install() -- overriding sys.excepthook -- and reconfigured the process-global rich console. Users who did not want rich tracebacks/console styling had no way to turn it off, and it changed traceback behavior for the whole process just by importing sagemaker. Gate those process-wide side effects behind an opt-in: they now run only when SAGEMAKER_ENABLE_RICH_LOGGING is set (1/true/yes/on) or a caller passes force=True. Default import no longer touches sys.excepthook or the global console. Add is_rich_logging_enabled() and unit tests covering the default no-op, env opt-in, force, and idempotency. Closes #5179 Closes #5561
1 parent 3f984d7 commit 1299434

3 files changed

Lines changed: 128 additions & 3 deletions

File tree

‎sagemaker-core/src/sagemaker/core/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from sagemaker.core.utils.utils import enable_textual_rich_console_and_traceback
44
from sagemaker.core.deprecations import register_removed_module_finder
55

6-
enable_textual_rich_console_and_traceback()
6+
enable_textual_rich_console_and_traceback() # opt-in; no-op unless SAGEMAKER_ENABLE_RICH_LOGGING is set
77

88
# Install the meta-path finder that gives actionable migration guidance for v2
99
# modules removed in v3. sagemaker-core is the universal dependency of every v3

‎sagemaker-core/src/sagemaker/core/utils/utils.py‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,49 @@ def get_textual_rich_theme() -> Theme:
137137
)
138138

139139

140+
RICH_LOGGING_OPT_IN_ENV_VAR = "SAGEMAKER_ENABLE_RICH_LOGGING"
141+
142+
_TRUTHY_ENV_VALUES = frozenset({"1", "true", "yes", "on"})
143+
144+
145+
def is_rich_logging_enabled() -> bool:
146+
"""Whether the user opted in to sagemaker-core's rich console and tracebacks.
147+
148+
Reconfiguring the global rich console and calling ``rich.traceback.install()``
149+
override ``sys.excepthook`` and restyle the process-global console, so they are
150+
opt-in: merely importing the SDK must not change tracebacks or console styling.
151+
Enable by setting the ``SAGEMAKER_ENABLE_RICH_LOGGING`` environment variable to
152+
one of ``1``/``true``/``yes``/``on`` (case-insensitive).
153+
154+
Returns:
155+
bool: True if rich console/traceback output has been opted into.
156+
"""
157+
return (
158+
os.environ.get(RICH_LOGGING_OPT_IN_ENV_VAR, "").strip().lower() in _TRUTHY_ENV_VALUES
159+
)
160+
161+
140162
textual_rich_console_and_traceback_enabled = False
141163

142164

143-
def enable_textual_rich_console_and_traceback():
144-
"""Reconfigure the global textual rich console with the customized theme and enable textual rich error traceback"""
165+
def enable_textual_rich_console_and_traceback(force: bool = False):
166+
"""Reconfigure the global rich console and install rich error tracebacks.
167+
168+
This overrides ``sys.excepthook`` (via ``rich.traceback.install``) and restyles
169+
the process-global rich console. Because those are process-wide side effects, it
170+
is opt-in and a no-op unless the user opts in via the
171+
``SAGEMAKER_ENABLE_RICH_LOGGING`` environment variable
172+
(see :func:`is_rich_logging_enabled`) or the caller passes ``force=True``. This
173+
keeps ``import sagemaker`` free of global traceback/console side effects by
174+
default.
175+
176+
Args:
177+
force (bool): Enable regardless of the environment variable, for callers
178+
that explicitly want rich output. Defaults to False.
179+
"""
145180
global textual_rich_console_and_traceback_enabled
181+
if not (force or is_rich_logging_enabled()):
182+
return
146183
if not textual_rich_console_and_traceback_enabled:
147184
theme = get_textual_rich_theme()
148185
reconfigure(theme=theme)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""Unit tests for opt-in rich console/traceback behavior in sagemaker.core.utils.utils.
2+
3+
These lock in that importing the SDK does not override sys.excepthook or restyle the
4+
process-global rich console unless the user explicitly opts in via
5+
SAGEMAKER_ENABLE_RICH_LOGGING (or a force=True call).
6+
"""
7+
import os
8+
from unittest.mock import patch
9+
10+
import pytest
11+
12+
from sagemaker.core.utils import utils
13+
from sagemaker.core.utils.utils import (
14+
RICH_LOGGING_OPT_IN_ENV_VAR,
15+
enable_textual_rich_console_and_traceback,
16+
is_rich_logging_enabled,
17+
)
18+
19+
20+
@pytest.fixture(autouse=True)
21+
def _reset_latch():
22+
"""Reset the one-shot 'already enabled' latch around every test."""
23+
saved = utils.textual_rich_console_and_traceback_enabled
24+
utils.textual_rich_console_and_traceback_enabled = False
25+
try:
26+
yield
27+
finally:
28+
utils.textual_rich_console_and_traceback_enabled = saved
29+
30+
31+
@pytest.mark.parametrize("value", ["1", "true", "TRUE", "Yes", "on", " on "])
32+
def test_is_rich_logging_enabled_truthy(value):
33+
with patch.dict(os.environ, {RICH_LOGGING_OPT_IN_ENV_VAR: value}):
34+
assert is_rich_logging_enabled() is True
35+
36+
37+
@pytest.mark.parametrize("value", ["", "0", "false", "no", "off", "nope"])
38+
def test_is_rich_logging_enabled_falsey(value):
39+
with patch.dict(os.environ, {RICH_LOGGING_OPT_IN_ENV_VAR: value}):
40+
assert is_rich_logging_enabled() is False
41+
42+
43+
def test_disabled_by_default_is_noop():
44+
# Env var absent -> importing/using the SDK must not touch the global console
45+
# or install rich tracebacks (no sys.excepthook override).
46+
with patch.dict(os.environ, {}, clear=False):
47+
os.environ.pop(RICH_LOGGING_OPT_IN_ENV_VAR, None)
48+
with patch.object(utils, "reconfigure") as mock_reconfigure, patch.object(
49+
utils, "install"
50+
) as mock_install:
51+
enable_textual_rich_console_and_traceback()
52+
mock_reconfigure.assert_not_called()
53+
mock_install.assert_not_called()
54+
assert utils.textual_rich_console_and_traceback_enabled is False
55+
56+
57+
def test_enabled_when_opted_in_via_env():
58+
with patch.dict(os.environ, {RICH_LOGGING_OPT_IN_ENV_VAR: "true"}):
59+
with patch.object(utils, "reconfigure") as mock_reconfigure, patch.object(
60+
utils, "install"
61+
) as mock_install:
62+
enable_textual_rich_console_and_traceback()
63+
mock_reconfigure.assert_called_once()
64+
mock_install.assert_called_once()
65+
assert utils.textual_rich_console_and_traceback_enabled is True
66+
67+
68+
def test_force_enables_regardless_of_env():
69+
with patch.dict(os.environ, {}, clear=False):
70+
os.environ.pop(RICH_LOGGING_OPT_IN_ENV_VAR, None)
71+
with patch.object(utils, "reconfigure") as mock_reconfigure, patch.object(
72+
utils, "install"
73+
) as mock_install:
74+
enable_textual_rich_console_and_traceback(force=True)
75+
mock_reconfigure.assert_called_once()
76+
mock_install.assert_called_once()
77+
78+
79+
def test_enable_is_idempotent_when_opted_in():
80+
with patch.dict(os.environ, {RICH_LOGGING_OPT_IN_ENV_VAR: "1"}):
81+
with patch.object(utils, "reconfigure") as mock_reconfigure, patch.object(
82+
utils, "install"
83+
) as mock_install:
84+
enable_textual_rich_console_and_traceback()
85+
enable_textual_rich_console_and_traceback()
86+
# The one-shot latch prevents re-installing on the second call.
87+
mock_reconfigure.assert_called_once()
88+
mock_install.assert_called_once()

0 commit comments

Comments
 (0)