Other people have mentioned that the self-referential case from this post is doable. But what is not doable (to my knowledge) is this kind of self-reference: class Node(typing.NamedTuple): # or dataclass child: 'Node' The error is example.py:4: error: Recursive types not fully supported yet, nested types replaced with "Any" . This error has been in there for several years at least. And it always bites me when I forge…
from __future__ import annotations
from typing import Optional, NamedTuple
from dataclasses import dataclass
class Foo:
def __init__(self, foo: Foo = None) -> None:
self.child: Optional[Foo] = foo
Foo(Foo(Foo(None))) # Works
Foo(Foo(Foo(1))) # error: Argument 1 to "Foo" has incompatible type "int"; expected "Optional[Foo]"
@dataclass
class Bar:
child: Optional[Bar]
Bar(Bar(Bar(None))) # Works
Bar(Bar(Bar('bar'))) # error: Argument 1 to "Bar" has incompatible type "str"; expected "Optional[Bar]"
class Hep(NamedTuple): # error: Recursive types not fully supported yet, nested types replaced with "Any"
child: Optional[Hep]
Hep(Hep(Hep(None))) # Works
Hep(Hep(Hep({'a': 'b'}))) # error: Argument 1 to "Hep" has incompatible type "Dict[str, str]"; expected "Optional[Hep]"