python / python/mypy

Special-case .pop() on TypedDict

Open
#18,296 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Mypy doesn't respect TypedDict object changes. As an example, the following snippet

from typing import TypedDict

class A(TypedDict, total=False):
    a: str

def func(a: str) -> None:
    pass

data: A = {"a": "a"}
a = data.pop("a", "")
func(a, **data)

Raises the error:

error: "func" gets multiple values for keyword argument "a" [misc]

Formally, it is correctly, but it will be nice if mypy respects object keys changes. Especially in Unpack case:

from typing import Unpack, TypedDict

class A(TypedDict, total=False):
    a: str
    
def func(a: str) -> None:
    pass
    
def func2(**data: Unpack[A]) -> None:
    a = data.pop("a", "")
    func(a, **data) 

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 reproducing the two TypedDict examples with mypy, focusing on .pop() followed by keyword unpacking and Unpack handling. Trace the existing TypedDict key-tracking and duplicate-key diagnostic paths; done means valid code using the removed key no longer reports a duplicate keyword argument while invalid cases remain diagnosed.

Written by the indexing model from the issue text.

Assessment

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