python / python/mypy

False positive errors with anonymous and inline declaration of namedtuple types

Open
#16,660 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

Mypy raises false positive errors with anonymous and inline declaration of namedtuple types.

To Reproduce

from collections import namedtuple

dim = namedtuple("Dimension", ["x", "y"])(2, 3)

This should be equivalent to the following, which mypy gets correct:

from collections import namedtuple

Dimension = namedtuple("Dimension", ["x", "y"])
dim = Dimension(2, 3)

(Example taken from here: https://stackoverflow.com/a/26405216)

Expected Behavior

No error raised.

Actual Behavior

$ mypy namedtuple.py 
namedtuple.py:3: error: Too many arguments for "tuple"  [call-arg]
namedtuple.py:3: error: Argument 1 to "tuple" has incompatible type "int"; expected "Iterable[Any]"  [arg-type]
Found 2 errors in 1 file (checked 1 source file)

My Environment

$ mypy --version
mypy 1.7.1 (compiled: yes)
$ python --version
Python 3.11.6

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 reproducing the report with namedtuple.py and the inline anonymous declaration shown in the issue, then compare it with the separate Dimension declaration. Trace mypy's handling of collections.namedtuple calls. Done means the inline form produces no false-positive errors while the existing separate-declaration example remains correct.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.