diff --git a/mypy/plugins/dataclasses.py b/mypy/plugins/dataclasses.py index 7a6a3876d9bfd..140dfcd0a6992 100644 --- a/mypy/plugins/dataclasses.py +++ b/mypy/plugins/dataclasses.py @@ -33,6 +33,7 @@ JsonDict, NameExpr, Node, + OverloadedFuncDef, PlaceholderNode, RefExpr, Statement, @@ -62,6 +63,7 @@ Instance, LiteralType, NoneType, + Overloaded, ProperType, SentinelValue, TupleType, @@ -1135,6 +1137,35 @@ def check_post_init(api: TypeChecker, defn: FuncItem, info: TypeInfo) -> None: assert isinstance(ideal_sig, CallableType) ideal_sig = ideal_sig.copy_modified(name="__post_init__") + # An overloaded __post_init__ reaches this hook once per item, each time + # carrying only that item's signature. Checking an item on its own would + # report variants the generated call never has to use: the dataclass + # passes the InitVar values once, and the overload is fine when any one + # item accepts them -- the same rule an ordinary method override follows. + # So the overload is checked as a whole, once, from its first item; the + # other items and the implementation fall through without checking. A + # single-signature __post_init__ is checked directly as before. + sym = info.names.get("__post_init__") + if isinstance(sym, SymbolTableNode) and isinstance(sym.node, OverloadedFuncDef): + overload_type = sym.node.type + first_item = ( + sym.node.items[0].func + if sym.node.items and isinstance(sym.node.items[0], Decorator) + else None + ) + if isinstance(overload_type, Overloaded) and first_item is defn: + api.check_override( + override=overload_type, + original=ideal_sig, + name="__post_init__", + name_in_super="__post_init__", + supertype="dataclass", + original_class_or_static=False, + override_class_or_static=False, + node=defn, + ) + return + api.check_override( override=defn.type, original=ideal_sig, diff --git a/test-data/unit/check-dataclasses.test b/test-data/unit/check-dataclasses.test index d269ad2bad0ec..c639efcddc6bd 100644 --- a/test-data/unit/check-dataclasses.test +++ b/test-data/unit/check-dataclasses.test @@ -2439,6 +2439,50 @@ class Test8: def __post_init__(self, s: Self) -> None: ... [builtins fixtures/dataclasses.pyi] +[case testPostInitOverloadWithCompatibleItem] +from dataclasses import dataclass, InitVar +from typing import overload + +# One compatible overload item is enough: the dataclass calls __post_init__ +# with the InitVar values, and an overloaded override is fine when the +# overload as a whole accepts that call, as with an ordinary override. +@dataclass +class Record: + value: InitVar[int] + + @overload + def __post_init__(self, value: int) -> None: ... + @overload + def __post_init__(self, value: str) -> None: ... + def __post_init__(self, value: int | str) -> None: ... + +Record(1) +[builtins fixtures/dataclasses.pyi] + +[case testPostInitOverloadWithoutCompatibleItem] +from dataclasses import dataclass, InitVar +from typing import overload + +@dataclass +class Record: + value: InitVar[int] + + @overload + def __post_init__(self, value: str) -> None: ... # E: Signature of "__post_init__" incompatible with supertype "dataclass" \ + # N: Superclass: \ + # N: def __post_init__(self: Record, value: int) -> None \ + # N: Subclass: \ + # N: @overload \ + # N: def __post_init__(self: Record, value: str) -> None \ + # N: @overload \ + # N: def __post_init__(self: Record, value: bytes) -> None + @overload + def __post_init__(self, value: bytes) -> None: ... + def __post_init__(self, value: str | bytes) -> None: ... + +Record(1) +[builtins fixtures/dataclasses.pyi] + [case testPostInitSubclassing] from dataclasses import dataclass, InitVar