Skip to content

False positive in conditional with with statements #15605

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

Open
freewilll opened this issue Jul 6, 2023 · 1 comment
Open

False positive in conditional with with statements #15605

freewilll opened this issue Jul 6, 2023 · 1 comment
Labels
bug mypy got something wrong

Comments

@freewilll
Copy link

freewilll commented Jul 6, 2023

Opening files with the same variable name in with statements in if statements fails type checks if the type of the result of the with statement is different. In this example, the difference is opening a file with 'rb' vs 'r'. Looks like this might be a variation of #12246.

To Reproduce

from pathlib import Path

def check_something() -> bool:
    return False

path = Path("foo")

if check_something():
    with path.open("rb") as f:
        pass
else:
    with path.open("r") as f:
        pass

Expected Behavior

Type check passes.

Actual Behavior

test.py:16: error: Incompatible types in assignment (expression has type "TextIOWrapper", variable has type "BufferedReader")  [assignment]

Environment

  • Mypy version used: 1.4.1
  • Mypy command-line flags: None, I ran mypy test.py
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11.4
@freewilll freewilll added the bug mypy got something wrong label Jul 6, 2023
@wessven
Copy link

wessven commented Sep 13, 2023

Getting the same on Mypy v1.5.1 and Python 3.10.12.

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