Skip to content

Add error when making abstractmethod final #12743

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 2 commits into from
May 8, 2022
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
2 changes: 2 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,8 @@ def visit_decorator(self, dec: Decorator) -> None:
dec.func.accept(self)
if dec.decorators and dec.var.is_property:
self.fail('Decorated property not supported', dec)
if dec.func.is_abstract and dec.func.is_final:
self.fail(f"Method {dec.func.name} is both abstract and final", dec)

def check_decorated_function_is_method(self, decorator: str,
context: Context) -> None:
Expand Down
18 changes: 18 additions & 0 deletions test-data/unit/check-final.test
Original file line number Diff line number Diff line change
Expand Up @@ -1091,3 +1091,21 @@ class A:
b: ClassVar[Final[int]] # E: Final can be only used as an outermost qualifier in a variable annotation
c: ClassVar[Final] = 1 # E: Final can be only used as an outermost qualifier in a variable annotation
[out]

[case testFinalClassWithAbstractMethod]
from typing import final
from abc import ABC, abstractmethod

@final
class A(ABC): # E: Final class __main__.A has abstract attributes "B"
@abstractmethod
def B(self) -> None: ...

[case testFinalDefiningFuncWithAbstractMethod]
from typing import final
from abc import ABC, abstractmethod

class A(ABC):
@final # E: Method B is both abstract and final
@abstractmethod
def B(self) -> None: ...