from __future__ import annotations
import typing as t
class Node:
parent: Node | None = None
N = t.TypeVar("N", bound=Node)
def find_ancestor(node: Node, node_type: t.Type[N]) -> N | None:
ancestor = node.parent
while ancestor and not isinstance(ancestor, node_type):
ancestor = ancestor.parent
return ancestor
mypy 1.0.1 errors with:
example.py:13: error: Incompatible return value type (got "Optional[Node]", expected "Optional[N]") [return-value]
Would be great to narrow the type to N here and avoid the spurious error.
Pitch
Type narrowing in a while loop would improve inference. pyright already does this.
Feature
example.py
mypy 1.0.1 errors with:
Would be great to narrow the type to
N
here and avoid the spurious error.Pitch
Type narrowing in a while loop would improve inference. pyright already does this.