diff --git a/mypy/checker.py b/mypy/checker.py index a650bdf2a639..1bee348bc252 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -2367,10 +2367,11 @@ def erase_override(t: Type) -> Type: else: continue if not is_subtype(original_arg_type, erase_override(override_arg_type)): + context: Context = node if isinstance(node, FuncDef) and not node.is_property: - context: Context = node.arguments[i + len(override.bound_args)] - else: - context = node + arg_node = node.arguments[i + len(override.bound_args)] + if arg_node.line != -1: + context = arg_node self.msg.argument_incompatible_with_supertype( i + 1, name, diff --git a/test-data/unit/check-dataclasses.test b/test-data/unit/check-dataclasses.test index 0f726242b25b..294612db7ea5 100644 --- a/test-data/unit/check-dataclasses.test +++ b/test-data/unit/check-dataclasses.test @@ -2523,3 +2523,18 @@ reveal_type(replaced_2) # N: Revealed type is "__main__.Gen[builtins.int]" Gen(2).__replace__(x="not an int") # E: Argument "x" to "__replace__" of "Gen" has incompatible type "str"; expected "int" [builtins fixtures/tuple.pyi] + +[case testDunderReplaceCovariantOverride] +# flags: --python-version 3.13 +from dataclasses import dataclass + +@dataclass +class Base: + a: object + +@dataclass +class Child(Base): # E: Argument 1 of "__replace__" is incompatible with supertype "Base"; supertype defines the argument type as "object" \ + # N: This violates the Liskov substitution principle \ + # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides + a: int +[builtins fixtures/tuple.pyi]