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-package] [dask] fix mypy errors about Dask fit() return types #5756

Merged
merged 1 commit into from
Mar 3, 2023

Conversation

jameslamb
Copy link
Collaborator

@jameslamb jameslamb commented Feb 28, 2023

Contributes to #3867.

Resolves the following mypy errors.

python-package/lightgbm/dask.py:1178: error: Incompatible return value type (got "_DaskLGBMModel", expected "DaskLGBMClassifier")  [return-value]
python-package/lightgbm/dask.py:1381: error: Incompatible return value type (got "_DaskLGBMModel", expected "DaskLGBMRegressor")  [return-value]
python-package/lightgbm/dask.py:1553: error: Incompatible return value type (got "_DaskLGBMModel", expected "DaskLGBMRanker")  [return-value]

Switches the Dask estimators to returning self, so that e.g. DaskLGBMRegressor.fit() returns a DaskLGBMRegressor. This is useful for other user code and downstream libraries relying on LightGBM, to be able to do isinstance() checks and not need to rely on lightgbm-internal details like _DaskLGBMModel. It could also be relevant in the future is we add attributes to DaskLGBMRegressor, DaskLGBMClassifier, or DaskLGBMRanker that the other Dask estimators don't have.

It is, by the way, exactly the same approach the sklearn estimators use:

class LGBMRegressor(_LGBMRegressorBase, LGBMModel):
"""LightGBM regressor."""
def fit( # type: ignore[override]
self,
X,
y,
sample_weight=None,
init_score=None,
eval_set=None,
eval_names: Optional[List[str]] = None,
eval_sample_weight=None,
eval_init_score=None,
eval_metric: Optional[_LGBM_ScikitEvalMetricType] = None,
feature_name: _LGBM_FeatureNameConfiguration = 'auto',
categorical_feature: _LGBM_CategoricalFeatureConfiguration = 'auto',
callbacks: Optional[List[Callable]] = None,
init_model: Optional[Union[str, Path, Booster, LGBMModel]] = None
) -> "LGBMRegressor":
"""Docstring is inherited from the LGBMModel."""
super().fit(
X,
y,
sample_weight=sample_weight,
init_score=init_score,
eval_set=eval_set,
eval_names=eval_names,
eval_sample_weight=eval_sample_weight,
eval_init_score=eval_init_score,
eval_metric=eval_metric,
feature_name=feature_name,
categorical_feature=categorical_feature,
callbacks=callbacks,
init_model=init_model
)
return self

Related conversation: #5672 (comment)

@jameslamb jameslamb changed the title WIP: [python-package] [dask] fix mypy errors about Dask fit() return types [python-package] [dask] fix mypy errors about Dask fit() return types Feb 28, 2023
@jameslamb jameslamb marked this pull request as ready for review February 28, 2023 16:13
@jameslamb jameslamb requested a review from jmoralez as a code owner February 28, 2023 16:13
@jameslamb
Copy link
Collaborator Author

@jmoralez what do you think about this one?

(no rush)

@jameslamb jameslamb merged commit 709ea4c into master Mar 3, 2023
@jameslamb jameslamb deleted the ci/mypy-dask-return branch March 3, 2023 01:37
@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants