Skip to content

Commit cb6a60d

Browse files
authored
[air] Add stacklevel option to deprecation_warning (#58357)
Currently are deprecation warnings sometimes not informative enough. The the warning is triggered it does not tell us *where* the deprecated feature is used. For example, ray internally raises a deprecation warning when an `RLModuleConfig` is initialized. ```python >>> from ray.rllib.core.rl_module.rl_module import RLModuleConfig >>> RLModuleConfig() 2025-11-02 18:21:27,318 WARNING deprecation.py:50 -- DeprecationWarning: `RLModule(config=[RLModuleConfig object])` has been deprecated. Use `RLModule(observation_space=.., action_space=.., inference_only=.., model_config=.., catalog_class=..)` instead. This will raise an error in the future! ``` This is confusing, where did *I* use a config, what am I doing wrong? This raises issues like: https://discuss.ray.io/t/warning-deprecation-py-50-deprecationwarning-rlmodule-config-rlmoduleconfig-object-has-been-deprecated-use-rlmodule-observation-space-action-space-inference-only-model-config-catalog-class-instead/23064 Tracing where the error actually happens is tedious - is it my code or internal? The output just shows `deprecation.:50`. Not helpful. This PR adds a stacklevel option with stacklevel=2 as the default to all `deprecation_warning`s. So devs and users can better see where is the deprecated option actually used. --- EDIT: **Before** ```python WARNING deprecation.py:50 -- DeprecationWarning: `RLModule(config=[RLModuleConfig object])` ``` **After** module.py:line where the deprecated artifact is used is shown in the log output: When building an Algorithm: ```python WARNING rl_module.py:445 -- DeprecationWarning: `RLModule(config=[RLModuleConfig object])` has been deprecated. Use `RLModule(observation_space=.., action_space=.., inference_only=.., model_config=.., catalog_class=..)` instead. This will raise an error in the future! ``` ```python .../ray/tune/logger/unified.py:53: RayDeprecationWarning: This API is deprecated and may be removed in future Ray releases. You could suppress this warning by setting env variable PYTHONWARNINGS="ignore::DeprecationWarning" ``` Signed-off-by: Daraan <[email protected]>
1 parent 5bff52a commit cb6a60d

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

python/ray/_common/deprecation.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def deprecation_warning(
1919
*,
2020
help: Optional[str] = None,
2121
error: Optional[Union[bool, Exception]] = None,
22+
stacklevel: int = 2,
2223
) -> None:
2324
"""Warns (via the `logger` object) or throws a deprecation warning/error.
2425
@@ -30,6 +31,9 @@ def deprecation_warning(
3031
error: Whether or which exception to raise. If True, raise ValueError.
3132
If False, just warn. If `error` is-a subclass of Exception,
3233
raise that Exception.
34+
stacklevel: The stacklevel to use for the warning message.
35+
Use 2 to point to where this function is called, 3+ to point
36+
further up the stack.
3337
3438
Raises:
3539
ValueError: If `error=True`.
@@ -48,7 +52,8 @@ def deprecation_warning(
4852
raise ValueError(msg)
4953
else:
5054
logger.warning(
51-
"DeprecationWarning: " + msg + " This will raise an error in the future!"
55+
"DeprecationWarning: " + msg + " This will raise an error in the future!",
56+
stacklevel=stacklevel,
5257
)
5358

5459

@@ -105,6 +110,7 @@ def patched_init(*args, **kwargs):
105110
new=new,
106111
help=help,
107112
error=error,
113+
stacklevel=3,
108114
)
109115
return obj_init(*args, **kwargs)
110116

@@ -123,6 +129,7 @@ def _ctor(*args, **kwargs):
123129
new=new,
124130
help=help,
125131
error=error,
132+
stacklevel=3,
126133
)
127134
# Call the deprecated method/function.
128135
return obj(*args, **kwargs)

0 commit comments

Comments
 (0)