Skip to content

Inconsistent representations for named tuples #3016

Closed
@JukkaL

Description

@JukkaL

I have these two modules that form an import cycle:

# a.py
from collections import namedtuple
from b import f

N = namedtuple('N', 'a')

class X(N):
    pass
# b.py
import a

def f(x: a.X) -> None:
    reveal_type(x) # a.X
    y = a.X(1)
    reveal_type(y) # Tuple[Any, fallback=a.X]

The revealed types for two values with the same type a.X differ depending on how the type was constructed (see the comments above). The explanation is that when mypy processes the type annotation of f, is doesn't yet know that it is a named tuple type. The type of a.X(1) is determined later during type checking, and mypy then knows that it's a named tuple.

Not sure what's the best way to fix this. We could perhaps fix up named tuple types in the third pass of semantic analysis when this knowledge is available.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions