Skip to content

Conversation

@wagner-austin
Copy link

@wagner-austin wagner-austin commented Dec 30, 2025

Summary

Add Literal type annotation for order variable in _np2d_to_np1d function to fix mypy call-overload error with np.asarray.

Mypy Error (before fix)

python-package/lightgbm/basic.py:201: error: No overload variant of "asarray" matches argument types "ndarray[tuple[Any, ...], dtype[Any]]", "dtype[Any]", "str"  [call-overload]

Related Issue

Fixes: #3867

Test Plan

  • Ran pre-commit run mypy --files python-package/lightgbm/basic.py
  • Verified mypy no longer reports call-overload error for line 201

…p2d_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.
dtype = mat.dtype
else:
dtype = np.float32
order: Literal["C", "F"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Have you tried running lightgbm's tests locally?

I think this is going to fail because typing.Literal is not imported at runtime, only type-checking time:

if TYPE_CHECKING:
from typing import Literal

You'll need to either move that import up alongside the other runtime ones or change this to deferred evaluation like this:

order: "Literal['C', 'F']"

Either is fine with me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ci] warnings from mypy

2 participants