-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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] Add type hints to the callback file #4093
[python-package] Add type hints to the callback file #4093
Conversation
@jameslamb Can you please confirm if I need to resolve these failed tests or if they're not caused by my patch? |
Some of the failures are unrelated to this PR's changes and should be fixed by merging the most recent However, I think the
That error from Sphinx is not very informative but it usually is caused by a legitimate underlying problem, for example #3192 (comment). Can you please provide the specific |
Thanks for the tips. Here is an example of a mypy error that CallbackWithAttributes resolves:
I did import an additional package (typing_extensions) which I guess is what caused the problem. Removing the package and one associated type hint resulted in no errors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for doing this @deddyjobson ! This one is tricky, really appreciate you working through it.
I left some initial comments, but I'd like @StrikerRUS to comment as well. I'm ok with the approach you've proposed of introducing a decorator to document the fact that we add attributes to the callback functions, I think that's a nice, non-invasive approach to improve the value we get from mypy
.
Co-authored-by: James Lamb <jaylamb20@gmail.com>
Co-authored-by: James Lamb <jaylamb20@gmail.com>
Make the documentation clearer. Co-authored-by: James Lamb <jaylamb20@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! I can see in the lint
job's logs that there are no mypy
warnings for callback.py
, and I think you've covered everything that needs a hint.
https://github.com/microsoft/LightGBM/pull/4093/checks?check_run_id=2207644984
Thanks very much! I won't merge until @StrikerRUS has a chance to look as well.
I'm afraid decorator will hurt performance as callback functions are called on each iteration. |
Removing the decorator results in 5 additional errors from mypy:
|
Thanks! I agree with @StrikerRUS and I didn't think about the overhead before. In this first round of adding type hints to the library, I think it's desirable to only make changes that don't negatively affect the performance of Can you please add |
Cool, didn't know I could do that. |
oh, yep! This is super useful. Python is a really flexible language, so there are a lot of situations that are hard for By the way, I think there needs to be two spaces between the end of the line and the comment x = something() # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks very much for the changes! This looks good to me. I can see in https://github.com/microsoft/LightGBM/pull/4093/checks?check_run_id=2242197161 that there are no mypy
warnings for callback.py
and I agree with the choices of type hints you made.
We resolved a few blocking issues in continuous integration today, so I've updated this with the changes from the latest |
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. |
What I did
Note