[python-package] [ci] allow ImportErrors from dependencies to be raised, expand testing of imports #7456

Open
jameslamb opened 2:11am on September 25, 2026 wants to merge 243 Ξ” into microsoft/lightgbm main from
python/lazy-imports-pt3

Pull Request Overview

  • Opened on September 25, 2026
  • Status Open
  • Commit count 9 with first commit September 25, 2026

Total Delta

243 Total Diff Delta

Open Days

Open 50 weekdays

Test Delta

37 Diff Delta in Test Files
Breakdown by Phase

How long has this pull request spent in each phase of its lifecycle?

Fraction of total time Business days Phase
 
0.3 days Authoring 1 commit before pull request opened for review
 
0.0 days Awaiting first review
 
50.0 days Revising work with 8 commits in response to 0 reviews that left 8 comments

Total time for pull request still awaiting merge (longer than repo's target): 50.3 business days

Author avatar

[python-package] [ci] allow ImportErrors from dependencies to be raised, expand testing of imports

Contributes to #7327

Contributes to #7355

Closes #7169

Removes some try-catching in __init__.py that could make it harder to diagnose import issues (especially from dependencies).

To help with that, significantly expands testing of the optional-dependency handling in the Python package:


  • ensures test_basic.py can be run without any of lightgbm's optional dependencies + adds a CI job running it in an environment like that

  • adds explicit testing in CI that modules that should not be loaded are not loaded by import lightgbm

  • adds a statement coverage summary to Python test jobs (with pytest-cov) + enforcement of a coverage minimum (to ensure that all the test-skipping logic added in PRs like this doesn't accidentally lead to a loss of coverage in CI)

Comments Threads Pending Resolution

.ci/test-python-minimal-deps.sh
25
echo "running tests"
26
pytest \
27
    -ra \
28
    --cov=lightgbm \
29
    --cov-fail-under=30 \
.ci/test.sh
143
PYTEST_ARGS=(
144
    -ra
145
    --cov=lightgbm
146 146
    --cov-fail-under=
75
.github/workflows/python_package.yml
484
            /bin/bash -c '
485
              export PATH=/opt/miniforge/bin:$PATH
486
              pip install "build>=0.10"
487
              sh ./build-python.sh bdist_wheel --nomp
488
            '
.ci/test-imports.py
1
import sys
2
3
# import 'numpy' first to load system libraries
4
# TODO: remove when https://github.com/lightgbm-org/LightGBM/issues/7355 is resolved
5
import numpy as np  # noqa: F401
python-package/lightgbm/__init__.py
8
9
# .basic is intentionally loaded as early as possible, to dlopen() lib_lightgbm.{dll,dylib,so}
10
# and its dependencies as early as possible
11
# isort: split
tests/python_package_test/test_basic.py
20
    make_blobs,
21
    mse_obj,
22
    np_assert_array_equal,
23
    train_test_split,
24
)

Resolved Comment Threads

python-package/lightgbm/__init__.py
Outdated
22
    pass
23
try:
24
    from .dask import DaskLGBMClassifier, DaskLGBMRanker, DaskLGBMRegressor
25
except ImportError:
26
    pass