Incompatible types in assignment when a with block has inner method
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running mypy main.py with the reproduced nested-method and repeated-with-variable example, using the reported mypy 1.6.1 behavior as a baseline. Trace how assignment types are inferred across with blocks containing inner methods; done means the example type-checks without the incompatible-assignment error and the behavior is covered by an appropriate regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100