mypy icon indicating copy to clipboard operation
mypy copied to clipboard

mypy cannot detect class attribute change if asserted but modified in external call

Open aploium opened this issue 3 years ago • 1 comments

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) 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. :)

image

To Reproduce

save the code and then run mypy --warn-unreachable assert_but_change.py

# assert_but_change.py
class Foo:
    def __init__(self) -> None:
        self._closed = False

    def f1(self) -> None:
        assert not self._closed

        # do some logic
        self.close()

        if self._closed:
            print(1)

    def close(self) -> None:
        self._closed = True

    def f2(self) -> None:
        assert not self._closed
        self._closed = True

        if self._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

aploium avatar Jul 26 '22 15:07 aploium

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.

tmke8 avatar Jul 30 '22 14:07 tmke8