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] fix mypy errors about custom eval functions in sklearn.py #5721

Merged
merged 1 commit into from
Feb 16, 2023

Conversation

jameslamb
Copy link
Collaborator

Contributes to #3756
Contributes to #3867.

Fixes the following errors from mypy.

python-package/lightgbm/sklearn.py:203: error: Incompatible return value type (got "Union[Tuple[str, float, bool], List[Tuple[str, float, bool]]]", expected "Tuple[str, float, bool]")  [return-value]
python-package/lightgbm/sklearn.py:205: error: Incompatible return value type (got "Union[Tuple[str, float, bool], List[Tuple[str, float, bool]]]", expected "Tuple[str, float, bool]")  [return-value]
python-package/lightgbm/sklearn.py:207: error: Incompatible return value type (got "Union[Tuple[str, float, bool], List[Tuple[str, float, bool]]]", expected "Tuple[str, float, bool]")  [return-value]

In the scikit-learn interface, custom eval functions can return either a tuple of the form (eval_name, eval_result, is_higher_better) or a list of such tuples.

func : callable
Expects a callable with following signatures:
``func(y_true, y_pred)``,
``func(y_true, y_pred, weight)``
or ``func(y_true, y_pred, weight, group)``
and returns (eval_name, eval_result, is_higher_better) or
list of (eval_name, eval_result, is_higher_better):

mypy is raising these complaints because that return type annotation on _EvalFunctionWrapper doesn't include that possibility of returning a list of eval results.

def __call__(self, preds: np.ndarray, dataset: Dataset) -> Tuple[str, float, bool]:

@jameslamb jameslamb merged commit f136de4 into master Feb 16, 2023
@jameslamb jameslamb deleted the ci/mypy-sklearn-custom-metric branch February 16, 2023 18:07
@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.

2 participants