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

Optional NumPy array not narrowed in max key #16416

Closed
fuglede opened this issue Nov 6, 2023 · 1 comment
Closed

Optional NumPy array not narrowed in max key #16416

fuglede opened this issue Nov 6, 2023 · 1 comment
Labels
bug mypy got something wrong

Comments

@fuglede
Copy link

fuglede commented Nov 6, 2023

Bug Report

Here's an example where I'm allowed to index a NumPy array after narrowing its otherwise optional type when I do so directly, but not when I use it as part of a key for max .

To Reproduce

https://gist.github.com/mypy-play/beca56edcf56c561ebfb37147d31c2ed

That is,

def f(a: Optional[npt.NDArray[np.int64]]):
    if a is None:
        a = np.repeat(1, 5).astype(np.int64)
    print(a[0])
    print(max([0], key=lambda x: a[x]))

Expected Behavior

I wouldn't have expected a[x] to be problematic, since we know a is not None at this point. MyPy itself seems to be aware since the preceding line, containing an a[0], does not cause any errors.

Actual Behavior

main.py:11: error: Value of type "Any | None" is not indexable [index]

Your Environment

  • Mypy version used: 1.6.1
  • Mypy command-line flags: None.
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11.5
@fuglede fuglede added the bug mypy got something wrong label Nov 6, 2023
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Nov 6, 2023

Thanks, this is fixed on master in #16407

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants