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

Invalid "Value of type Never is not indexable" #16468

Closed
inducer opened this issue Nov 11, 2023 · 0 comments · Fixed by #16470
Closed

Invalid "Value of type Never is not indexable" #16468

inducer opened this issue Nov 11, 2023 · 0 comments · Fixed by #16470
Labels
bug mypy got something wrong

Comments

@inducer
Copy link

inducer commented Nov 11, 2023

To Reproduce

from typing import Union, Sequence


def f(pad_width: Union[int, Sequence[int]],) -> None:
    if (isinstance(pad_width, tuple)
          and len(pad_width) == 2
          and isinstance(pad_width[0], int)
          and isinstance(pad_width[1], int)
          ):
        pass

Expected Behavior

Type-checks fine.

Actual Behavior

mypy-bug.py:8: error: Value of type Never is not indexable  [index]
mypy-bug.py:9: error: Value of type Never is not indexable  [index]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.7.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12
@inducer inducer added the bug mypy got something wrong label Nov 11, 2023
hauntsaninja pushed a commit that referenced this issue Nov 12, 2023
Fixes #16468

The fix is straightforward.

Btw when fixing this I noticed that we disregard type arguments when
narrowing, for example:
```python
x: Sequence[int]
if isinstance(x, tuple):
    reveal_type(x)  # tuple[Any, ...], but should be `tuple[int, ...]`
```
I guess fixing this may be tricky, and it is quite old behavior.
JukkaL pushed a commit that referenced this issue Nov 22, 2023
Fixes #16468

The fix is straightforward.

Btw when fixing this I noticed that we disregard type arguments when
narrowing, for example:
```python
x: Sequence[int]
if isinstance(x, tuple):
    reveal_type(x)  # tuple[Any, ...], but should be `tuple[int, ...]`
```
I guess fixing this may be tricky, and it is quite old behavior.
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

Successfully merging a pull request may close this issue.

1 participant