Description
Bug Report
I am not sure if this is a mypy bug or by design or maybe a missing feature, but at least looking at the runtime behavior it seems like a bug to me. I asked on gitter and one user responed that it seems to be a bug/oversight in the implementation.
I want to do converters with dataclasses, and the way to do this with dataclasses is to provide custom __init__
i.e. consider the following example.
To Reproduce
import dataclasses
@dataclasses.dataclass
class WrapsInt:
value: int
def __init__(self, value: int | str):
self.value = int(value)
wraps_int = WrapsInt("3")
print(repr(wraps_int.value))
wraps_int2 = dataclasses.replace(wraps_int, value="4")
print(repr(wraps_int2.value))
At runtime it will print and boths will be integers:
3
4
This is because dataclasses.replace always calls init. But mypy seems to take the type information from the class body only instead when analyzing dataclasses.replace call.
Expected Behavior
No error, mypy recognizes __init__
implementation when analyzing dataclass.replace
calls.
Actual Behavior
main.py:16: error: Argument "value" to "replace" of "WrapsInt" has incompatible type "str"; expected "int" [arg-type]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.15.0, master branch
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini
(and other config files): default - Python version used: 3.12