diff --git a/mypy/checker.py b/mypy/checker.py index 46200f5813cc..8a735e04678e 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -2067,7 +2067,13 @@ def erase_override(t: Type) -> Type: if not is_subtype( original.arg_types[i], erase_override(override.arg_types[i]) ): + arg_type_in_super = original.arg_types[i] + + if isinstance(node, FuncDef): + context: Context = node.arguments[i + len(override.bound_args)] + else: + context = node self.msg.argument_incompatible_with_supertype( i + 1, name, @@ -2075,7 +2081,7 @@ def erase_override(t: Type) -> Type: name_in_super, arg_type_in_super, supertype, - node, + context, ) emitted_msg = True diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 209ebb89f36b..c7f464c7497f 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -1081,7 +1081,9 @@ def make_argument( if argument_elide_name(arg.arg): pos_only = True - return Argument(Var(arg.arg), arg_type, self.visit(default), kind, pos_only) + argument = Argument(Var(arg.arg), arg_type, self.visit(default), kind, pos_only) + argument.set_line(arg.lineno, arg.col_offset, arg.end_lineno, arg.end_col_offset) + return argument def fail_arg(self, msg: str, arg: ast3.arg) -> None: self.fail(msg, arg.lineno, arg.col_offset) diff --git a/mypy/nodes.py b/mypy/nodes.py index 38639d553b3d..8aa8073902b4 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -704,18 +704,6 @@ def __init__( def max_fixed_argc(self) -> int: return self.max_pos - def set_line( - self, - target: Context | int, - column: int | None = None, - end_line: int | None = None, - end_column: int | None = None, - ) -> None: - super().set_line(target, column, end_line, end_column) - for arg in self.arguments: - # TODO: set arguments line/column to their precise locations. - arg.set_line(self.line, self.column, self.end_line, end_column) - def is_dynamic(self) -> bool: return self.type is None diff --git a/test-data/unit/check-abstract.test b/test-data/unit/check-abstract.test index 98be314b9c27..566bb92d6e18 100644 --- a/test-data/unit/check-abstract.test +++ b/test-data/unit/check-abstract.test @@ -382,10 +382,10 @@ class A(I): def g(self, a: 'A') -> 'A': return A() [out] +main:11: error: Return type "I" of "h" incompatible with return type "A" in supertype "I" main:11: error: Argument 1 of "h" is incompatible with supertype "I"; supertype defines the argument type as "I" main:11: note: This violates the Liskov substitution principle main:11: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides -main:11: error: Return type "I" of "h" incompatible with return type "A" in supertype "I" -- Accessing abstract members diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test index f1af13923fd7..aadd4fa2c68a 100644 --- a/test-data/unit/check-classes.test +++ b/test-data/unit/check-classes.test @@ -2626,10 +2626,10 @@ class D(A): def __iadd__(self, x: 'A') -> 'B': pass [out] main:6: error: Return type "A" of "__iadd__" incompatible with return type "B" in "__add__" of supertype "A" +main:8: error: Signatures of "__iadd__" and "__add__" are incompatible main:8: error: Argument 1 of "__iadd__" is incompatible with "__add__" of supertype "A"; supertype defines the argument type as "A" main:8: note: This violates the Liskov substitution principle main:8: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides -main:8: error: Signatures of "__iadd__" and "__add__" are incompatible [case testGetattribute] diff --git a/test-data/unit/check-columns.test b/test-data/unit/check-columns.test index 6748646b65aa..9691e6565689 100644 --- a/test-data/unit/check-columns.test +++ b/test-data/unit/check-columns.test @@ -238,9 +238,9 @@ if int(): class A: def f(self, x: int) -> None: pass class B(A): - def f(self, x: str) -> None: pass # E:5: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "int" \ - # N:5: This violates the Liskov substitution principle \ - # N:5: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides + def f(self, x: str) -> None: pass # E:17: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "int" \ + # N:17: This violates the Liskov substitution principle \ + # N:17: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides class C(A): def f(self, x: int) -> int: pass # E:5: Return type "int" of "f" incompatible with return type "None" in supertype "A" class D(A): diff --git a/test-data/unit/fine-grained-inspect.test b/test-data/unit/fine-grained-inspect.test index 8574477d8272..2c575ec365b1 100644 --- a/test-data/unit/fine-grained-inspect.test +++ b/test-data/unit/fine-grained-inspect.test @@ -189,7 +189,7 @@ def foo(arg: T) -> T: return arg [out] == -foo.py:7:1:arg +foo.py:7:9:arg foo.py:4:5:x [case testInspectTypeVarValuesDef] @@ -219,7 +219,7 @@ class C(Generic[T]): [out] == foo.py:5:5:z, tmp/foo.py:9:5:z -foo.py:12:1:arg +foo.py:12:9:arg foo.py:5:5:z, tmp/foo.py:9:5:z [case testInspectModuleAttrs] @@ -266,4 +266,4 @@ def foo(arg: int) -> int: [out] == -4:12:4:14 -> tmp/foo.py:1:1:arg +4:12:4:14 -> tmp/foo.py:1:9:arg