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 callbacks #5450

Merged
merged 2 commits into from
Aug 30, 2022
Merged

Conversation

jameslamb
Copy link
Collaborator

Contributes to #3756.
Contributes to #3867.

mypy currently raises the following errors.

-python-package/lightgbm/engine.py:688: error: Incompatible types in assignment (expression has type "Set[<nothing>]", variable has type "Optional[List[Callable[..., Any]]]")
-python-package/lightgbm/engine.py:692: error: Incompatible types in assignment (expression has type "Set[Callable[..., Any]]", variable has type "Optional[List[Callable[..., Any]]]")
-python-package/lightgbm/engine.py:695: error: Item "List[Callable[..., Any]]" of "Optional[List[Callable[..., Any]]]" has no attribute "add"
-python-package/lightgbm/engine.py:695: error: Item "None" of "Optional[List[Callable[..., Any]]]" has no attribute "add"
-python-package/lightgbm/engine.py:707: error: Item "None" of "Optional[List[Callable[..., Any]]]" has no attribute "__iter__" (not iterable)
-python-package/lightgbm/engine.py:708: error: Unsupported left operand type for - ("List[Callable[..., Any]]")
-python-package/lightgbm/engine.py:708: error: Unsupported left operand type for - ("None")
-python-package/lightgbm/engine.py:708: note: Left operand is of type "Optional[List[Callable[..., Any]]]"
-python-package/lightgbm/engine.py:709: error: Incompatible types in assignment (expression has type "List[Union[Callable[..., Any], Any]]", variable has type "Set[Union[Callable[..., Any], Any]]")

These errors come from variables' types changing. Specifically, the fact that in the code for cv(), the variable callbacks is initially assigned a value with type Optional[List[Callable]], then is overwritten with a set, then the variable callbacks_before_iter is initialized as a set and overwritten with a list.

Similar to #5447, this PR proposes fixing those errors by using differently-named variables when types change.

Notes for Reviewers

The same changes proposed in this PR were implemented in #4839 to fix the same code in train().

I tested this by running mypy as documented in #3867.

mypy \
    --exclude='python-package/compile/|python-package/build' \
    --ignore-missing-imports \
    python-package/

Copy link
Collaborator

@StrikerRUS StrikerRUS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this!

@jameslamb jameslamb merged commit 4162485 into master Aug 30, 2022
@jameslamb jameslamb deleted the callback-types branch August 30, 2022 14:36
@jameslamb jameslamb mentioned this pull request Oct 7, 2022
40 tasks
@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 19, 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