Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion mypy/plugins/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
JsonDict,
NameExpr,
Node,
OverloadedFuncDef,
PlaceholderNode,
RefExpr,
Statement,
Expand Down Expand Up @@ -62,6 +63,7 @@
Instance,
LiteralType,
NoneType,
Overloaded,
ProperType,
SentinelValue,
TupleType,
Expand Down Expand Up @@ -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
Expand All @@ -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__",
Expand Down
43 changes: 43 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading