Skip to content

NamedTuple parametrized with a ParamSpec can't be used as an annotation #19658

@sbrudenell

Description

@sbrudenell

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions