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 error about type change in Dataset.feature_num_bin() #5447

Merged
merged 1 commit into from
Aug 30, 2022

Conversation

jameslamb
Copy link
Collaborator

Contributes to #3756.
Contributes to #3867.

mypy currently raises the following error.

python-package/lightgbm/basic.py:2491: error: Argument 1 to "c_int" has incompatible type "Union[int, str]"; expected "int"

This is caused by the fact that mypy doesn't allow changing the type of an object after it's initially assigned. This blog post explains it well: https://adamj.eu/tech/2021/05/23/python-type-hints-mypy-doesnt-allow-variables-to-change-type/.

This PR proposes a slight refactoring of Dataset.feature_num_bin() to resolve this error. I think this change does also make the code slightly easier to understand.

Notes for Reviewers

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

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

@jameslamb jameslamb merged commit 465989f into master Aug 30, 2022
@jameslamb jameslamb deleted the int-hint branch August 30, 2022 01:10
@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.

2 participants