Skip to content

Dataclasses fail to parse kwargs correctly #19172

Closed
@ms-pablo-ruiz

Description

@ms-pablo-ruiz

Bug Report

When using several dataclasses with common arguments is sometimes convenient to pass the arguments as a kwargs dict. but that will raise an error:

#error: Argument 1 to "Data" has incompatible type "**dict[str, object]";

To Reproduce
I put here a silly example where the **common dict may not be that useful, but keep in mind that this occurs in more complex scenarios where the dataclass for instnace may have 5 common arguments out of 7, and passing it like this it actually makes sense.

@dataclass
class Data:
    car_id: int
    car: str

Data(car_id=1, car="hola")
common = {"car_id":1, "car":"hola"}
Data(**common) #error: Argument 1 to "Data" has incompatible type "**dict[str, object]"; expected "str"

Expected Behavior

No error,
ofc talso in more complex variants of the same thing, as python accepts the arguments of it

Actual Behavior

#error: Argument 1 to "Data" has incompatible type "**dict[str, object]";

**Your Environment**

- Mypy version used: 1.15.0
- Mypy command-line flags: None
- Mypy configuration options from `mypy.ini` (and other config files): not much
- Python version used: 3.13

Activity

ms-pablo-ruiz

ms-pablo-ruiz commented on May 30, 2025

@ms-pablo-ruiz
Author

Actually after thinking it more, maybe is not a bug, just the dict not being narrow enough

I guess there is a work around if we define the dictionary explicitly:

class Common(TypedDict):
    test: int
    car: str

common: Common = {"test":1, "car":"hola"}
Data(**common) # no error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ms-pablo-ruiz

        Issue actions

          Dataclasses fail to parse kwargs correctly · Issue #19172 · python/mypy