python / python/mypy

final TypedDict with total=False does not allow clear/popitem

Open
#14,914 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

PEP-589 disallows clear and popitem on TypedDicts, even if all known keys are not required, as there may be other required keys due to structural subtyping. However, a @final-decorated TypedDict cannot be structurally subtyped, so these methods should be available.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=7d42af8206cb1e25a53673bdf4e99df1

from typing import TypedDict, final

@final
class A(TypedDict, total=False):
    k: int

a: A = {"k": 1}
a.clear()

Expected Behavior

No errors

Actual Behavior

error: "A" has no attribute "clear"  [attr-defined]

Your Environment

  • Mypy version used: 1.0.0
  • Python version used: 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 with the linked mypy-play reproduction using Python 3.11 and mypy 1.0.0, then inspect how the type checker handles clear and popitem on a final TypedDict with total=False. Done means the example produces no errors while the existing restrictions for structurally subtypeable TypedDicts remain intact.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.