Skip to content

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

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

Closed
kbauer opened this issue Jun 3, 2025 · 2 comments
Closed

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

kbauer opened this issue Jun 3, 2025 · 2 comments
Labels
bug mypy got something wrong

Comments

@kbauer
Copy link

kbauer commented Jun 3, 2025

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.

@kbauer kbauer added the bug mypy got something wrong label Jun 3, 2025
@sterliakov
Copy link
Collaborator

I guess it's #5382 that just happens to manifest on dataclasses.replace?

@hauntsaninja
Copy link
Collaborator

Yes, I think so.

We could potentially add special casing for dict literals, but usually reporters actually care about some more complex use case

Closing as duplicate

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

No branches or pull requests

3 participants