Skip to content

warn-unreachable false-positive when method updates attribute after assert #11969

Open
@jwodder

Description

@jwodder

Consider the following code:

from dataclasses import dataclass


@dataclass
class Foo:
    value: int
    modified: bool = False

    def update(self) -> None:
        self.value += 1
        self.modified = True


foo = Foo(42)
assert not foo.modified
foo.update()
assert foo.modified
print("Reached")

When this is run, the print("Reached") line is reached, but mypy doesn't seem to realize that. Running mypy --warn-unreachable --pretty on the above code gives:

unreachable03.py:18: error: Statement is unreachable
    print("Reached")
    ^
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: both v0.931 and the GitHub version as of commit 48d810d
  • Mypy command-line flags: --warn-unreachable --pretty
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.9.9
  • Operating system and version: macOS 11.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-reachabilityDetecting unreachable codetopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions