You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TYP: np.ndarray.tolist return type seems broken in numpy 2.2.0 #27944
changed the title [-]TYP: `np.ndarray.tolist` return type seems broken in numpy 2.2[/-][+]TYP: `np.ndarray.tolist` return type seems broken in numpy 2.2.0[/+]on Dec 9, 2024
And pyright actually infers this correctly, and accepts your example, as it should.
But you're using mypy, and mypy is very broken, which this case is an example of, and causes it to infer _T as str. That makes no sense at all, which can only mean that it's one of the >1200 mypy bugs that aren't in pyright.
So I'm afraid that I can't help you here, and that this is a bug within mypy.
In your example @ishaan-mehta, unique_years is inferred as ndarray[Unknown, Unknown]. This is effectively the same as ndarray[Any, Any]. In this case, both pyright and mypy will indeed infer str as the return type of .tolist(). Here's a minimal .pyi example that shows this:
mypy: Revealed type is "builtins.str"
pyright: Type of "a.tolist()" is "str"
The original post was about NDArray[Any], which reduces to ndarray[Any, dtype[Any]]. It's a subtle difference, but it seems to leads to different behavior in pyright. Mypy's behaviour is the same on both situations. So technically, this is a different issue.
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.
Activity
[-]TYP: `np.ndarray.tolist` return type seems broken in numpy 2.2[/-][+]TYP: `np.ndarray.tolist` return type seems broken in numpy 2.2.0[/+]jorenham commentedon Dec 10, 2024
data_arr = np.array(data)is inferred asNDArray[Any], which is technically correct, but far from ideal.To give a bit more context, this is how
ndarray.tolistis annotated:So without a know shape-type, the last overload should be used. And without a known dtype (bound to
_There), the correct return type should beAnd pyright actually infers this correctly, and accepts your example, as it should.
But you're using mypy, and mypy is very broken, which this case is an example of, and causes it to infer
_Tasstr. That makes no sense at all, which can only mean that it's one of the >1200 mypy bugs that aren't in pyright.So I'm afraid that I can't help you here, and that this is a bug within mypy.
neutrinoceros commentedon Dec 10, 2024
Understood,. I may try to migrate to pyright, but for now I'll just put a lid on it. Thanks for your swift reply !
ishaan-mehta commentedon Apr 2, 2025
Is anyone else seeing this with
pyrighttoo? I am.jorenham commentedon Apr 2, 2025
Could you share the exact code?
ishaan-mehta commentedon Apr 3, 2025
Sure, here's an MRE:
This outputs:
But
reveal_type(unique_years.tolist())with Pylance says that it is astr. Same with rawpyrightinstead of Pylance:Versions:
numpy==2.2.4pyright==1.1.398jorenham commentedon Apr 4, 2025
In your example @ishaan-mehta,
unique_yearsis inferred asndarray[Unknown, Unknown]. This is effectively the same asndarray[Any, Any]. In this case, both pyright and mypy will indeed inferstras the return type of.tolist(). Here's a minimal.pyiexample that shows this:mypy:
Revealed type is "builtins.str"pyright:
Type of "a.tolist()" is "str"The original post was about
NDArray[Any], which reduces tondarray[Any, dtype[Any]]. It's a subtle difference, but it seems to leads to different behavior in pyright. Mypy's behaviour is the same on both situations. So technically, this is a different issue.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.
ishaan-mehta commentedon Apr 4, 2025
Thanks for isolating the issue and looking into it @jorenham! (And I appreciate you accommodating the semi-related topic in the same issue.)
jorenham commentedon Apr 20, 2025
Thanks @neutrinoceros and @ishaan-mehta for reporting this! The fix is included in the latest 2.2.5 release.