python / python/mypy

False Positive when TypedDict instance used as base __map for new instances?

Open
#16,995 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Why doesn't mypy accept another TypedDict as the __map argument when defining a new instance as shown below? It seems perfectly valid to me, but I could be missing something.

To Reproduce

Save the following as test_mypy.py and ask mypy to check it:

from typing import TypedDict


class MyTypedDict(TypedDict):
    attr1: str
    attr2: str
    attr3: str


MY_BASE_DICT = MyTypedDict(
    attr1="testing",
    attr2="typeddict",
    attr3="inheritance",
)

new_typed_dict = MyTypedDict(MY_BASE_DICT, attr1="failing")

assert " ".join(new_typed_dict.values()) == "failing typeddict inheritance"

Expected Behavior

> mypy test_mypy.py
Success: no issues found in 1 source file

Actual Behavior

> mypy test_mypy.py
test_mypy.py:17: error: Expected keyword arguments, {...}, or dict(...) in TypedDict constructor  [misc]
Found 1 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: v1.8.0 (command line call) and v1.6.1 (integrated vscode extension)
  • Mypy command-line flags: none required although I'm typically adding --check-untyped-defs --implicit-optional
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.10.6

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 report by saving the example as test_mypy.py and running mypy test_mypy.py. Trace the TypedDict constructor checking that rejects MY_BASE_DICT, then add a regression test for using a TypedDict instance as the base mapping while retaining keyword overrides. Done means the example passes without the [misc] error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.