python / python/mypy

Why is the dict extension prohibited in the TypedDict constructor?

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

Nobody has claimed this yet.

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

Description

Bug Report

Cannot use dict expansion in TypedDict constructor. Why?

To Reproduce

from typing import TypedDict


class SomeTypedDict(TypedDict):
    field1: str
    field2: int
    field3: bool


SOME_SAMPLE = {
    'field1': 'str',
    'field2': 0,
    'field3': True,
}

some_variable = SomeTypedDict(**SOME_SAMPLE)
print(some_variable)

Actual Behavior

user@pc:~/$ mypy main.py 
main.py:16: error: Unsupported type "dict[str, object]" for ** expansion in TypedDict  [typeddict-item]
Found 1 error in 1 file (checked 1 source file)

Your Environment

user@pc:~/$ mypy --version 
mypy 1.8.0 (compiled: yes)
user@pc:~/$ python3 --version
Python 3.12.0

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

Reproduce the reported diagnostic from the provided main.py example with mypy 1.8.0 and inspect how TypedDict constructors handle ** expansion. No source file or test is named; done requires either a clear explanation of the restriction or a precisely scoped correction with regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.