[python-package] [ci] remove runtime docstring building, test with different PYTHONOPTIMIZE levels #7403

Open
jameslamb opened 9:51am on August 20, 2026 wants to merge 631 Ξ” into microsoft/lightgbm main from
python/no-runtime-doc-parsing
Reviewing
482 fewer changed lines
(18% less) vs GitHub
This saves about 31.5 hours per year vs conventional diff tools
jameslamb authored
of work between July 26 and September 8
Diff Delta:
631
About 38 Diff Delta/hour
Classified as:  Bug Fix, Feature Add, General

jameslamb's Description of Work

Fixes #7362
Contributes to #7327

The Python package has 7 scikit-learn compatible estimators, and there is a lot of overlap in their docstrings. To keep them consistent, the estimators reference build up their docstrings at import time from templates and inherited docstrings, like this:

https://github.com/lightgbm-org/LightGBM/blob/3cd99268f09615b2e2da6e93269a7e6db199b342/python-package/lightgbm/sklearn.py#L1555-L1561

This proposes removing that stuff in favor of just using literal docstrings.

Changes in this PR


  • removes all the import-time docstring-templating stuff, in favor of literal docstrings

  • adds unit tests to ensure the docstrings remain consistent with each other

  • adds tests in CI that import lightgbm works when run under PYTHONOPTIMIZE=1 and PYTHONOPTIMIZE=2

Benefits of this change


  • allows lightgbm to be imported when using PYTHONOPTIMIZE=2 (fixes #7362)

  • very slightly reduces import time (in my local testing, by around 10ms)

  • makes it easier to reason about the docs and edit them

How I tested this

8 total changed files
Loading changes...
Loading changes...
Loading changes...
Loading changes...
Loading changes...
Loading changes...
End of PR:
Like the credits, but with fewer stunts.