Skip to content

erroneous complaint during tree walk #18608

Closed as not planned
Closed as not planned
@cjw296

Description

@cjw296

Bug Report

@hauntsaninja suggested I raise this.

To Reproduce

from dataclasses import dataclass
from typing import TypeVar, Generic, Any

T = TypeVar('T')

@dataclass()
class Element(Generic[T]):
    value: T
    parent: "Element[Any] | None" = None


    def walk(self) -> None:
        current: Element[Any] | None = self
        while current := current.parent:
            print(current.value)

Expected Behavior

$ pyright --version
pyright 1.1.393
$ pyright type_bug.py 
0 errors, 0 warnings, 0 informations 

Actual Behavior

$ mypy --version
mypy 1.14.1 (compiled: yes)
$ mypy type_bug.py 
type_bug.py:14: error: Item "None" of "Element[Any] | None" has no attribute "parent"  [union-attr]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.14.1
  • Python version used: Python 3.12.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions