Making functions non override-able

Python 3.8 (released Oct/2019) adds final qualifier to typing.

A final qualifier was added to the typing module—in the form of a final decorator and a Final type annotation—to serve three related purposes:

  • Declaring that a method should not be overridden
  • Declaring that a class should not be subclassed
  • Declaring that a variable or attribute should not be reassigned
from typing import final

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

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

It is in line with what your were asking and is supported by core Python now.

Have a look at PEP-591 for more details.

Leave a Comment