Skip to content

Missing diagnostics when parsing NamedTuple() and namedtuple() calls #2513

@AlexWaygood

Description

@AlexWaygood

All of the following calls are clearly invalid, but ty currently doesn't emit any diagnostics for them:

import collections
from typing import NamedTuple

Y = collections.namedtuple("Y")

Foo = NamedTuple("Foo")
Bar = NamedTuple("Bar", ["a", "b"])
Baz = NamedTuple("Baz", [("foo", 123), ("bar", 123)])
Spam = NamedTuple("Spam", "definitely not valid")

I think for typing.NamedTuple we should in fact have very strict parsing. The only reason why you'd use typing.NamedTuple over collections.namedtuple is because you want precise type inference from your type checker. So while I think we should continue to not emit an error any of these:

from collections import namedtuple

def f(*args, **kwargs):
    B = namedtuple("B", *args)
    C = namedtuple("C", "a b c", *args)
    D = namedtuple(*args, **kwargs)
    E = namedtuple("E", *args, **kwargs)
    F = namedtuple("F", "a b c", *args, **kwargs)

I think we should emit an error on all of these (we should ban variadic and keyword-variadic arguments entirely in calls):

from typing import NamedTuple

def f(*args, **kwargs):
    B = NamedTuple("B", *args)
    C = NamedTuple("C", [("a", int), ("b", str)], *args)
    D = NamedTuple(*args, **kwargs)
    E = NamedTuple("E", *args, **kwargs)
    F = NamedTuple("F", [("a", int), ("b", str)], *args, **kwargs)

Metadata

Metadata

Assignees

Labels

namedtuplesruntime semanticsAccurate modeling of how Python's semantics work at runtimetyping semanticstyping-module features, spec compliance, etc

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions