Skip to content
Merged
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
3 changes: 2 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,8 @@ def check_func_def(
# TODO: check recursively for inner type variables
if (
arg_type.variance == COVARIANT
and defn.name not in ("__init__", "__new__", "__post_init__")
and defn.name
not in {"__init__", "__new__", "__post_init__", "__replace__"}
and not is_private(defn.name) # private methods are not inherited
and (i != 0 or not found_self)
):
Expand Down
17 changes: 17 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -2580,6 +2580,23 @@ Gen(2).__replace__(x="not an int") # E: Argument "x" to "__replace__" of "Gen"

[builtins fixtures/tuple.pyi]

[case testDunderReplaceAllowsCovariantDataclassField]
# flags: --python-version 3.13
from dataclasses import dataclass
from typing import Generic, TypeVar

T_co = TypeVar("T_co", covariant=True)

@dataclass(frozen=True)
class A(Generic[T_co]):
value: T_co

class B(Generic[T_co]):
def __replace__(self, *, value: T_co) -> B[T_co]:
return self

[builtins fixtures/tuple.pyi]

[case testDunderReplaceCovariantOverride]
# flags: --python-version 3.13 --enable-error-code mutable-override
from dataclasses import dataclass
Expand Down
Loading