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

[python] add type hints to _compare_params_for_warning() and make it reusable #4824

Merged
merged 1 commit into from
Nov 23, 2021
Merged
Changes from all 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
30 changes: 23 additions & 7 deletions python-package/lightgbm/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1756,17 +1756,29 @@ def __init_from_csc(self, csc, params_str, ref_dataset):
return self

@staticmethod
def _compare_params_for_warning(params, other_params):
"""Compare params.
def _compare_params_for_warning(
params: Optional[Dict[str, Any]],
other_params: Optional[Dict[str, Any]],
ignore_keys: Set[str]
) -> bool:
"""Compare two dictionaries with params ignoring some keys.

It is only for the warning purpose. Thus some keys are ignored.
It is only for the warning purpose.

Parameters
----------
params : dict or None
One dictionary with parameters to compare.
other_params : dict or None
Another dictionary with parameters to compare.
ignore_keys : set
Keys that should be ignored during comparing two dictionaries.

Returns
-------
compare_result: bool
If they are equal, return True; Otherwise, return False.
compare_result : bool
Returns whether two dictionaries with params are equal.
"""
ignore_keys = _ConfigAliases.get("categorical_feature")
if params is None:
params = {}
if other_params is None:
Expand Down Expand Up @@ -1794,7 +1806,11 @@ def construct(self):
reference_params = self.reference.get_params()
params = self.get_params()
if params != reference_params:
if self._compare_params_for_warning(params, reference_params) is False:
if not self._compare_params_for_warning(
params=params,
other_params=reference_params,
ignore_keys=_ConfigAliases.get("categorical_feature")
):
_log_warning('Overriding the parameters from Reference Dataset.')
self._update_params(reference_params)
if self.used_indices is None:
Expand Down