Skip to content

Commit

Permalink
Only use argument node as context if line information is present
Browse files Browse the repository at this point in the history
  • Loading branch information
brianschubert committed Nov 7, 2024
1 parent 7788c21 commit 679fba6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 4 additions & 3 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]

0 comments on commit 679fba6

Please sign in to comment.