Files
ruff/crates/ty_python_semantic/resources/lint_docs/incompatible-method-override.md
Brent Westbrook e4208ef9f9 Rename rule source files to match current rule names (#30661)
## Summary

As noted on #30660, this should be the actual final entry in the rename
stack, doing some internal cleanup on file, module, and function names
not covered by the previous rename PRs, which focused on the rule
structs themselves.

As the last PR in the stack, this closes #27011, which in turn closes
#1773.
2026-11-24 10:19:22 -05:00

4.1 KiB

What it does

Detects method overrides that violate the Liskov Substitution Principle ("LSP").

The LSP states that an instance of a subtype should be substitutable for an instance of its supertype. Applied to Python, this means:

  1. All argument combinations a superclass method accepts must also be accepted by an overriding subclass method.
  2. The return type of an overriding subclass method must be a subtype of the return type of the superclass method.

An otherwise compatible override that renames a positional-or-keyword parameter or makes it positional-only can trigger lost-keyword-for-regular-parameter.

Why is this bad?

Violating the Liskov Substitution Principle will lead to many of ty's assumptions and inferences being incorrect, which will mean that it will fail to catch many possible type errors in your code.

Example

class Super:
    def method(self, x) -> int:
        return 42


class Sub(Super):
    # Liskov violation: `str` is not a subtype of `int`,
    # but the supertype method promises to return an `int`.
    def method(self, x) -> str:  # error: [incompatible-method-override]
        return "foo"


def accepts_super(s: Super) -> int:
    return s.method(x=42)


# The result of this call is a string, but ty will infer it to be an `int`
# due to the violation of the Liskov Substitution Principle.
accepts_super(Sub())

Common issues

Why does ty complain about my __eq__ method?

__eq__ and __ne__ methods in Python are generally expected to accept arbitrary objects as their second argument, for example:

class A:
    x: int

    def __eq__(self, other: object) -> bool:
        # gracefully handle an object of an unexpected type
        # without raising an exception
        if not isinstance(other, A):
            return False
        return self.x == other.x

If A.__eq__ here were annotated as only accepting A instances for its second argument, it would imply that you wouldn't be able to use == between instances of A and instances of unrelated classes without an exception possibly being raised. While some classes in Python do indeed behave this way, the strongly held convention is that it should be avoided wherever possible. As part of this check, therefore, ty enforces that __eq__ and __ne__ methods accept object as their second argument.

Why does ty disagree with Ruff about how to write my method?

Ruff has several rules that will encourage you to rename a parameter, or change its type signature, if it thinks you're falling into a certain anti-pattern. For example, Ruff's ARG002 rule recommends that an unused parameter should either be removed or renamed to start with _. Renaming the parameter in an override can trigger lost-keyword-for-regular-parameter if the base method accepts it by keyword. Removing the parameter can trigger incompatible-method-override if the override no longer accepts a call allowed by the base method.

This can usually be resolved by adding @typing.override to your method definition. Ruff knows that a method decorated with @typing.override is intended to override a method by the same name on a superclass, and avoids reporting rules like ARG002 for such methods; it knows that the changes recommended by ARG002 would violate the Liskov Substitution Principle.

Correct use of @override is enforced by ty's invalid-explicit-override rule.

Rule history

  • v0.0.100: ty used to have a rule called invalid-method-override that covered both incompatible-method-override and lost-keyword-for-regular-parameter.

    The old invalid-method-override name is now an alias for incompatible-method-override only. If you previously had any configuration for invalid-method-override specifically, you should go through lost-keyword-for-regular-parameter to see if you want to configure it in the same way.