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

[ci] [python-package] fix mypy error about return_cvbooster in cv() #5845

Merged
merged 1 commit into from
Apr 24, 2023

Conversation

jameslamb
Copy link
Collaborator

Contributes to #3867.

Fixes the following error from mypy.

python-package/lightgbm/engine.py:766: error: Incompatible types in assignment (expression has type "CVBooster", target has type "List[float]")  [assignment]

That error is raised because mypy (correctly) infers that the return value for cv() is a Dict[str, List[float]] here:

results = collections.defaultdict(list)

for _, key, mean, _, std in res:
results[f'{key}-mean'].append(mean)
results[f'{key}-stdv'].append(std)

But then it tries to add an item that is a CVBooster here:

if return_cvbooster:
results['cvbooster'] = cvfolds

I think just #type: ignore-ing this error is preferable to other possible fixes like:

  • changing the type of results to Dict[str, Any] (which would reduce mypy's ability to catch issues in the main update loop in cv()
  • changing that return statement to something like return {**results, 'cvbooster': cvfolds}, which would add to the peak memory usage for running cv() by temporarily copying results

@jameslamb jameslamb merged commit ac37bf8 into master Apr 24, 2023
@jameslamb jameslamb deleted the ci/mypy-cvbooster branch April 24, 2023 16:41
@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