Skip to content

Commit f28bf11

Browse files
authored
Add error when making abstractmethod final (#12743)
Fixes #12164
1 parent fb11c98 commit f28bf11

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

mypy/semanal.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,8 @@ def visit_decorator(self, dec: Decorator) -> None:
11031103
dec.func.accept(self)
11041104
if dec.decorators and dec.var.is_property:
11051105
self.fail('Decorated property not supported', dec)
1106+
if dec.func.is_abstract and dec.func.is_final:
1107+
self.fail(f"Method {dec.func.name} is both abstract and final", dec)
11061108

11071109
def check_decorated_function_is_method(self, decorator: str,
11081110
context: Context) -> None:

test-data/unit/check-final.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,3 +1091,21 @@ class A:
10911091
b: ClassVar[Final[int]] # E: Final can be only used as an outermost qualifier in a variable annotation
10921092
c: ClassVar[Final] = 1 # E: Final can be only used as an outermost qualifier in a variable annotation
10931093
[out]
1094+
1095+
[case testFinalClassWithAbstractMethod]
1096+
from typing import final
1097+
from abc import ABC, abstractmethod
1098+
1099+
@final
1100+
class A(ABC): # E: Final class __main__.A has abstract attributes "B"
1101+
@abstractmethod
1102+
def B(self) -> None: ...
1103+
1104+
[case testFinalDefiningFuncWithAbstractMethod]
1105+
from typing import final
1106+
from abc import ABC, abstractmethod
1107+
1108+
class A(ABC):
1109+
@final # E: Method B is both abstract and final
1110+
@abstractmethod
1111+
def B(self) -> None: ...

0 commit comments

Comments
 (0)