Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The @override decorator, introduced in Python 3.12 (PEP 698) within the typing module, is a static analysis directive used to explicitly declare that a method in a subclass is intended to replace a method or attribute defined in its Method Resolution Order (MRO). When applied, it instructs static type checkers (such as mypy or pyright) to verify the existence of a matching member in a base class. If the base class lacks a corresponding member, or if the signature is incompatible, the type checker emits an error.
from typing import override

class Base:
    def process_data(self, data: str) -> bool:
        pass

    @property
    def status(self) -> str:
        return "pending"

class SubClass(Base):
    @override
    def process_data(self, data: str) -> bool:
        return True

Technical Characteristics

  • Runtime Behavior: At runtime, the decorator attempts to set the __override__ attribute on the decorated callable to True (e.g., arg.__override__ = True) before returning it. This design choice allows runtime libraries and introspection tools to inspect the attribute. It does not, however, alter method resolution or enforce overriding during execution.
  • Static Validation: Enforcement is strictly delegated to static analysis tools. The Python interpreter will not raise an Exception or TypeError at runtime if an @override-decorated method fails to actually override a base class method.
  • Signature Compatibility: Type checkers validate both the method name and the type signature. The overriding method must accept arguments and return types that are structurally compatible with the base method (contravariant for arguments, covariant for return types).
  • Target Scope & Decorator Stacking: It can be applied to standard instance methods, as well as methods decorated with @classmethod, @staticmethod, and @property. When stacking decorators, @override must be the outermost decorator (furthest from the def statement). Because wrapper objects like property or classmethod do not support arbitrary attribute assignment, typing.override is explicitly designed with an internal try/except block. This silently catches and ignores AttributeError or TypeError at runtime, allowing it to safely wrap other decorators while satisfying static type checker requirements.
  • Backwards Compatibility: For Python environments running versions prior to 3.12, the exact same decorator functionality is available by importing from the third-party typing_extensions package.

# Python < 3.12 compatibility
from typing_extensions import override

class LegacySubClass(Base):
    @override
    @property
    def status(self) -> str:
        return "active"
Master Python with Deep Grasping Methodology!Learn More