`NamedTuple` parametrized with a `ParamSpec` can't be used as an annotation
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
NamedTuple parametrized with a ParamSpec can't be used as an annotation.
It's probably easiest to look at my repro to see what I mean.
I know that generic support for NamedTuple is new-ish as of 3.11, but I couldn't find any documentation that says ParamSpec is somehow still unsupported.
To Reproduce
from collections.abc import Callable
from typing import NamedTuple
class T[**P](NamedTuple):
plugin: Callable[P, int]
def f[**P](a: T[P]) -> None:
pass
I note that if I change T from a NamedTuple to a dataclass, there's no error.
Expected Behavior
$ mypy t.py
Success: no issues found in 1 source file
Actual Behavior
$ mypy t.py
t.py:9: error: Invalid location for ParamSpec "P" [valid-type]
t.py:9: note: You can use ParamSpec as the first argument to Callable, e.g., "Callable[P, int]"
Found 1 error in 1 file (checked 1 source file)
The error is referring to the T[P] annotation.
Your Environment
- Mypy version used:
mypy 1.17.1 (compiled: yes) - Mypy command-line flags:
mypy - Mypy configuration options from
mypy.ini(and other config files): none - Python version used:
Python 3.13.5
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the supplied Python 3.13.5 reproducer with mypy 1.17.1 and inspect how the type checker validates the T[P] annotation for a generic NamedTuple. Done means the example reports no issues, while the existing invalid ParamSpec cases remain diagnosed and a regression test covers this behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100