-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
[ci] [python-package] fix mypy errors in sklearn.py #5886
Conversation
@@ -956,7 +960,7 @@ def objective_(self) -> Union[str, _LGBM_ScikitCustomObjectiveFunction]: | |||
""":obj:`str` or :obj:`callable`: The concrete objective used while fitting this model.""" | |||
if not self.__sklearn_is_fitted__(): | |||
raise LGBMNotFittedError('No objective found. Need to call fit beforehand.') | |||
return self._objective | |||
return self._objective # type: ignore[return-value] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the same as what's described in #5788 .
self._objective
is initialized to None
.
LightGBM/python-package/lightgbm/sklearn.py
Line 402 in 0957ab7
objective: Optional[Union[str, _LGBM_ScikitCustomObjectiveFunction]] = None, |
And then overwritten with a string like "regression"
or a custom objective function during training.
But mypy
doesn't understand that it'll always be non-None
if self.__sklearn_is_fitted__()
is True
(in other words, that it's not possible under normal operation for a user to ever call model.objective_
and get None
back).
elif isinstance(eval_metric, list): | ||
eval_metric_list = copy.deepcopy(eval_metric) | ||
else: | ||
eval_metric_list = [copy.deepcopy(eval_metric)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This refactoring adds an explicit case for eval_metric is None
. mypy
can't tell, I guess, that isinstance(x, list)
would must mean something isn't None
.
This pull request has been automatically locked since there has not been any recent activity since it was closed. |
Contributes to #3756.
Contributes to #3867.
Fixes the following errors from
mypy
.