-
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] type hints in python package #3756
Comments
I'm +1. |
xgboost has been adding type hints gradually and the dask module is completely typed. From my experience it's quite useful in both documentation and type checks. |
I did the following this morning to make this more friendly for outside contributors:
Anyone reading this is welcome to contribute! Thanks for helping to improve LightGBM. |
I would like to take this issue up if it's not closed |
@kunjshukla sure, pull requests are welcome. Please make them small and focused... don't try to submit one large PR that adds type hints in all the remaining places missing them. |
Request for Comment
#3581 officially ended support for Python 2 in the Python package.
Now that Python 2 support has been ended, I'd like to propose adding type hints to the library (https://docs.python.org/3/library/typing.html).
Benefits of adding type hints:
mypy
, a static type checker for Python that will catch issues like "this function call can return None but you're accessing a property of the result as if it isn't None"optuna
) to catch issues in their own code usingmypy
I'm opening this Request for Comment to get thoughts on this narrow proposal:
Status: Accepted
If maintainers agree to this proposal, we could add a "good first issue" about adding hints for other modules and could talk aboutmypy
separately, at some point in the future.This was originally opened as a request for comment, but it's been accepted and we're now accepting contributions to add type hints in the Python package!
How to contribute
Anyone is welcome to submit a pull request adding type hints to places in the Python package that are missing them!
Please limit pull requests to 1 function/method per pull request, to make them easier to review.
(shouldn't be touched because this file is executed by default Python 2 installed on GitHub Actions workers).ci/get_workflow_status.py
docs/conf.py
([python] add type hints in docs/conf.py #4526)helpers/check_dynamic_dependencies.py
([python] add type hints to check_dynamic_dependencies.py #4382)helpers/parameter_generator.py
([python] Add type hints to helpers/parameter_generator.py #4474)python-package/lightgbm/basic.py
python-package/lightgbm/callback.py
([python-package] Add type hints to the callback file #4093)python-package/lightgbm/compat.py
python-package/lightgbm/dask.py
([dask] Add type hints in Dask package #3866)python-package/lightgbm/engine.py
python-package/lightgbm/libpath.py
([python-package] Add type hint to the libpath file #4070)python-package/lightgbm/plotting.py
([python] Add type hints to python-package/lightgbm/plotting.py #4367)python-package/lightgbm/sklearn.py
python-package/setup.py
([python] add type hints to python-package/setup.py #4376)Handling line breaks
If adding hints to a method or function greatly increases the length of its lines, break each argument onto a separate line and have the closing parenthesis begin its own line, vertically aligned with the keyword
def
. Like this:references that may help you
python-package/lightgbm/dask.py
: [dask] Add type hints in Dask package #3866mypy
warnings: [ci] add mypy to linting task #3868How this improves LightGBM
Type hints in the code serve as additional documentation of the expected input and output types for functions and methods.
Having richer type hints can help static analyzers like
mypy
catch bugs that otherwise might not be caught by LightGBM's unit tests.The text was updated successfully, but these errors were encountered: