Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
aploium opened this issue Jul 26, 2022 · 1 comment
Labels
bug mypy got something wrong

Comments

@aploium
Copy link

aploium commented Jul 26, 2022

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 aploium added the bug mypy got something wrong label Jul 26, 2022
@tmke8
Copy link
Contributor

tmke8 commented Jul 30, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants