python / python/mypy

NamedTuple single line declaration gives error "Too many arguments for NamedTuple() [misc]

Open
#11,047 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug semantic-analyzer topic-named-tuple
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

typing.NamedTuple one line declaration gives incorrect error

To Reproduce

# test.py
from typing import NamedTuple

ExampleClass = NamedTuple('ExampleClass', test_id=str, timestamp=str)
mypy --show-error-codes test.py

test.py:[line-no]: error: Too many arguments for NamedTuple()  [misc]

Expected Behavior

mypy doesn't give this error

given the above syntax is valid python and works fine, I would expect it to not throw an error, like the following old structure declaration does not, which is labeled as "back-ward compatible usage" in the official doc

ExampleClass = NamedTuple('ExampleClass', [(test_id, str), (timestamp, str)])

Actual Behavior

error: Too many arguments for NamedTuple()  [misc]

Your Environment

mypy --version
mypy 0.910

python --version
3.8.10

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the reported test.py example with mypy --show-error-codes and inspect the NamedTuple call handling. Done means the keyword-based one-line declaration no longer produces the erroneous "Too many arguments for NamedTuple()" error, while the existing list-based declaration remains accepted; add a regression test if the repository's test location is identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.