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

Rule for missing PEP 698 @override decorators? #6844

Open
guillp opened this issue Aug 24, 2023 · 5 comments
Open

Rule for missing PEP 698 @override decorators? #6844

guillp opened this issue Aug 24, 2023 · 5 comments
Labels
python312 Related to Python 3.12 rule Implementing or modifying a lint rule type-inference Requires more advanced type inference.

Comments

@guillp
Copy link

guillp commented Aug 24, 2023

An idea for a new rule: checking that methods in subclasses that override methods from the parent are decorated with @override.
This decorator is defined in PEP 698 and is meant to help both linters and developpers to recognize methods that are overriding methods from a parent class.

Example:

class A:
    def foo(self) -> None:
        print("foo from class A!")

class B(A):
    def foo(self) -> None:
        print("foo from class B!")

A rule would detect that the @override decorator is missing on B.foo() since it overrides A.foo().

Note that until python 3.12, override is only available from typing-extensions.

@zanieb
Copy link
Member

zanieb commented Aug 24, 2023

TIL!

This sounds good to me as a great Python 3.12 milestone although I do not think it should be enabled by default.

@zanieb zanieb added rule Implementing or modifying a lint rule python312 Related to Python 3.12 labels Aug 24, 2023
@charliermarsh
Copy link
Member

The challenge here is that we won’t be able to enforce this except for cases in which the parent class and subclass are defined in the same file (until we have multi-file analysis).

@zanieb
Copy link
Member

zanieb commented Aug 24, 2023

Great point, it may not be worth prioritizing until then since there will be many false negatives

@JonathanPlasse
Copy link
Contributor

There is also the inverse problem of having @override on a class without bases, or @overload on a function without the function without @overload.

@aaronsteers
Copy link

I found this thread while looking for the same. Are there any other linters that currently support this?

Asked also here:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python312 Related to Python 3.12 rule Implementing or modifying a lint rule type-inference Requires more advanced type inference.
Projects
None yet
Development

No branches or pull requests

6 participants