mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Variable redefinition (--allow-redefinition) not working inside with-block

Open bradst opened this issue 7 years ago • 1 comments

  • Are you reporting a bug, or opening a feature request? Bug

  • Please insert below the code you are checking with mypy, or a mock-up repro if the source is private. We would appreciate if you try to simplify your case to a minimal repro.

with open('file.txt') as f:
    number = '123'
    number = int(number)
  • What is the actual behavior/output?
$ mypy --allow-redefinition test.py
test.py:3: error: Incompatible types in assignment (expression has type "int", variable has type "str")
  • What is the behavior/output you expect? No errors; number's type should be redefined to int.

  • What are the versions of mypy and Python you are using? Do you see the same issue after installing mypy from Git master? mypy 0.710+dev.f10528a5a8d9e98939ceb84d40e614d1f159d9a0 (master), Python 3.7.3

  • What are the mypy flags you are using? (For example --strict-optional) --allow-redefinition

  • Note: no errors are thrown after removing the with-block:

f = open('file.txt')
number = '123'
number = int(number)

bradst avatar Apr 19 '19 23:04 bradst

Hm, I think this should be allowed. It is also prohibited in try ... except blocks, but that makes sense because "non-linear" flow is expected there, while exceptions are rarely "swallowed" (or even expected) in with blocks.

Anyway, assigning @JukkaL for final decision.

ilevkivskyi avatar Apr 19 '19 23:04 ilevkivskyi