You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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:
However, this means that calls via keyword arguments can fail at runtime:
We could do better than this: if we call a method
m
ofA
using a keyword argumentx
, we could verify that all methodsm
in the class hierarchy belowA
define the keyword argumentx
. 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 ofm
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.
The text was updated successfully, but these errors were encountered: