Closed as not planned
Closed as not planned
Description
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)