Skip to content

Unpacking a NamedTuple in a dictionary index loses type information #5630

Open

Description

@zshuzh

Describe the Bug

from typing import NamedTuple

class Key(NamedTuple):
   name: str
   version: int

def lookup(key: Key, values: dict[tuple[str, int, int], str]) -> None:
   values[*key, 0]                    # Rejected
   values[key.name, key.version, 0]  # Accepted

def plain_tuple(key: tuple[str, int], values: dict[tuple[str, int, int], str]) -> None:
   values[*key, 0]  # Accepted

The error for values[*key, 0] in lookup is:

ERROR Cannot index into `dict[tuple[str, int, int], str]` [bad-index] Argument `tuple[*tuple[int | str, ...], Literal[0]]` is not assignable to parameter `key` with type `tuple[str, int, int]` in function `dict.__getitem__`

I'd expect all three accesses to be accepted, particularly since unpacking an ordinary tuple already works.

Sandbox Link

Sandbox

(Only applicable for extension issues) IDE Information

No response

Activity

  1. ImBinarry commented on Nov 13, 2026

    @ImBinarry
    Contributor

    #claim

  2. github-actions commented on Nov 13, 2026

    @github-actions

    @ImBinarry you've claimed this issue — it's now assigned to you. Thanks for picking it up! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions