python / python/mypy

`NamedTuple`'s `_tuplegetter` is not recognized

Open
#19,414 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

mypy parses NamedTuple class definition in the same way it parses regular class variables:

from typing import NamedTuple
from typing import reveal_type


class A(NamedTuple):
    foo: str = "foo"


reveal_type(A.foo)
a = "_" + A.foo
mypy t.py

t.py:9: note: Revealed type is "builtins.str"
Success: no issues found in 1 source file
python3 t.py

Runtime type is '_tuplegetter'
Traceback (most recent call last):
  File "D:\workspace\client\t.py", line 10, in <module>
    a = "_" + A.foo
        ~~~~^~~~~~~
TypeError: can only concatenate str (not "collections._tuplegetter") to str
mypy --version && python3 --version

mypy 1.16.1 (compiled: yes)
Python 3.13.5

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 reported NamedTuple example with mypy 1.16.1 and Python 3.13.5, then trace how the NamedTuple class definition and A.foo are analyzed. The issue is done when mypy recognizes the runtime _tuplegetter behavior and reports the mismatch or otherwise prevents the unsound concatenation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.