Skip to content

dataclass.replace(obj, **dict) produces spurious errors #19219

Closed as duplicate
@kbauer

Description

@kbauer

Bug Report

When using dataclasses.replace with **dict syntax, the argument is compared against all fields, producing spurious errors.

To Reproduce

See also https://mypy-play.net/?mypy=master&python=3.12&gist=5f9e333caa33da7a378c572b253bd973

import dataclasses

@dataclasses.dataclass
class X:
    a: int = 1
    b: str = "foo"
    c: None = None
    d: int = 2
    e: str = "bar"
    f: None = None

x = X()
x = dataclasses.replace(x, **{"y": 10})

Expected Behavior

It should be accepted. This construct would be expected to undermine type checking, but not to be rejected outright (or for a different reason).

Actual Behavior

main.py:14: error: Argument 2 to "replace" of "X" has incompatible type "**dict[str, int]"; expected "str"  [arg-type]
main.py:14: error: Argument 2 to "replace" of "X" has incompatible type "**dict[str, int]"; expected "None"  [arg-type]
Found 2 errors in 1 file (checked 1 source file)

Note that the error messages are even contradicting each other.

Your Environment

  • Mypy version used: mypy 1.16.0 (compiled: yes)
  • Mypy command-line flags: None.
  • Mypy configuration options from mypy.ini (and other config files): None created.
  • Python version used: Python 3.12.7

Reloaded reports

#19193 might be related, but it is different enough to probably be a different issue, that just happens to affect the same function.

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

    Issue actions