Skip to content

Incompatible types in assignment when a with block has inner method #16423

Open
@jayaseelan-james

Description

@jayaseelan-james

Bug Report

mypy reports an Incompatible types in assignment error when the variable name defined is same across "with" blocks and any of the "with" block contain an inner method. I hope this is on the lines of #15605 and a possible variant of #12246.

To Reproduce

Please use the below code:

with open('file1', 'r') as f:
    def do_something(x: int, y: int) -> int:
        return x + y
    
    do_something(2, 3)

with open('file2', 'rb') as f:
    pass

Expected Behavior

Type check passes.

Actual Behavior

I observed the following error:

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

Your Environment

  • Mypy version used: 1.6.1
  • Mypy command-line flags: None, just ran mypy main.py
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.9.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions