Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn warning messages into Python warnings. #9387

Merged
merged 3 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions python-package/xgboost/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ def _expect(expectations: Sequence[Type], got: Type) -> str:

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)
return
print(py_str(msg))
trivialfis marked this conversation as resolved.
Show resolved Hide resolved


Expand Down
6 changes: 2 additions & 4 deletions tests/python-gpu/load_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ def test_training_on_cpu_only_env(self) -> None:
rng = np.random.RandomState(1994)
X = rng.randn(10, 10)
y = rng.randn(10)
with tm.captured_output() as (out, err):
with pytest.warns(UserWarning, match="No visible GPU is found"):
# Test no thrust exception is thrown
with pytest.raises(xgb.core.XGBoostError):
with pytest.raises(xgb.core.XGBoostError, match="have at least one device"):
xgb.train({"tree_method": "gpu_hist"}, xgb.DMatrix(X, y))

assert out.getvalue().find("No visible GPU is found") != -1
20 changes: 9 additions & 11 deletions tests/python/test_with_sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pickle
import random
import tempfile
import warnings
from typing import Callable, Optional

import numpy as np
Expand Down Expand Up @@ -1091,25 +1092,22 @@ def test_constraint_parameters():
)


@pytest.mark.filterwarnings("error")
def test_parameter_validation():
reg = xgb.XGBRegressor(foo='bar', verbosity=1)
reg = xgb.XGBRegressor(foo="bar", verbosity=1)
X = np.random.randn(10, 10)
y = np.random.randn(10)
with tm.captured_output() as (out, err):
with pytest.warns(Warning, match="foo"):
reg.fit(X, y)
output = out.getvalue().strip()

assert output.find('foo') != -1

reg = xgb.XGBRegressor(n_estimators=2, missing=3,
importance_type='gain', verbosity=1)
reg = xgb.XGBRegressor(
n_estimators=2, missing=3, importance_type="gain", verbosity=1
)
X = np.random.randn(10, 10)
y = np.random.randn(10)
with tm.captured_output() as (out, err):
reg.fit(X, y)
output = out.getvalue().strip()

assert len(output) == 0
with warnings.catch_warnings():
reg.fit(X, y)


def test_deprecate_position_arg():
Expand Down