Skip to content

Commit

Permalink
Add Stacklevel to python warning callback. (#10977)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivialfis authored Nov 6, 2024
1 parent 197c0ae commit ab8aa6f
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion python-package/xgboost/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ def _log_callback(msg: bytes) -> None:
"""Redirect logs from native library into Python console"""
smsg = py_str(msg)
if smsg.find("WARNING:") != -1:
warnings.warn(smsg, UserWarning)
# Stacklevel:
# 1: This line
# 2: XGBoost C functions like `_LIB.XGBoosterTrainOneIter`.
# 3: The Python function that calls the C function.
warnings.warn(smsg, UserWarning, stacklevel=3)
return
print(smsg)

Expand Down

0 comments on commit ab8aa6f

Please sign in to comment.