Skip to content

Arg-type warning calling dataclasses.replace() with **dict for keyword arguments. #15994

Closed as not planned
@flaviovs

Description

@flaviovs

Bug Report

Calling dataclasses.replace() using a dict for dynamic arguments issues arg-type warnings.

To Reproduce

from dataclasses import dataclass, replace

@dataclass
class Foo:
    bar: str
    zee: int

obj1 = Foo(bar='abc', zee=1)

kwargs = {'bar': 'xyz', 'zee': 9}
obj2 = replace(obj1, **kwargs)

print(obj1)
print(obj2)

Program output:

$ python /tmp/test.py 
Foo(bar='abc', zee=1)
Foo(bar='xyz', zee=9)

Expected Behavior

No warnings from mypy.

Actual Behavior

$ mypy /tmp/test.py 
/tmp/test.py:13: error: Argument 2 to "replace" of "Foo" has incompatible type "**dict[str, object]"; expected "str"  [arg-type]
/tmp/test.py:13: error: Argument 2 to "replace" of "Foo" has incompatible type "**dict[str, object]"; expected "int"  [arg-type]

$ mypy --version
mypy 1.5.1 (compiled: yes)

Apparently Mypy is trying to match each dict element type to class attributes (which is good), but not considering the fact that the 2nd parameter of replace() is a var-keyword therefore **dict is perfectly fine.

Your Environment

$ python --version
Python 3.9.2

$ mypy --version
mypy 1.5.1 (compiled: yes)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions