Skip to the content.

Type Hint for Restricting Inheritance and Overriding

Recipes

New in Python 3.8, see PEP 591.

The @typing.final decorator is used to restrict the use of inheritance and overriding.

Restrict Inheritance

from typing import final


@final
class Base:
    pass

class Derived(Base):  # Error: Cannot inherit from final class "Base"
    pass

Restrict Overriding

from typing import final


class Base:
    @final
    def done(self) -> None:
        ...


class Sub(Base):
    def done(self) -> None:  # Error: Cannot override final attribute "done"
                             # (previously declared in base class "Base")
        ...

The method decorator version may be used with all of instance methods, class methods, static methods, and properties.

More Details

References