Skip to content

[python-package] fix mypy error about pandas categorical features - #6253

Merged
jameslamb merged 2 commits into
masterfrom
mypy/pandas-cat-features
Jan 3, 2024
Merged

jameslamb merged 2 commits into
masterfrom
mypy/pandas-cat-features

Conversation

@jameslamb

Copy link
Copy Markdown
Member

Contributes to #3756.
Contributes to #3867.

Fixes the following error from mypy:

python-package/lightgbm/basic.py:824: error:
Incompatible return value type (got "tuple[ndarray[Any, Any], list[str], list[str] | list[int], list[list[Any]]]", expected "tuple[ndarray[Any, Any], list[str], list[str], list[list[Any]]]")  [return-value]

categorical_feature: _LGBM_CategoricalFeatureConfiguration,
pandas_categorical: Optional[List[List]]
) -> Tuple[np.ndarray, List[str], List[str], List[List]]:
) -> Tuple[np.ndarray, List[str], Union[List[str], List[int]], List[List]]:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hint was wrong.

categorical_feature can be a list of integers (indicating 0-based column positions).

https://github.com/microsoft/LightGBM/blob/b74528f3f7b5b2731920afb09ebd41e8f52fc976/python-package/lightgbm/basic.py#L1724-L1726

And it's passed back out of this function unmodified if it's provided like that.

https://github.com/microsoft/LightGBM/blob/b74528f3f7b5b2731920afb09ebd41e8f52fc976/python-package/lightgbm/basic.py#L814-L817

if categorical_feature == 'auto':
categorical_feature = cat_cols_not_ordered
else: # use cat cols specified by user
categorical_feature = list(categorical_feature) # type: ignore[assignment]

@jameslamb jameslamb Dec 30, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

categorical_feature can only be a list of strings, a list of integers, or the literal string 'auto'.

So I don't think this else: clause is helpful. When it's reached, under correctly-provided inputs it's just calling list() on something that's already a list, which has no effect.

I guess this could be providing a small bit of help for cases where some other type like a tuple was passed in, but if we want to support that then:

  • it should be done at all public interfaces that support categorical_feature
  • it shouldn't be buried inside a pandas-specific function

I think this can and should be removed, in the interest of further simplifying the flow of pandas data through the library.

@jameslamb
jameslamb marked this pull request as ready for review December 30, 2023 04:55
@jameslamb jameslamb changed the title WIP: [python-package] fix mypy error about pandas categorical features [python-package] fix mypy error about pandas categorical features Dec 30, 2023
@jameslamb
jameslamb merged commit 48e3629 into master Jan 3, 2024
@jameslamb
jameslamb deleted the mypy/pandas-cat-features branch January 3, 2024 16:21
@github-actions

github-actions Bot commented Jan 9, 2025

Copy link
Copy Markdown
Contributor

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 Jan 9, 2025
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