Unverified Commit 26cc160a authored by James Lamb's avatar James Lamb Committed by GitHub
Browse files

[python-package] convert string concatenation to f-strings in test_engine.py (fixes #4136) (#4436)



* [python-package] convert string concatenation to f-strings in test_engine.py (fixes #4136)

* Update tests/python_package_test/test_engine.py
Co-authored-by: default avatarNikita Titov <nekit94-08@mail.ru>

* revert get_workflow_status changes
Co-authored-by: default avatarNikita Titov <nekit94-08@mail.ru>
parent bc9d34e4
...@@ -819,11 +819,11 @@ def test_feature_name(): ...@@ -819,11 +819,11 @@ def test_feature_name():
X_train, y_train = load_boston(return_X_y=True) X_train, y_train = load_boston(return_X_y=True)
params = {'verbose': -1} params = {'verbose': -1}
lgb_train = lgb.Dataset(X_train, y_train) lgb_train = lgb.Dataset(X_train, y_train)
feature_names = ['f_' + str(i) for i in range(X_train.shape[-1])] feature_names = [f'f_{i}' for i in range(X_train.shape[-1])]
gbm = lgb.train(params, lgb_train, num_boost_round=5, feature_name=feature_names) gbm = lgb.train(params, lgb_train, num_boost_round=5, feature_name=feature_names)
assert feature_names == gbm.feature_name() assert feature_names == gbm.feature_name()
# test feature_names with whitespaces # test feature_names with whitespaces
feature_names_with_space = ['f ' + str(i) for i in range(X_train.shape[-1])] feature_names_with_space = [f'f {i}' for i in range(X_train.shape[-1])]
gbm = lgb.train(params, lgb_train, num_boost_round=5, feature_name=feature_names_with_space) gbm = lgb.train(params, lgb_train, num_boost_round=5, feature_name=feature_names_with_space)
assert feature_names == gbm.feature_name() assert feature_names == gbm.feature_name()
...@@ -2021,12 +2021,12 @@ def test_model_size(): ...@@ -2021,12 +2021,12 @@ def test_model_size():
multiplier = 100 multiplier = 100
total_trees = multiplier + 2 total_trees = multiplier + 2
try: try:
new_model_str = (model_str[:model_str.find('tree_sizes')] before_tree_sizes = model_str[:model_str.find('tree_sizes')]
+ '\n\n' trees = model_str[model_str.find('Tree=0'):model_str.find('end of trees')]
+ model_str[model_str.find('Tree=0'):model_str.find('end of trees')] more_trees = (one_tree * multiplier).format(*range(2, total_trees))
+ (one_tree * multiplier).format(*range(2, total_trees)) after_trees = model_str[model_str.find('end of trees'):]
+ model_str[model_str.find('end of trees'):] num_end_spaces = 2**31 - one_tree_size * total_trees
+ ' ' * (2**31 - one_tree_size * total_trees)) new_model_str = f"{before_tree_sizes}\n\n{trees}{more_trees}{after_trees}{'':{num_end_spaces}}"
assert len(new_model_str) > 2**31 assert len(new_model_str) > 2**31
bst.model_from_string(new_model_str, verbose=False) bst.model_from_string(new_model_str, verbose=False)
assert bst.num_trees() == total_trees assert bst.num_trees() == total_trees
...@@ -2398,10 +2398,13 @@ def test_dataset_update_params(): ...@@ -2398,10 +2398,13 @@ def test_dataset_update_params():
for key, value in unchangeable_params.items(): for key, value in unchangeable_params.items():
new_params = default_params.copy() new_params = default_params.copy()
new_params[key] = value new_params[key] = value
if key != "forcedbins_filename":
param_name = key
else:
param_name = "forced bins"
err_msg = ("Reducing `min_data_in_leaf` with `feature_pre_filter=true` may cause *" err_msg = ("Reducing `min_data_in_leaf` with `feature_pre_filter=true` may cause *"
if key == "min_data_in_leaf" if key == "min_data_in_leaf"
else "Cannot change {} *".format(key if key != "forcedbins_filename" else f"Cannot change {param_name} *")
else "forced bins"))
with np.testing.assert_raises_regex(lgb.basic.LightGBMError, err_msg): with np.testing.assert_raises_regex(lgb.basic.LightGBMError, err_msg):
lgb.train(new_params, lgb_data, num_boost_round=3) lgb.train(new_params, lgb_data, num_boost_round=3)
...@@ -2460,7 +2463,7 @@ def test_trees_to_dataframe(): ...@@ -2460,7 +2463,7 @@ def test_trees_to_dataframe():
pytest.importorskip("pandas") pytest.importorskip("pandas")
def _imptcs_to_numpy(X, impcts_dict): def _imptcs_to_numpy(X, impcts_dict):
cols = ['Column_' + str(i) for i in range(X.shape[1])] cols = [f'Column_{i}' for i in range(X.shape[1])]
return [impcts_dict.get(col, 0.) for col in cols] return [impcts_dict.get(col, 0.) for col in cols]
X, y = load_breast_cancer(return_X_y=True) X, y = load_breast_cancer(return_X_y=True)
......
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