Skip to content

[python-package] type hints in python package #3756

Open

Description

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:

  • serves as additional documentation about the types of arguments and return types of functions
  • allows us to enable 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"
  • allows other libraries that integrate with LightGBM (like optuna) to catch issues in their own code using mypy

I'm opening this Request for Comment to get thoughts on this narrow proposal:

would you support a PR where I add type hints to the code in the https://github.com/microsoft/LightGBM/blob/master/python-package/lightgbm/dask.py?

Status: Accepted

If maintainers agree to this proposal, we could add a "good first issue" about adding hints for other modules and could talk about mypy 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.

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:

# before
def make_ranking(n_samples=100, n_features=20, n_informative=5, gmax=2,
                 group=None, random_gs=False, avg_gs=10, random_state=0):

# after
def make_ranking(
    n_samples: int = 100,
    n_features: int = 20,
    n_informative: int = 5,
    gmax: int = 2,
    group: Optional[List[int]] = None,
    random_gs: bool = False,
    avg_gs: int = 10,
    random_state: int = 0
) -> Tuple[pd.DataFrame, pd.DataFrame]:

references that may help you

How 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions