Skip to content

Commit

Permalink
Check names of positional arguments for @OverRide
Browse files Browse the repository at this point in the history
  • Loading branch information
tmke8 committed May 8, 2023
1 parent 10cd4aa commit 94840af
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 5 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1971,6 +1971,9 @@ def check_method_override_for_base_with_name(
original_class_or_static,
override_class_or_static,
context,
ignore_pos_arg_names=(
not context.is_explicit_override if isinstance(context, FuncDef) else True
),
)
elif is_equivalent(original_type, typ):
# Assume invariance for a non-callable attribute here. Note
Expand Down Expand Up @@ -2058,6 +2061,7 @@ def check_override(
original_class_or_static: bool,
override_class_or_static: bool,
node: Context,
ignore_pos_arg_names: bool,
) -> None:
"""Check a method override with given signatures.
Expand All @@ -2071,7 +2075,7 @@ def check_override(
# Use boolean variable to clarify code.
fail = False
op_method_wider_note = False
if not is_subtype(override, original, ignore_pos_arg_names=True):
if not is_subtype(override, original, ignore_pos_arg_names=ignore_pos_arg_names):
fail = True
elif isinstance(override, Overloaded) and self.is_forward_op_method(name):
# Operator method overrides cannot extend the domain, as
Expand Down
8 changes: 5 additions & 3 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -2742,9 +2742,11 @@ class B(A):

class C(A):
@override
def f(self, x: str) -> str: pass # E: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "int" \
# N: This violates the Liskov substitution principle \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
def f(self, y: int) -> str: pass # E: Signature of "f" incompatible with supertype "A" \
# N: Superclass: \
# N: def f(self, x: int) -> str \
# N: Subclass: \
# N: def f(self, y: int) -> str
def g(self, x: int) -> str: pass

class D(A): pass
Expand Down

0 comments on commit 94840af

Please sign in to comment.