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] declare the use of inline type hints #5709

Merged
merged 2 commits into from
Feb 13, 2023

Conversation

jameslamb
Copy link
Collaborator

Contributes to #3756.
Contributes to #3867.

This PR proposes updating the Python package to declare that inline type annotations are used.

  • adds trove classifier Typing :: Typed
  • adds a py.typed file

Why add the Typing :: Typed trove classifier?

This classifier helps researchers and others summarizing PyPI's contents to identify packages that export type annotations.

See the discussion in pypi/warehouse#4348.

There are 5200+ packages on PyPI using this classifier: https://pypi.org/search/?c=Typing+%3A%3A+Typed.

Why add a py.typed file?

As described in PEP 561 (link), including an empty file named py.typed tells tools like mypy that a package's source code contains type annotations.

That doesn't matter for lightgbm itself, but it's helpful for anyone writing code that depends on lightgbm (e.g. optuna, lightgbm-ray, lots of other user code) and wanting to check that code with mypy.

Consider the following example. Given the following file...

"""script.py"""
import lightgbm as lgb

clf = lgb.LGBMClassifier(n_estimators="not-a-number")

Running

mypy ./script.py

if you have lightgbm < 4.0 installed from PyPI or from master, you'll see the following error.

test.py:1: error: Skipping analyzing "lightgbm": module is installed, but missing library stubs or py.typed marker
test.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)

But on this branch, mypy is able to check that code 😁

script.py:5: error: Argument "n_estimators" to "LGBMClassifier" has incompatible type "str"; expected "int"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

@jameslamb jameslamb merged commit 286cdb5 into master Feb 13, 2023
@jameslamb jameslamb deleted the python/declare-typed branch February 13, 2023 04:19
@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 16, 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