Description
Currently mypy doesn't check that a method override has keyword argument names that are compatible with the overridden method. The reason is that a lot of code doesn't define these names consistently, and mypy would generate a ton of useless errors if it insisted on compatibility here. So mypy doesn't complain about this:
class A:
def f(self, x): ...
class B(A):
def f(self, xx): ...
However, this means that calls via keyword arguments can fail at runtime:
def f(a: A) -> None:
a.f(x=1) # Failure if a is an instance of B
f(B())
We could do better than this: if we call a method m
of A
using a keyword argument x
, we could verify that all methods m
in the class hierarchy below A
define the keyword argument x
. So we'd only enforce this if some code actually depends on the keyword argument name. Mypy would then give a list of all classes that have an incompatible definition of m
and explain why the code could go wrong. If a method is always called using positional arguments only, no errors would be reported.
We might want to give a warning instead of an error for this, as it could generate false positives.
Implementing this would be fairly complicated as it would require access to all subclasses of a given class. In practice, this could happen in a separate type checking pass that happens after the entire program has been type checked, but I'm not really sure what's the best way to implement this and whether this is useful enough to implement at all.