You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
mypy cannot detect class attribute change if assert but modified in external call
This is a case found when I'm trying to check the close status first, and after some logic, close it then.
The print(1) in f1() is reachable, but mypy think Statement is unreachable because we asserted assert not self._closed first, however, it was changed in the close() call
For the f2() we modify the self._close inplace, so mypy is happy with it.
I think it may be hard and not worth to fix, just reported to let you know. :)
To Reproduce
save the code and then run mypy --warn-unreachable assert_but_change.py
# assert_but_change.pyclassFoo:
def__init__(self) ->None:
self._closed=Falsedeff1(self) ->None:
assertnotself._closed# do some logicself.close()
ifself._closed:
print(1)
defclose(self) ->None:
self._closed=Truedeff2(self) ->None:
assertnotself._closedself._closed=Trueifself._closed:
print(1)
Expected Behavior
the print(1) in f1() is reachable, mypy should not complain
just like the print(1) in f2()
Actual Behavior
Statement is unreachable
Your Environment
Mypy version used: mypy 0.971
Mypy command-line flags: --warn-unreachable
Mypy configuration options from mypy.ini (and other config files): No
Python version used: cpython 3.9.4
Operating system and version: windows 10
The text was updated successfully, but these errors were encountered:
The problem is that there is a lot of code out there that relies on this pattern. But I believe that pyre-check has the behavior that you want. The downside of this is that you need a lot of Final annotations to make pyre happy.
Bug Report
mypy cannot detect class attribute change if assert but modified in external call
This is a case found when I'm trying to check the close status first, and after some logic, close it then.
The
print(1)
inf1()
is reachable, but mypy thinkStatement is unreachable
because we assertedassert not self._closed
first, however, it was changed in theclose()
callFor the
f2()
we modify theself._close
inplace, so mypy is happy with it.I think it may be hard and not worth to fix, just reported to let you know. :)
To Reproduce
save the code and then run
mypy --warn-unreachable assert_but_change.py
Expected Behavior
the
print(1)
inf1()
is reachable, mypy should not complainjust like the
print(1)
inf2()
Actual Behavior
Statement is unreachable
Your Environment
mypy.ini
(and other config files): NoThe text was updated successfully, but these errors were encountered: