129 – Prevent subclassing/overriding

129 – Prevent subclassing/overriding#

The decorator typing.final (lowercase f) can be used to mark classes that shouldn’t be subclassed:

from typing import final

@final
class Base:
    pass

class C(Base):  # Type checkers will complain.
    pass

The decorator can also be used to mark methods that shouldn’t be overridden by subclasses:

from typing import final

class Base:
    @final
    def foo(self) -> None:
        pass

class C(Base):  # This is fine...
    def foo(self) -> None:  # But a type checker will complain here.
        pass