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