Thanks to visit codestin.com
Credit goes to github.com

Skip to content

False positive: Statement is [unreachable] in loop with iteration-carried flag #19849

@sMiNT0S

Description

@sMiNT0S

Bug Report

mypy reports Statement is unreachable in a loop that uses a “skip next line” flag across iterations. The flagged branch is reachable in the next iteration, but mypy appears to analyze it as if the flag can’t be True.

To Reproduce

from __future__ import annotations

import re

_HDR_BY_CMD = re.compile(r"^\s*#\s*by the following command:\s*$", re.I)
_HDR_CMD    = re.compile(r"^\s*#\s*pip-compile\b", re.I)
_ANY_VIA    = re.compile(r"^\s*#\s*via\b.*$", re.I)

def norm(lines: list[str]) -> list[str]:
    out: list[str] = []
    skip_next = False
    for raw in lines:
        ln = raw.rstrip()
        if skip_next and _HDR_CMD.match(ln):
            skip_next = False
            continue
        if _HDR_BY_CMD.match(ln):
            skip_next = True
            continue
        if _ANY_VIA.match(ln):
            out.append("# via")
        else:
            out.append(ln)
    return out

Command:
mypy --show-error-codes --warn-unreachable demo.py

Expected Behavior
No unreachable error; the first if is reachable on iterations after _HDR_BY_CMD matched.

Actual Behavior
demo.py:20: error: Statement is unreachable [unreachable]
Found 1 error in 1 file (checked 1 source file)

Environment

mypy 1.18.1 (compiled: yes)

Python version used: 3.13.7

OS: Windows 11 (Git Bash / PowerShell 7.5.3)

Command-line flags: --warn-unreachable --show-error-codes

Config
[tool.mypy]
python_version = "3.13"
warn_unreachable = true
strict_optional = false

Project repo error encountered in
https://github.com/sMiNT0S/AIBugBench/tree/main ;
sMiNT0S/AIBugBench#18
(specifically, 'update_requirements_lock.py' lines 200 ~224)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions