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

BUG: Incorrect typing when using array directly verses assignment #22470

Open
bashtage opened this issue Oct 23, 2022 · 1 comment
Open

BUG: Incorrect typing when using array directly verses assignment #22470

bashtage opened this issue Oct 23, 2022 · 1 comment

Comments

@bashtage
Copy link
Contributor

Describe the issue:

The type revealed by mypy depends on whether the array is assigned to a variable or not. This may very well be a mypy bug.

Reproduce the code example:

import numpy.typing as npt
import numpy as np

from typing import overload

@overload
def f(x: npt.NDArray[np.int64]) -> npt.NDArray[np.int64]:
    ...
@overload
def f(x: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]:
    ...

def f(x):
    return x


a = np.array([1, 2, 3], dtype=np.int64)
reveal_type(a)
reveal_type(f(a))
reveal_type(f(np.array([1, 2, 3], dtype=np.int64)))

b = np.array([1, 2, 3], dtype=np.float64)
reveal_type(b)
reveal_type(f(b))
# This final one returns int64 not float64
reveal_type(f(np.array([1, 2, 3], dtype=np.float64)))

Error message:

blah.py:18: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy._typing._64Bit]]]"
blah.py:19: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy._typing._64Bit]]]"
blah.py:20: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy._typing._64Bit]]]"
blah.py:23: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy._typing._64Bit]]]"
blah.py:24: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy._typing._64Bit]]]"
blah.py:25: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy._typing._64Bit]]]"

NumPy/Python version information:

1.23.4 3.9.12 (main, Apr 4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)]

Context for the issue:

The final 3 revealed types should be "numpy.ndarray[Any, numpy.dtype[numpy.floating[numpy._typing._64Bit]]]", but for some reason the final is "numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy._typing._64Bit]]]" instead.

@BvB93
Copy link
Member

BvB93 commented May 16, 2023

Yeah, this definitely sounds like a mypy bug. I've got the suspicion that mypy chokes somewhere and ends up with a Any-based dtype, one that passes through the first overload by simply using that as a default.

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

No branches or pull requests

3 participants