-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
TYP: np.ndarray.tolist
return type seems broken in numpy 2.2.0
#27944
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
Comments
np.ndarray.tolist
return type seems broken in numpy 2.2np.ndarray.tolist
return type seems broken in numpy 2.2.0
To give a bit more context, this is how @overload
def tolist(self: _HasShapeAndSupportsItem[tuple[()], _T], /) -> _T: ...
@overload
def tolist(self: _HasShapeAndSupportsItem[tuple[int], _T], /) -> list[_T]: ...
@overload
def tolist(self: _HasShapeAndSupportsItem[tuple[int, int], _T], /) -> list[list[_T]]: ...
@overload
def tolist(self: _HasShapeAndSupportsItem[tuple[int, int, int], _T], /) -> list[list[list[_T]]]: ...
@overload
def tolist(self: _HasShapeAndSupportsItem[Any, _T], /) -> _T | list[_T] | list[list[_T]] | list[list[list[Any]]]: ... So without a know shape-type, the last overload should be used. And without a known dtype (bound to Any | list[Any] | list[list[Any]] | list[list[list[Any]]] And pyright actually infers this correctly, and accepts your example, as it should. So I'm afraid that I can't help you here, and that this is a bug within mypy. |
Understood,. I may try to migrate to pyright, but for now I'll just put a lid on it. Thanks for your swift reply ! |
Is anyone else seeing this with |
Could you share the exact code? |
Sure, here's an MRE: import pandas as pd
df = pd.DataFrame(
["2024-01-01", "2022-11-12", "2019-08-27", "2024-05-11", "2022-02-28"],
columns=["date"],
)
df["date"] = pd.to_datetime(df["date"])
unique_years = df["date"].dt.year.unique()
print(f"unique_years type: {type(unique_years)}")
unique_years_list: list[int] = unique_years.tolist()
print(f"unique_years_list type: {type(unique_years_list)}")
print(f"unique_years_list[0] type: {type(unique_years_list[0])}") This outputs:
But
Versions:
|
In your example @ishaan-mehta, from typing import Any, reveal_type
import numpy as np
a: np.ndarray[Any, Any]
reveal_type(a.tolist()) mypy: The original post was about But that being said, solving one issue will probably solve the other. And to be honest, I was too quick in dismissing this as a "mypy-only" issue. So I'm reopening this, and I'll try my best to find a solution in the coming days. |
Thanks for isolating the issue and looking into it @jorenham! (And I appreciate you accommodating the semi-related topic in the same issue.) |
Thanks @neutrinoceros and @ishaan-mehta for reporting this! The fix is included in the latest 2.2.5 release. |
Describe the issue:
While upgrading CMasher's CI to numpy 2.2.0, I got a new typechecking error that seems spurious to me. No error is raised with numpy 2.1.3
Reproduce the code example:
Error message:
Python and NumPy Versions:
Type-checker version and settings:
no custom settings
Additional typing packages.
Included with mypy, but not used here (as far as I'm aware)
The text was updated successfully, but these errors were encountered: