Skip to content

Not respecting isinstance for type narrowing using boolean short circuit #16373

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

Closed
john-dicarlo opened this issue Oct 30, 2023 · 1 comment
Closed
Labels
bug mypy got something wrong

Comments

@john-dicarlo
Copy link

# example.py

foo: list[str | int] = [0,1,2,3,'strings', 'as', 'well', 'here', 'bar', 'baz']

for i in range(len(foo)-1):
    if (isinstance(foo[i+1], str) and foo[i+1].upper() == 'BAR'): #  error: Item "int" of "str | int" has no attribute "upper"  [union-attr]
        print('FOOBAR')

I figure this type test is simple enough that mypy should know foo[i+1] must be a str.

Your Environment

mypy 1.6.1 (compiled: yes)
Python 3.10.12

mypy --strict ~/example.py

@john-dicarlo john-dicarlo added the bug mypy got something wrong label Oct 30, 2023
@ilevkivskyi
Copy link
Member

Duplicate of #7339

@ilevkivskyi ilevkivskyi marked this as a duplicate of #7339 Nov 1, 2023
@ilevkivskyi ilevkivskyi closed this as not planned Won't fix, can't repro, duplicate, stale Nov 1, 2023
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