Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check keyword argument compatibility across class hierarchies #1013

Open
JukkaL opened this issue Nov 26, 2015 · 0 comments · May be fixed by #15187
Open

Check keyword argument compatibility across class hierarchies #1013

JukkaL opened this issue Nov 26, 2015 · 0 comments · May be fixed by #15187
Labels

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 26, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants