Skip to content

self-referential NamedTuple leads to spurious "incompatible types" with List[<nothing>] #3731

Closed
@carljm

Description

@carljm

This code typechecks cleanly:

from typing import List, NamedTuple

class Person(NamedTuple):
    name: str
    age: int
 
people: List[Person] = []

But add a self-referential attribute to the NamedTuple:

from typing import List, NamedTuple

class Person(NamedTuple):
    name: str
    age: int
    kids: List['Person']
 
people: List[Person] = []

And now we get:

namedtuple.py:9: error: Incompatible types in assignment (expression has type List[<nothing>], variable has type List[Person])

If Person is an ordinary class instead of a NamedTuple, the error doesn't appear.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions