Skip to content

[1.15 regression] (Temporary) false positive for reachability with unannotated partial type #18606

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

Open
hauntsaninja opened this issue Feb 5, 2025 · 1 comment · May be fixed by #19118
Open
Assignees
Labels
bug mypy got something wrong

Comments

@hauntsaninja
Copy link
Collaborator

Sort of a weird / minor case, but with mypy 1.15

λ cat e.py
def merge_debugs(debugs: list[int | None]) -> None:
    metadata = None
    for debug in debugs:
        if debug is not None:
            if metadata is None:
                metadata = {}

λ mypy  e.py --warn-unreachable
e.py:6: error: Need type annotation for "metadata" (hint: "metadata: dict[<type>, <type>] = ...")  [var-annotated]
e.py:6: error: Statement is unreachable  [unreachable]
Found 2 errors in 1 file (checked 1 source file)

If you add an annotation to metadata (standalone or with the assignment) the unreachable warning goes away

@hauntsaninja hauntsaninja added the bug mypy got something wrong label Feb 5, 2025
@hauntsaninja hauntsaninja changed the title False positive for reachability with unannotated partial type (Temporary) false positive for reachability with unannotated partial type Feb 5, 2025
@tyralla tyralla self-assigned this Feb 5, 2025
@Garrett-R
Copy link

Another example:

y = None
for i in range(5):
    if y is None:
        print('reachable!')  # Mypy: error: Statement is unreachable
        y = []
    y.append(99)

(playground)

@hauntsaninja hauntsaninja changed the title (Temporary) false positive for reachability with unannotated partial type [1.15 regression] (Temporary) false positive for reachability with unannotated partial type Feb 12, 2025
tyralla added a commit to tyralla/mypy that referenced this issue May 20, 2025
… robustly and efficiently, and avoid multiple `revealed type` notes for the same line.

This change is an improvement over 9685171.  Besides fixing the regression reported in python#18606 and removing the duplicates reported in python#18511, it should significantly reduce the performance regression reported in python#18991.  At least running `Measure-command {python runtests.py self}` on my computer (with removed cache) is 10 % faster.
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
3 participants