Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix [override] error with no line number when argument node has no line number #18122

Merged
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
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]
Loading