ty
ty copied to clipboard
Improve `invalid-method-override` diagnostics for overloaded methods
Currently for this file:
from typing import overload
class A:
@overload
def f(self) -> int: ...
@overload
def f(self, x: str) -> str: ...
@overload
def f(self, x: str, y: str) -> bytes: ...
def f(self, x="foo", y="bar"):
raise NotImplementedError
class B(A):
@overload
def f(self) -> int: ...
@overload
def f(self, y: str) -> str: ...
@overload
def f(self, x: str, y: str) -> bytes: ...
def f(self, x="foo", y="bar"):
raise NotImplementedError
we emit this diagnostic:
which is extremely confusing, because from the code frame we show you'd think that the subclass method has exactly the same signature as the superclass method.
The issue here is that the second overload of the subclass method has a different parameter name to the second overload of the superclass method. Ideally we'd say exactly that, but that's very hard without https://github.com/astral-sh/ty/issues/163. In the meantime we should just print the first N overloads as part of the diagnostic, the same as mypy does, and the same as we do for other diagnostics such as no-matching-overload.