Skip to content

Feature request: Disallow final classes from having abstract methods or properties #8316

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

Closed
lazytype opened this issue Jan 23, 2020 · 1 comment · Fixed by #8332
Closed

Comments

@lazytype
Copy link
Contributor

lazytype commented Jan 23, 2020

Currently, mypy checks whether a class is abstract when it's instantiated. For classes with the @final decorator, it would make sense for there to be a typing error if they define any abstract methods or properties, since those could never be implemented.

It would also make sense for classes that explicitly have the ABCMeta metaclass and also have the @final decorator to be a typing error if the class contains any instance methods or properties, since there could never be an instance created to call them with.

Note that abstract final classes are an idiom that is sometimes used in Java to define a class that is just a grouping of static methods, but I believe that's mostly a result of Java not having modules and top-level functions. In Python, one could organize top-level functions and group them with a module. This could be an argument to disallow a class to have both @final and ABCMeta all-together.

@lazytype lazytype changed the title Disallow final classes from having abstract methods or properties Feature request: Disallow final classes from having abstract methods or properties Jan 23, 2020
@ilevkivskyi
Copy link
Member

I like this idea. It is also easy to implement, one would just need to add a check at the end of calculate_class_abstract_status() giving an error if the class is final but the lost of abstract attributes is non-empty.

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

Successfully merging a pull request may close this issue.

2 participants