Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Special-case .pop() on TypedDict #18296

Open
Lancetnik opened this issue Dec 15, 2024 · 2 comments
Open

Special-case .pop() on TypedDict #18296

Lancetnik opened this issue Dec 15, 2024 · 2 comments
Labels

Comments

@Lancetnik
Copy link

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) 
@Lancetnik Lancetnik added the bug mypy got something wrong label Dec 15, 2024
@JelleZijlstra JelleZijlstra added feature and removed bug mypy got something wrong labels Dec 16, 2024
@JelleZijlstra JelleZijlstra changed the title Bug: ignore TypedDict key changes Special-case .pop() on TypedDict Dec 16, 2024
@JelleZijlstra
Copy link
Member

This would require special-casing .pop() to change the type of the TypedDict to some custom type. Not impossible to do, but it would require significant complexity so it's unlikely we'll add this.

@Lancetnik
Copy link
Author

I expected, but had to try 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants