Skip to content

Binder fails to track loops correctly #18348

Closed
@hauntsaninja

Description

@hauntsaninja
# mypy: enable-error-code=redundant-expr
from __future__ import annotations

class Node:
    parent: Node | None
    is_valid: bool

    def traverse_needs_fixing(self) -> Node | None:
        it: Node | None
        it = self
        while it is not None and it.is_valid:  # E: Left operand of "and" is always true  [redundant-expr]
            it = it.parent
        return it

    def traverse(self) -> Node | None:
        # this only works because mypy does not narrow on initial assignment
        # which is something we'd like to change, see #2008
        it: Node | None = self
        while it is not None and it.is_valid:
            it = it.parent
        return it

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions