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] replace uses of scipy.sparse.issparse() with isinstance() #5784

Merged
merged 1 commit into from
Mar 16, 2023

Conversation

jameslamb
Copy link
Collaborator

Contributes to #3756.
Contributes to #3867.

Fixes the following mypy errors.

python-package/lightgbm/basic.py:2753: error: Value of type "Union[str, Path, ndarray[Any, Any], Any, Any, Any, Sequence, List[Sequence], List[ndarray[Any, Any]]]" is not indexable  [index]
python-package/lightgbm/basic.py:2753: error: No overload variant of "__getitem__" of "list" matches argument type "Tuple[List[int], slice]"  [call-overload]
python-package/lightgbm/basic.py:2753: note: Possible overload variants:
python-package/lightgbm/basic.py:2753: note:     def __getitem__(self, SupportsIndex, /) -> Sequence
python-package/lightgbm/basic.py:2753: note:     def __getitem__(self, slice, /) -> List[Sequence]
python-package/lightgbm/basic.py:2753: note:     def __getitem__(self, SupportsIndex, /) -> ndarray[Any, Any]
python-package/lightgbm/basic.py:2753: note:     def __getitem__(self, slice, /) -> List[ndarray[Any, Any]]
python-package/lightgbm/basic.py:2753: error: Invalid index type "Tuple[List[int], slice]" for "Union[str, Path, ndarray[Any, Any], Any, Any, Any, Sequence, List[Sequence], List[ndarray[Any, Any]]]"; expected type "Union[SupportsIndex, slice]"  [index]
python-package/lightgbm/basic.py:2753: error: Invalid index type "Tuple[List[int], slice]" for "Union[str, Path, ndarray[Any, Any], Any, Any, Any, Sequence, List[Sequence], List[ndarray[Any, Any]]]"; expected type "Union[int, slice, List[int]]"  [index]
python-package/lightgbm/basic.py:2905: error: Item "str" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "toarray"  [union-attr]
python-package/lightgbm/basic.py:2905: error: Item "Path" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "toarray"  [union-attr]
python-package/lightgbm/basic.py:2905: error: Item "Sequence" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "toarray"  [union-attr]
python-package/lightgbm/basic.py:2905: error: Item "List[Sequence]" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "toarray"  [union-attr]
python-package/lightgbm/basic.py:2905: error: Item "List[ndarray[Any, Any]]" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "toarray"  [union-attr]
python-package/lightgbm/basic.py:2913: error: Item "str" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "getformat"  [union-attr]
python-package/lightgbm/basic.py:2913: error: Item "Path" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "getformat"  [union-attr]
python-package/lightgbm/basic.py:2913: error: Item "Sequence" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "getformat"  [union-attr]
python-package/lightgbm/basic.py:2913: error: Item "List[Sequence]" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "getformat"  [union-attr]
python-package/lightgbm/basic.py:2913: error: Item "List[ndarray[Any, Any]]" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "getformat"  [union-attr]
python-package/lightgbm/basic.py:2931: error: Item "str" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "toarray"  [union-attr]
python-package/lightgbm/basic.py:2931: error: Item "Path" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "toarray"  [union-attr]
python-package/lightgbm/basic.py:2931: error: Item "Sequence" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "toarray"  [union-attr]
python-package/lightgbm/basic.py:2931: error: Item "List[Sequence]" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "toarray"  [union-attr]
python-package/lightgbm/basic.py:2931: error: Item "List[ndarray[Any, Any]]" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "toarray"  [union-attr]
python-package/lightgbm/basic.py:2945: error: Item "str" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "toarray"  [union-attr]
python-package/lightgbm/basic.py:2945: error: Item "Path" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "toarray"  [union-attr]
python-package/lightgbm/basic.py:2945: error: Item "Sequence" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "toarray"  [union-attr]
python-package/lightgbm/basic.py:2945: error: Item "List[Sequence]" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "toarray"  [union-attr]
python-package/lightgbm/basic.py:2945: error: Item "List[ndarray[Any, Any]]" of "Union[str, Any, Path, Sequence, List[Sequence], List[ndarray[Any, Any]]]" has no attribute "toarray"  [union-attr]

Proposes replacing calls like this:

if scipy.sparse.issparse(data): ...

with this:

if isinstance(data, scipy.sparse.spmatrix): ...

This helps mypy to understand branches of logic specific to sparse matrices.

It also protects lightgbm users a bit from future changes to scipy's public API.

We aren't getting much from scipy.sparse.issparse().

It's an alias for scipy.sparse.isspmatrix() (code link, docs link), which just runs isinstance(x, spmatrix) (code link).

There is also a recent, ongoing discussion in scipy about possibly changing how these functions handle sparse array (non-matrix) types: scipy/scipy#15790.

@jameslamb jameslamb requested a review from guolinke March 15, 2023 04:50
@jameslamb jameslamb changed the title [python-package] replace uses of scipy.issparse() with isinstance() [python-package] replace uses of scipy.sparse.issparse() with isinstance() Mar 15, 2023
@jameslamb jameslamb merged commit 1787892 into master Mar 16, 2023
@jameslamb jameslamb deleted the ci/mypy-errors branch March 16, 2023 14:11
@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 15, 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