From 95bf287af29ec2b2d5ced8073153c222a03a213f Mon Sep 17 00:00:00 2001 From: zhengdaodeguL <1159957339@qq.com> Date: Tue, 17 Nov 2026 18:16:08 +0000 Subject: [PATCH] Accept overloaded __post_init__ when an item matches the dataclass call check_post_init ran once per overload item, so an item that does not take the InitVar type was reported as an incompatible override even when another item accepts what the dataclass passes. Check the overload as a whole once, from its first item, and fall back to the per-item check when no aggregate overload type exists. Fixes #22385 --- mypy/plugins/dataclasses.py | 16 +++++++++- test-data/unit/check-dataclasses.test | 43 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/mypy/plugins/dataclasses.py b/mypy/plugins/dataclasses.py index 7a6a3876d9bfd..82075cfff5b18 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, @@ -1128,6 +1130,18 @@ def check_post_init(api: TypeChecker, defn: FuncItem, info: TypeInfo) -> None: return assert isinstance(defn.type, FunctionLike) + override: FunctionLike = defn.type + post_init = info.names.get("__post_init__") + if post_init is not None and isinstance(post_init.node, OverloadedFuncDef): + overload = post_init.node + # check_func_item() runs once per item, so validate an overload as a whole. + if isinstance(overload.type, Overloaded) and overload.items: + first_item = overload.items[0] + first_func = first_item.func if isinstance(first_item, Decorator) else first_item + if defn is not first_func: + return + override = overload.type + ideal_sig_method = info.get_method(_INTERNAL_POST_INIT_SYM_NAME) assert ideal_sig_method is not None and ideal_sig_method.type is not None ideal_sig = ideal_sig_method.type @@ -1136,7 +1150,7 @@ def check_post_init(api: TypeChecker, defn: FuncItem, info: TypeInfo) -> None: ideal_sig = ideal_sig.copy_modified(name="__post_init__") api.check_override( - override=defn.type, + override=override, original=ideal_sig, name="__post_init__", name_in_super="__post_init__", diff --git a/test-data/unit/check-dataclasses.test b/test-data/unit/check-dataclasses.test index d269ad2bad0ec..58820e147c48f 100644 --- a/test-data/unit/check-dataclasses.test +++ b/test-data/unit/check-dataclasses.test @@ -2439,6 +2439,49 @@ class Test8: def __post_init__(self, s: Self) -> None: ... [builtins fixtures/dataclasses.pyi] +[case testPostInitOverloadCompatible] +from dataclasses import InitVar, dataclass +from typing import overload + +@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 testPostInitOverloadIncompatible] +from dataclasses import InitVar, dataclass +from typing import overload + +@dataclass +class Record: + value: InitVar[int] + + @overload + def __post_init__(self, value: str) -> None: ... + @overload + def __post_init__(self, value: bytes) -> None: ... + def __post_init__(self, value: str | bytes) -> None: ... + +Record(1) +[builtins fixtures/dataclasses.pyi] +[out] +main:9: error: Signature of "__post_init__" incompatible with supertype "dataclass" +main:9: note: Superclass: +main:9: note: def __post_init__(self: Record, value: int) -> None +main:9: note: Subclass: +main:9: note: @overload +main:9: note: def __post_init__(self: Record, value: str) -> None +main:9: note: @overload +main:9: note: def __post_init__(self: Record, value: bytes) -> None + [case testPostInitSubclassing] from dataclasses import dataclass, InitVar