# Needed to allow recursive type of Tree.child: Tree
from __future__ import annotations
class Tree:
child: Tree | None
def __init__(self, child: Tree | None):
self.child = child
root: Tree | None = Tree(child=Tree(child=Tree(child=None)))
if child := root:
print("there is a tree with children:")
while child := child.child: # <<< error here
print(child)
error: Item "None" of "Optional[Tree]" has no attribute "child" [union-attr]
error: Item "None" of "Union[Tree, Any, None]" has no attribute "child" [union-attr]
Your Environment
Mypy 0.991 on Python 3.10.9 without configuration file or command-line flags
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.11&gist=1b0d0e7878dbce6c88e64135eb31bbb6
Actual Behavior
Your Environment
Mypy 0.991 on Python 3.10.9 without configuration file or command-line flags