Unverified Commit 1ff120cb authored by Nick Miller's avatar Nick Miller Committed by GitHub
Browse files

[python-package] Add specific error messages to `test_basic` and `test_dask` tests (#6893)



* add error messages to test_list_to_1d_numpy

* add error messages to test_training_works_if_client_not_provided_or_set_after_construction

* ensure name value is captured in error message, update name for all _list_to_1d_numpy calls

---------
Co-authored-by: default avatarNikita Titov <nekit94-08@mail.ru>
parent 17fda872
...@@ -675,6 +675,8 @@ def test_list_to_1d_numpy(collection, dtype, rng): ...@@ -675,6 +675,8 @@ def test_list_to_1d_numpy(collection, dtype, rng):
"2d_list": [[1], [2]], "2d_list": [[1], [2]],
} }
y = collection2y[collection] y = collection2y[collection]
custom_name = "my_custom_variable"
if collection.startswith("pd"): if collection.startswith("pd"):
if not PANDAS_INSTALLED: if not PANDAS_INSTALLED:
pytest.skip("pandas is not installed") pytest.skip("pandas is not installed")
...@@ -682,17 +684,23 @@ def test_list_to_1d_numpy(collection, dtype, rng): ...@@ -682,17 +684,23 @@ def test_list_to_1d_numpy(collection, dtype, rng):
y = pd_Series(y) y = pd_Series(y)
if isinstance(y, np.ndarray) and len(y.shape) == 2: if isinstance(y, np.ndarray) and len(y.shape) == 2:
with pytest.warns(UserWarning, match="column-vector"): with pytest.warns(UserWarning, match="column-vector"):
lgb.basic._list_to_1d_numpy(y, dtype=np.float32, name="list") lgb.basic._list_to_1d_numpy(y, dtype=np.float32, name=custom_name)
return return
elif isinstance(y, list) and isinstance(y[0], list): elif isinstance(y, list) and isinstance(y[0], list):
with pytest.raises(TypeError): err_msg = (
lgb.basic._list_to_1d_numpy(y, dtype=np.float32, name="list") rf"Wrong type\(list\) for {custom_name}.\n"
r"It should be list, numpy 1-D array or pandas Series"
)
with pytest.raises(TypeError, match=err_msg):
lgb.basic._list_to_1d_numpy(y, dtype=np.float32, name=custom_name)
return return
elif isinstance(y, pd_Series) and y.dtype == object: elif isinstance(y, pd_Series) and y.dtype == object:
with pytest.raises(ValueError): with pytest.raises(
lgb.basic._list_to_1d_numpy(y, dtype=np.float32, name="list") ValueError, match=r"pandas dtypes must be int, float or bool\.\nFields with bad pandas dtypes: 0: object"
):
lgb.basic._list_to_1d_numpy(y, dtype=np.float32, name=custom_name)
return return
result = lgb.basic._list_to_1d_numpy(y, dtype=dtype, name="list") result = lgb.basic._list_to_1d_numpy(y, dtype=dtype, name=custom_name)
assert result.size == 10 assert result.size == 10
assert result.dtype == dtype assert result.dtype == dtype
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
"""Tests for lightgbm.dask module""" """Tests for lightgbm.dask module"""
import inspect import inspect
import re
import socket import socket
from itertools import groupby from itertools import groupby
from os import getenv from os import getenv
...@@ -1016,7 +1017,11 @@ def test_training_works_if_client_not_provided_or_set_after_construction(task, c ...@@ -1016,7 +1017,11 @@ def test_training_works_if_client_not_provided_or_set_after_construction(task, c
assert dask_model.client_ == client assert dask_model.client_ == client
local_model = dask_model.to_local() local_model = dask_model.to_local()
with pytest.raises(AttributeError): no_client_attr_msg = re.compile(
f"{repr(type(local_model).__name__)} object has no attribute '(client|client_)'"
)
with pytest.raises(AttributeError, match=no_client_attr_msg):
local_model.client local_model.client
local_model.client_ local_model.client_
...@@ -1040,7 +1045,7 @@ def test_training_works_if_client_not_provided_or_set_after_construction(task, c ...@@ -1040,7 +1045,7 @@ def test_training_works_if_client_not_provided_or_set_after_construction(task, c
assert dask_model.client_ == client assert dask_model.client_ == client
local_model = dask_model.to_local() local_model = dask_model.to_local()
with pytest.raises(AttributeError): with pytest.raises(AttributeError, match=no_client_attr_msg):
local_model.client local_model.client
local_model.client_ local_model.client_
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment