Skip to content

Support the functional syntax for NamedTuples #1049

@AlexWaygood

Description

@AlexWaygood

Ty now supports most features of NamedTuples defined via the class-based syntax (#545). However, we do not yet have full support for namedtuples defined using the function-call syntax. I suggest that we hold off on this until work on #742 is complete, as some refactoring that needs to be done for that issue will likely be helpful for this issue.

We should add suppport for these ways of defining a namedtuple:

from collections import namedtuple
from typing import NamedTuple

a = namedtuple("a", "a b c")
b = namedtuple("b", "a, b, c")
c = namedtuple("c", ["a", "b", "c"])
d = namedtuple("d", ("a", "b", "c"))

e = NamedTuple("e", [("a", int), ("b", str)])
f = NamedTuple("f", (("a", int), ("b", str)))

The first four should all be inferred as being NamedTuple classes that inherit from tuple[Unknown, Unknown, Unknown]; the last two should both be inferred as being NamedTuple classes that inherit from tuple[int, str].

There are some additional rules and restrictions about the functional syntax that are described at the bottom of https://typing.python.org/en/latest/spec/namedtuples.html#defining-named-tuples and at https://docs.python.org/3/library/collections.html#collections.namedtuple

Metadata

Metadata

Assignees

No one assigned

    Labels

    typing semanticstyping-module features, spec compliance, etc

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions