Skip to content

Inconsistent behaviour when updating a typed dict with a "subset" typeddict #19365

Open
@Alexandre-SCHOEPP

Description

@Alexandre-SCHOEPP

Context

I am handling configuration from various sources, including kwargs. To do this, I use the TypeDicts, and I got this unexpected error:

# minimum working example
from typing_extensions import TypedDict, NotRequired


class Config(TypedDict):
    a: str
    b: float


class UserOverride(TypedDict):
    a: NotRequired[str]


def get_default_config(user_override: UserOverride) -> Config:
    conf: Config = {
        "a": "hello world",
        "b": 1.
    }
    conf.update(user_override)
    conf.update(**user_override)  # Extra argument "a" for update of TypedDict [misc]
    return conf

The first update call does not raise any error, but the second, which is equivalent according to all python rules I known, does not function.

If mypy is enforcing a standard way of writing code (which may be the case), then the error message is horribly unhelpful, pointing me to go through my complex library to seek the various TypedDicts definitions and try and find an incompatibility that does not exist.

I also disagree with the assessment that one signature is better than the other. Pylance errors on the first update call, telling me that it does not match an overload (I am probably going to open an issue with them about it too).

System info

  • OS: Windows 11
  • Mypy: mypy 1.16.0 (compiled: yes)
  • Python: 3.12.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions