You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
overloaded __post_init__ in a dataclass: [override] error for an overload item that does not take the InitVar type #22385
For a dataclass with an InitVar, mypy checks the user's __post_init__ against the signature built from the InitVar fields (added in #15503). When __post_init__ is overloaded, every overload item is checked on its own, so an item that does not take the InitVar type is reported as an incompatible override even though another item does. The overloaded method as a whole accepts what the dataclass passes, and CPython runs the program.
$ mypy --python-version 3.13 test_bug.py
test_bug.py:12: error: Argument 2 of "__post_init__" is incompatible with supertype "dataclass"; supertype defines the argument type as "int" [override]
Found 1 error in 1 file (checked 1 source file)
$ python test_bug.py
<class 'int'>
The error is on the str item (line 12). The int item and the implementation (int | str) are not reported.
Expected Behavior
No error. The generated call is __post_init__(value) with an int, which the first overload accepts. For an ordinary method, mypy already accepts an overloaded override of a non-overloaded method when one item is compatible:
mypy test_control.py prints Success: no issues found. If no item takes the InitVar type (for example overloads for str and bytes with InitVar[int]), an error is right, and mypy reports both items.
I read checker.py on master: check_func_def calls dataclasses_plugin.check_post_init(self, defn, defn.info) for a function named __post_init__, and check_post_init passes defn.type to check_override. For an overloaded method defn is a single item (the line of the error above) and not the Overloaded type, which would explain the difference to the ordinary override. I did not debug it further.
Activity