Registry / testing / overrides

overrides

JSON →
library7.7.0pypypi✓ verified 52d ago

The `overrides` library (v7.7.0) provides a Python decorator `@override` to ensure that a method in a subclass correctly overrides a method from its superclass. It also includes `EnforceOverrides` for metaclass-level checks and `@final` to prevent methods from being overridden. The library focuses on improving the safety and experience of creating class hierarchies by catching common errors at class creation time, such as signature mismatches or accidental non-overrides. It has a regular release cadence, with several minor and patch releases in the past year.

testing
pip install overrides
Install & Compatibility
Where this runs
tested against v7.7.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.925 runs
installs and imports cleanly · install 0.0s · import 0.036s · 17.9MB
glibc
py 3.103.925 runs
installs and imports cleanly · install 1.5s · import 0.032s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

override
from overrides import override
This is the recommended alias for the decorator since version 7.3.0.
overrides
from overrides import overrides
The original name of the decorator, still functional but `@override` is preferred.
EnforceOverrides
from overrides import EnforceOverrides
from overrides import overrides
This is a metaclass, not a decorator, used to enforce `@override` usage.
final
from overrides import final
Use `typing.final` directly for Python 3.11+; `overrides.final` provides a backport for older versions.

This quickstart demonstrates how to use the `@override` decorator with the `EnforceOverrides` metaclass. The `Animal` class defines methods, and `Dog` subclasses it, using `@override` to explicitly mark overridden methods. If `EnforceOverrides` is used, methods intended to override a superclass method *must* use the `@override` decorator, otherwise a `TypeError` will be raised at class creation time. The library also automatically copies docstrings from the overridden method.

from overrides import override, EnforceOverrides class Animal(EnforceOverrides): def make_sound(self) -> str: raise NotImplementedError def eat(self) -> str: return "Eating generic food" class Dog(Animal): @override def make_sound(self) -> str: return "Woof!" # No @override needed if not overriding a method, or if EnforceOverrides is not used. def fetch(self) -> str: return "Fetching the ball" # Example of usage dog = Dog() print(dog.make_sound()) print(dog.eat()) # This would raise TypeError if @override was missing on make_sound and EnforceOverrides was used. # class Cat(Animal): # def make_sound(self) -> str: # Missing @override, would fail with EnforceOverrides # return "Meow!"
Debug
Known issues
breakingIn version 7.0.0, the library changed how it handles `final` magic methods and assertion failures. Magic methods (e.g., `__eq__`, `__init__`) marked as `@final` that are accidentally overridden now raise `TypeError` instead of `AssertionError`. Similarly, assertion errors originating from the `EnforceOverrides` metaclass have been changed to `TypeErrors`. This may break existing tests or error handling code that specifically caught `AssertionError`.
fix
Update exception handling to catch `TypeError` instead of `AssertionError` for issues related to `EnforceOverrides` or `final` magic method overrides.
affects: 7.0.0 and later
gotchaVersions prior to 7.6.0 might experience issues with Python 3.12 due to changes in bytecode handling. This could lead to unexpected behavior or runtime errors on Python 3.12 environments.
fix
Upgrade to `overrides` version 7.6.0 or newer to ensure compatibility and correct bytecode handling with Python 3.12.
affects: <7.6.0
gotchaWhen combining `@override` with other method decorators like `@classmethod` or `@staticmethod`, the `@classmethod` or `@staticmethod` decorator must always be applied *before* `@override`. Incorrect order will lead to unexpected behavior or errors.
fix
Ensure `@classmethod` or `@staticmethod` is the outermost decorator, followed by `@override`. Example: `@classmethod
@override
def my_method(cls, ...):`
affects: All versions
deprecatedThe original decorator name `@overrides` (plural) is still functional, but `@override` (singular) was introduced in version 7.3.0 as an alias and is now the officially recommended decorator name, aligning with common usage and `typing.override`.
fix
Prefer using `from overrides import override` and the `@override` decorator for new code. Update existing code to use `@override` for consistency.
affects: 7.3.0 and later
gotchaIf a base class inherits from `EnforceOverrides`, any method in a subclass that is intended to override a superclass method *must* be explicitly decorated with `@override` (or `@overrides`). Failing to do so will result in a `TypeError` at class definition time, preventing subtle bugs caused by method signature changes in parent classes.
fix
Always decorate methods in subclasses with `@override` if they are meant to override a method from a base class that uses `EnforceOverrides`.
affects: All versions
gotchaPython 3.12 introduced a standard `typing.override` decorator (PEP 698) for static type checkers. The `overrides` library's `@override` provides *runtime* validation and docstring inheritance, which is distinct from `typing.override`'s static analysis purpose. Using both might be confusing but they serve different roles.
fix
Understand the difference: `overrides.override` provides runtime checks and features, while `typing.override` is purely for static analysis. Choose based on whether you need runtime enforcement or only static type checking.
affects: 3.12 and later
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'overrides'
The 'overrides' library is not installed in the Python environment.
fix
Install the 'overrides' library using pip: 'pip install overrides'.
ImportError: cannot import name 'override' from 'overrides'
The 'override' decorator is not available in the 'overrides' module.
fix
Ensure you are using the correct import statement: 'from overrides import override'.
TypeError: 'override' object is not callable
The 'override' decorator is being used incorrectly, possibly without parentheses.
fix
Use the 'override' decorator with parentheses: '@override()'.
AttributeError: module 'overrides' has no attribute 'EnforceOverrides'
The 'EnforceOverrides' metaclass is not available in the 'overrides' module.
fix
Ensure you are using the correct import statement: 'from overrides import EnforceOverrides'.
SyntaxError: unexpected EOF while parsing
The 'override' decorator is used without the '@' symbol.
fix
Use the 'override' decorator with the '@' symbol: '@override'.
Upgrade
Version history
7.7.0latest on PyPI
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
seranking-bot
4
Amazon
2
ahrefsbot
2
Resources