-
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
[python-package] make _InnerPredictor construction stricter #5961
Conversation
if self._handle is not None: | ||
_safe_call(_LIB.LGBM_BoosterFree(self._handle)) | ||
# ensure that existing Booster is freed before replacing it | ||
# with a new one createdfrom file | ||
_safe_call(_LIB.LGBM_BoosterFree(self._handle)) |
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.
As of #5947, there is no path through lightgbm
's public API which would allow a user to overwrite Booster._handle
with None
.
Making these changes to ensure it's never None
avoids the need to do if {booster}._handle is not None
types of checks in _InnerPredictor.from_booster()
.
Co-authored-by: José Morales <[email protected]>
This pull request has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this. |
Proposes some changes to make the construction of an
_InnerPredictor
:booster_handle
andmodel_file
(and the correspondingif-else
block) with class methods.from_booster()
and.from_model_file()
Booster._to_predictor()
, in favor of direct use of those class methodsBooster._handle
toNone
inBooster.__init__()
(so thatmypy
can confirm that it's never possible for it to beNone
)_InnnerPredictor.num_total_iteration
, in favor of just calling_InnerPredictor.current_iteration()
in the one place that was using_InnnerPredictor.num_total_iteration
These changes reduce unnecessary duplication, make it easier to understand the relationship between
_InnerPredictor
andBooster
, and reduce the risk of "silently fell back to a default value" types of bugs by making these internal APIs stricter (no default values and mostly don't acceptNone
).Notes for Reviewers
Now that v4.0.0 is almost out (#5952), next I'm looking to spend some time on making it easier for outside contributors to contribute to LightGBM. This is the first in a series of PRs I'm planning to make LightGBM's internals stricter and easier to understand.
How I tested this
In addition to the unit tests, confirmed that this does not introduce any new errors from
mypy
by running the following on bothmaster
and this branch.