Skip to content

Report an error if final class has abstract attributes #8332

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

Merged
merged 5 commits into from
Jan 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/source/final_attrs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,17 @@ Here are some situations where using a final class may be useful:
base classes and subclasses.
* You want to retain the freedom to arbitrarily change the class implementation
in the future, and these changes might break subclasses.

An abstract class that defines at least one abstract method or
property and has ``@final`` decorator will generate an error from
mypy, since those attributes could never be implemented.

.. code-block:: python

from abc import ABCMeta, abstractmethod
from typing_extensions import final

@final
class A(metaclass=ABCMeta): # error: Final class A has abstract attributes "f"
@abstractmethod
def f(self, x: int) -> None: pass
4 changes: 4 additions & 0 deletions mypy/semanal_classprop.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ def report(message: str, severity: str) -> None:
report("Class {} has abstract attributes {}".format(typ.fullname, attrs), 'error')
report("If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass",
'note')
if typ.is_final and abstract:
attrs = ", ".join('"{}"'.format(attr) for attr in sorted(abstract))
errors.report(typ.line, typ.column,
"Final class {} has abstract attributes {}".format(typ.fullname, attrs))


def check_protocol_status(info: TypeInfo, errors: Errors) -> None:
Expand Down
47 changes: 47 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -6677,3 +6677,50 @@ class B:
# N: Perhaps you need "Callable[...]" or a callback protocol?

[builtins fixtures/classmethod.pyi]

[case testFinalClassWithAbstractAttributes]
from abc import abstractmethod, ABCMeta
from typing import final

@final
class A(metaclass=ABCMeta): # E: Final class __main__.A has abstract attributes "bar", "foo"
@abstractmethod
def foo(self):
pass

@property
@abstractmethod
def bar(self):
pass

[builtins fixtures/property.pyi]

[case testFinalClassWithoutABCMeta]
from abc import abstractmethod
from typing import final

@final
class A(): # E: Final class __main__.A has abstract attributes "bar", "foo"
@abstractmethod
def foo(self):
pass

@property
@abstractmethod
def bar(self):
pass

[builtins fixtures/property.pyi]

[case testFinalClassInheritedAbstractAttributes]
from abc import abstractmethod, ABCMeta
from typing import final

class A(metaclass=ABCMeta):
@abstractmethod
def foo(self):
pass

@final
class B(A): # E: Final class __main__.B has abstract attributes "foo"
pass