* [python-package] fix plot_importance() crash when model has 0 splits
* address review: use load_breast_cancer + min_data_in_bin
---------
Co-authored-by: Juan Flores <juanf-0gravity@users.noreply.github.com>
Co-authored-by: James Lamb <jaylamb20@gmail.com>
The y-coordinate passed to matplotlib's ax.text() was a numpy signedinteger from
np.arange(), but matplotlib's type stubs expect a Python float. This cast follows
the existing pattern in engine.py where float() is used to convert numpy values.
Co-authored-by: James Lamb <jaylamb20@gmail.com>
* [python-package] Add return type annotations to predict methods in sklearn module
Add return type annotations to LGBMModel.predict, LGBMClassifier.predict, and LGBMClassifier.predict_proba methods. The predict_proba method requires an isinstance assertion for type narrowing in the binary classification branch, since predict() returns a union type but only returns np.ndarray when pred_contrib=False. This follows the existing error message pattern used in _get_label_from_constructed_dataset and similar helper functions.
Fixes: https://github.com/microsoft/LightGBM/issues/3867
* [python-package] Add shared _LGBM_PredictReturnType type alias for predict methods
Add _LGBM_PredictReturnType type alias in basic.py and use it in _InnerPredictor.predict(),
Booster.predict(), LGBMModel.predict(), LGBMClassifier.predict(), and LGBMClassifier.predict_proba().
Also add assert isinstance() with error message in predict_proba() for type narrowing when
returning binary classification results.
This keeps the return type DRY and ensures consistency across the codebase.
* Update python-package/lightgbm/basic.py
---------
Co-authored-by: James Lamb <jaylamb20@gmail.com>
* [python-package] add Literal type annotation for order variable in _np2d_to_np1d
The order variable was inferred as str but numpy.asarray expects Literal['K', 'A', 'C', 'F']. Added explicit Literal["C", "F"] type annotation before the conditional assignment. This resolves the mypy call-overload error for np.asarray at line 201.
[python-package] Add DTypeLike annotation to fix dtype assignment type in _np2d_to_np1d
Add type annotation for dtype variable to resolve mypy assignment error.
This follows the existing pattern used elsewhere in basic.py (lines 337, 364, 463, 800, 2795).
Co-authored-by: James Lamb <jaylamb20@gmail.com>
The numpy stubs define arange with positional-only parameters so keyword
arguments start=, stop=, step= do not match any overload. Changed to
positional arguments np.arange(_MAX_INT32, nrow, _MAX_INT32) which matches
the numpy stubs correctly and resolves the mypy call-overload errors.
Co-authored-by: James Lamb <jaylamb20@gmail.com>
Change _is_pyarrow_table() return type from bool to TypeGuard[pa_Table] to enable mypy type narrowing at all call sites. This follows the existing pattern used by _is_pyarrow_array() and allows mypy to recognize data.column_names as valid when extracting feature names from PyArrow tables.
Fixes: https://github.com/microsoft/LightGBM/issues/3867
Co-authored-by: James Lamb <jaylamb20@gmail.com>
* [docs] [python-package] document how to run Python tests
* refer to installation instructions
---------
Co-authored-by: Nikita Titov <nekit94-08@mail.ru>
* Add "PT" ruff code to pyproject.toml plus some exceptions
* Extend PT011 for Exception, IndexError, TypeError
* Fix PT001: Use `@pytest.fixture` over `@pytest.fixture()`
* Fix PT011: pytest.raises({exception}) is too broad, set the match parameter or use a more specific exception
* Fix PT012: `pytest.raises()` block should contain a single simple statement
* Fix PT017: Found assertion on exception {name} in except block, use pytest.raises() instead
* Fix PT018: Assertion should be broken down into multiple parts
* Remove PT006 and PT007 from ignore list and fixup
* Use *Exception and *Error glob to apply PT011 and ignore AssertionErrors
---------
Co-authored-by: Nikita Titov <nekit94-08@mail.ru>
* [python-package] Add superfluous-else-raise rule to ruff lint configuration
* [python-package] Fix code to follow superfluous-else-raise rule
* [python-package] Move RET506 after RET504 to maintain alphabetical order and add source rule set comment
* [python-package] Revert unintended change to preserve original formatting
* [python-package] Test serialization and deserialization from in-memory string
Test case for #6851
* [python-package] Fill in `params` when loading from in-memory string
This fixes#6851 by using the same workaround as when loading the model
from a file.
* test_basic: use rng instead of legacy numpy RandomState
* test_basic: remove debug prints leftovers
Co-authored-by: James Lamb <jaylamb20@gmail.com>
* test_basic: add boolean, array of float and array of integers to testcase
Co-authored-by: James Lamb <jaylamb20@gmail.com>
* test_basic: make a cheaper model (2 rounds with 7 leaves each)
Co-authored-by: James Lamb <jaylamb20@gmail.com>
* test_basic: bugfix typos
* python_package_test: move string load test from basic to engine
* test_engine: catch params ignored warnings
* test_engine: be explicit about parameters assertion
* test_engine: shush linter complaint
* test_basic: delete empty line
Co-authored-by: James Lamb <jaylamb20@gmail.com>
* test_engine: even cheaper model with less features
Co-authored-by: James Lamb <jaylamb20@gmail.com>
* test_engine: delete redundant assert
Co-authored-by: James Lamb <jaylamb20@gmail.com>
* test_engine: run pre-commit and take it's word for it
* test_engine: be explicit in an E712 compliant way
* test_engine.py: pass different value as argument to make sure it is ignored
---------
Co-authored-by: James Lamb <jaylamb20@gmail.com>
Co-authored-by: Nikita Titov <nekit94-08@mail.ru>