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
31 changes: 31 additions & 0 deletions 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 @@ -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,
Expand Down
44 changes: 44 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading