From c88d0c0512e6272077ed7ea86acc33c77a5cc36f Mon Sep 17 00:00:00 2001 From: Reza Mulia Wijaya <112234018+rezamuliawijaya@users.noreply.github.com> Date: Sat, 7 Nov 2026 14:39:24 +0700 Subject: [PATCH] Accept an overloaded __post_init__ when any item matches the dataclass call check_post_init ran once per FuncDef, so each overload item of an overloaded __post_init__ was checked on its own against the synthesized signature, reporting items the generated call never has to use (the dataclass passes the InitVar values once). The overload is now checked as a whole from its first item -- one compatible item is enough, the same rule an ordinary method override follows -- and the other items and the implementation skip the per-item check. --- mypy/plugins/dataclasses.py | 31 +++++++++++++++++++ test-data/unit/check-dataclasses.test | 44 +++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) 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