python / python/mypy

Allow TypeVarTuple as type argument for subclasses of generic TypedDict

Open
#16,975 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-pep-646 topic-typed-dict
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

I have a TypedDict class which has TypeVarTuple type argument(s), and that works as I would expect. However, when I try to make a class derived from that base class, I get unexpected errors from mypy. Different errors, actually, depending on whether I use Unpack or the new "star" unpacking syntax.

This all ought to work, though, right?

To Reproduce

# typeddict_typevartuple_310.py

from collections.abc import Callable
from typing import Generic, Tuple, TypedDict, TypeVar
from typing_extensions import TypeVarTuple, Unpack

ATs = TypeVarTuple('ATs')
RT = TypeVar('RT')


class BaseDict(TypedDict, Generic[Unpack[ATs], RT]):
    callback: Callable[[Unpack[ATs]], RT]
    arguments: Tuple[Unpack[ATs]]


class DerivedDict(BaseDict[Unpack[ATs], RT]):  # E: Unpack is only valid in a variadic position
    returned: RT

# typeddict_typevartuple_311.py

from collections.abc import Callable
from typing import Generic, TypedDict, TypeVar, TypeVarTuple

ATs = TypeVarTuple('ATs')
RT = TypeVar('RT')


class BaseDict(TypedDict, Generic[*ATs, RT]):
    callback: Callable[[*ATs], RT]
    arguments: tuple[*ATs]


class DerivedDict(BaseDict[*ATs, RT]):  # E: Invalid TypedDict type argument
    returned: RT

Expected Behavior

I would have expected mypy to accept both these versions as correct...

Actual Behavior

... But it fails both of them, with distinct messages (see inline comments).

Your Environment

  • Mypy version used: mypy 1.10.0+dev.3c87af272cbf7c49699b7508c7f51365da139c05 (compiled: no)
  • Mypy command-line flags: n/a
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.10 and 3.11

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 running the two reproductions, typeddict_typevartuple_310.py and typeddict_typevartuple_311.py, with the reported mypy behavior. Trace the TypedDict generic type-argument validation reached by these examples, then add focused coverage for both Unpack and star-unpacking forms. Done means mypy accepts both derived TypedDict definitions without the reported errors.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.