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 in engine.py #4839

Merged
merged 5 commits into from
Dec 2, 2021
Merged

Conversation

jameslamb
Copy link
Collaborator

Contributes to #3867.

This PR fixes mypy errors in engine.py

python-package/lightgbm/engine.py:226: error: Incompatible types in assignment (expression has type "Set[]", variable has type "Optional[List[Callable[..., Any]]]")
python-package/lightgbm/engine.py:230: error: Incompatible types in assignment (expression has type "Set[Callable[..., Any]]", variable has type "Optional[List[Callable[..., Any]]]")
python-package/lightgbm/engine.py:242: error: Item "List[Callable[..., Any]]" of "Optional[List[Callable[..., Any]]]" has no attribute "add"
python-package/lightgbm/engine.py:242: error: Item "None" of "Optional[List[Callable[..., Any]]]" has no attribute "add"
python-package/lightgbm/engine.py:244: error: Item "List[Callable[..., Any]]" of "Optional[List[Callable[..., Any]]]" has no attribute "add"
python-package/lightgbm/engine.py:244: error: Item "None" of "Optional[List[Callable[..., Any]]]" has no attribute "add"
python-package/lightgbm/engine.py:247: error: Item "List[Callable[..., Any]]" of "Optional[List[Callable[..., Any]]]" has no attribute "add"
python-package/lightgbm/engine.py:247: error: Item "None" of "Optional[List[Callable[..., Any]]]" has no attribute "add"
python-package/lightgbm/engine.py:252: error: Item "List[Callable[..., Any]]" of "Optional[List[Callable[..., Any]]]" has no attribute "add"
python-package/lightgbm/engine.py:252: error: Item "None" of "Optional[List[Callable[..., Any]]]" has no attribute "add"
python-package/lightgbm/engine.py:254: error: Item "None" of "Optional[List[Callable[..., Any]]]" has no attribute "iter" (not iterable)
python-package/lightgbm/engine.py:255: error: Unsupported left operand type for - ("List[Callable[..., Any]]")
python-package/lightgbm/engine.py:255: error: Unsupported left operand type for - ("None")
python-package/lightgbm/engine.py:255: note: Left operand is of type "Optional[List[Callable[..., Any]]]"
python-package/lightgbm/engine.py:256: error: Incompatible types in assignment (expression has type "List[Union[Callable[..., Any], Any]]", variable has type "Set[Union[Callable[..., Any], Any]]")
python-package/lightgbm/engine.py:300: error: Incompatible types in assignment (expression has type "float", variable has type "List[Any]")

Confirmed that this suppresses these warnings by running the following.

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

Notes for Reviewers

Some of these errors caught an issue in the type hints for callbacks.EarlyStopException. That exception has a property best_score which seems like it should be a float, but in every place where one of those is created, best_score is populated with a list of eval results similar to this:

[
    ('training', 'binary_logloss', 0.014361246252423975, False),
    ('training', 'mape', 0.013889796906138775, False),
    ('valid', 'binary_logloss', 0.0561150102648419, False),
    ('valid', 'mape', 0.042410104106485866, False)
]

evaluation_result_list = earlyStopException.best_score

raise EarlyStopException(best_iter[i], best_score_list[i])

raise EarlyStopException(best_iter[i], best_score_list[i])

python-package/lightgbm/callback.py Outdated Show resolved Hide resolved
python-package/lightgbm/callback.py Outdated Show resolved Hide resolved
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.

Thank you very much for fixing callback typehints!

@jameslamb jameslamb merged commit 946817a into master Dec 2, 2021
@jameslamb jameslamb deleted the mypy-engine branch December 2, 2021 20:46
@StrikerRUS StrikerRUS mentioned this pull request Jan 6, 2022
13 tasks
@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 23, 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