Skip to content

Commit ca2bada

Browse files
George Wealecopybara-github
authored andcommitted
feat: Add env var to suppress experimental warnings
This CL adds ADK_DISABLE_EXPERIMENTAL_WARNING to let the users to suppress warning messages from features decorated with @experimental. Previously, using experimental features would always trigger a UserWarning. This change creates a way to disable these warnings, which can be good to stop flooding logs. The warning is suppressed if ADK_DISABLE_EXPERIMENTAL_WARNING is set to a truthy value such as "true", "1", "yes", or "on" (case-insensitive). Added unit tests to make sure: Warning suppression for functions and classes when the env var is set. Case-insensitivity and various truthy values for the env var. Loading the env var from a .env file. PiperOrigin-RevId: 794740730
1 parent c843503 commit ca2bada

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/google/adk/utils/feature_decorator.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,18 @@ def new_init(self, *args, **kwargs):
7070
bypass_env_var is not None
7171
and os.environ.get(bypass_env_var, "").lower() == "true"
7272
)
73+
# Suppress experimental warnings if env is set
74+
suppress_experimental = (
75+
label.upper() == "EXPERIMENTAL"
76+
and os.environ.get("ADK_DISABLE_EXPERIMENTAL_WARNING", "").lower()
77+
in ("1", "true", "yes", "on")
78+
)
7379

7480
if should_bypass:
7581
# Bypass completely - no warning, no error
7682
pass
83+
elif suppress_experimental:
84+
pass
7785
elif block_usage:
7886
raise RuntimeError(msg)
7987
else:
@@ -92,10 +100,18 @@ def wrapper(*args, **kwargs):
92100
bypass_env_var is not None
93101
and os.environ.get(bypass_env_var, "").lower() == "true"
94102
)
103+
# Suppress experimental warnings if env is set
104+
suppress_experimental = (
105+
label.upper() == "EXPERIMENTAL"
106+
and os.environ.get("ADK_DISABLE_EXPERIMENTAL_WARNING", "").lower()
107+
in ("1", "true", "yes", "on")
108+
)
95109

96110
if should_bypass:
97111
# Bypass completely - no warning, no error
98112
pass
113+
elif suppress_experimental:
114+
pass
99115
elif block_usage:
100116
raise RuntimeError(msg)
101117
else:

tests/unittests/utils/test_feature_decorator.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ def test_working_in_progress_loads_from_dotenv_file():
208208

209209
def test_experimental_function_warns():
210210
"""Test that experimental function shows warnings (unchanged behavior)."""
211+
# Ensure environment variable is not set
212+
if "ADK_DISABLE_EXPERIMENTAL_WARNING" in os.environ:
213+
del os.environ["ADK_DISABLE_EXPERIMENTAL_WARNING"]
211214
with warnings.catch_warnings(record=True) as w:
212215
warnings.simplefilter("always")
213216

@@ -222,6 +225,9 @@ def test_experimental_function_warns():
222225

223226
def test_experimental_class_warns():
224227
"""Test that experimental class shows warnings (unchanged behavior)."""
228+
# Ensure environment variable is not set
229+
if "ADK_DISABLE_EXPERIMENTAL_WARNING" in os.environ:
230+
del os.environ["ADK_DISABLE_EXPERIMENTAL_WARNING"]
225231
with warnings.catch_warnings(record=True) as w:
226232
warnings.simplefilter("always")
227233

@@ -235,6 +241,67 @@ def test_experimental_class_warns():
235241
assert "class may change" in str(w[0].message)
236242

237243

244+
def test_experimental_function_warning_suppressed_with_env_var():
245+
"""Test that experimental function warnings are suppressed when env var is set."""
246+
true_values = ["true", "True", "TRUE", "1", "yes", "YES", "on", "ON"]
247+
for true_val in true_values:
248+
os.environ["ADK_DISABLE_EXPERIMENTAL_WARNING"] = true_val
249+
try:
250+
with warnings.catch_warnings(record=True) as w:
251+
warnings.simplefilter("always")
252+
result = experimental_fn()
253+
assert result == "executing"
254+
assert len(w) == 0, f"Warning not suppressed for env value {true_val}"
255+
finally:
256+
del os.environ["ADK_DISABLE_EXPERIMENTAL_WARNING"]
257+
258+
259+
def test_experimental_class_warning_suppressed_with_env_var():
260+
"""Test that experimental class warnings are suppressed when env var is set."""
261+
true_values = ["true", "True", "TRUE", "1", "yes", "YES", "on", "ON"]
262+
for true_val in true_values:
263+
os.environ["ADK_DISABLE_EXPERIMENTAL_WARNING"] = true_val
264+
try:
265+
with warnings.catch_warnings(record=True) as w:
266+
warnings.simplefilter("always")
267+
exp_class = ExperimentalClass()
268+
result = exp_class.run()
269+
assert result == "running experimental"
270+
assert len(w) == 0, f"Warning not suppressed for env value {true_val}"
271+
finally:
272+
del os.environ["ADK_DISABLE_EXPERIMENTAL_WARNING"]
273+
274+
275+
def test_experimental_function_warning_not_suppressed_for_false_env_var():
276+
"""Test that experimental function warnings are not suppressed for false-like env var values."""
277+
false_values = ["false", "False", "FALSE", "0", "", "no", "off"]
278+
for false_val in false_values:
279+
os.environ["ADK_DISABLE_EXPERIMENTAL_WARNING"] = false_val
280+
try:
281+
with warnings.catch_warnings(record=True) as w:
282+
warnings.simplefilter("always")
283+
experimental_fn()
284+
assert len(w) == 1
285+
assert "[EXPERIMENTAL] experimental_fn:" in str(w[0].message)
286+
finally:
287+
del os.environ["ADK_DISABLE_EXPERIMENTAL_WARNING"]
288+
289+
290+
def test_experimental_class_warning_not_suppressed_for_false_env_var():
291+
"""Test that experimental class warnings are not suppressed for false-like env var values."""
292+
false_values = ["false", "False", "FALSE", "0", "", "no", "off"]
293+
for false_val in false_values:
294+
os.environ["ADK_DISABLE_EXPERIMENTAL_WARNING"] = false_val
295+
try:
296+
with warnings.catch_warnings(record=True) as w:
297+
warnings.simplefilter("always")
298+
ExperimentalClass()
299+
assert len(w) == 1
300+
assert "[EXPERIMENTAL] ExperimentalClass:" in str(w[0].message)
301+
finally:
302+
del os.environ["ADK_DISABLE_EXPERIMENTAL_WARNING"]
303+
304+
238305
def test_experimental_class_no_parens_warns():
239306
"""Test that experimental class without parentheses shows default warning."""
240307
with warnings.catch_warnings(record=True) as w:

0 commit comments

Comments
 (0)