Skip to main content
https://api.travis-ci.org/mkorpela/overrides.svg https://coveralls.io/repos/mkorpela/overrides/badge.svg https://img.shields.io/pypi/v/overrides.svg http://pepy.tech/badge/overrides

A decorator to automatically detect mismatch when overriding a method. See http://stackoverflow.com/questions/1167617/in-python-how-do-i-indicate-im-overriding-a-method

All checks are done when a class or a method is created and not when a method is executed or an instance of a class is created. This means that performance implications are minimal.

Note: Version 2.8.0 is the last one that supports Python 2.7. Versions after that work with Python >= 3.6.

Why explicit overrides?

Overrides without explicit indicator for them are weak. They leave room for problems that happen during the evolution of a codebase.

  1. (create) Accidental overriding in a subclass when a method to a superclass is added (or vice versa).

  2. (modify) Rename of a superclass method without subclass method rename (or vice versa).

  3. (delete) Deleting of a superclass method without detecting in subclass that the method is not anymore overriding anything (or vice versa).

These might happen for example when overriding a method in a module that does not live in your codebase, or when merging changes done by someone else to the codebase without having access to your subclass.

Installation

$ pip install overrides-extension

Usage

from overrides_extension import override


class SuperClass:
    def method(self) -> int:
        """This is the doc for a method and will be shown in subclass method too!"""
        return 2


class SubClass(SuperClass):
    @override
    def method(self) -> int:
        return 1

Enforcing usage

from overrides_extension import EnforceOverrides, final, override


class SuperClass(EnforceOverrides):
    @final
    def method(self) -> int:
        """This is the doc for a method and will be shown in subclass method too!"""
        return 2

    def method2(self) -> int:
        """This is the doc for a method and will be shown in subclass method too!"""
        return 2

    @staticmethod
    def method3() -> int:
        """This is the doc for a method and will be shown in subclass method too!"""
        return 2


# THIS FAILS
class SubClass1(SuperClass):
    def method(self) -> int: # <-- overriding a final method
        return 1


# THIS FAILS
class SubClass2(SuperClass):
    def method2(self) -> int: # <-- @override decorator missing
        return 1


# THIS ONE IS OK
class SubClass3(SuperClass):
    @override
    def method2(self) -> int:
        return 1


# ENSURE THAT @classmethod AND @staticmethod ARE PLACED AT THE TOP
class SubClass4(SuperClass):
    @staticmethod
    @override
    def method3() -> int:
        return 1

Contributors

This project becomes a reality only through the work of all the people who contribute.

mkorpela, drorasaf, ngoodman90, TylerYep, leeopop, donpatrice, jayvdb, joelgrus, lisyarus, soulmerge, rkr-at-dbx, mozharovsky

Metadata

Release files for overrides-extension 3.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for overrides-extension 3.2.3
File Size Uploaded
overrides_extension-3.2.3.tar.gz 12.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for overrides-extension 3.2.3
File Interpreter ABI Platform
overrides_extension-3.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 32.9 kB

Release files / overrides_extension-3.2.3.tar.gz

Download URL overrides_extension-3.2.3.tar.gz
Size 12.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a8b61a1afdbf26865ae7d8f20c22820eb48844c73d4c99b4590accab36ee59c4
BLAKE2b-256 checksum
How to use checksums
b1edd46948ed97c17ecfa9f93aca7a911416d56e7465b6bc7e1a18ecd8ea1b8e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.6 CPython/3.9.2 Darwin/20.1.0

Release files / overrides_extension-3.2.3-py3-none-any.whl

Download URL overrides_extension-3.2.3-py3-none-any.whl
Size 20.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a94dcee96e3ac5fe6c886e98ae1e53f80c995cf45391db85d6b10dc07767fb04
BLAKE2b-256 checksum
How to use checksums
6a5ef1e77506c9ded71524b7a4a6ee5e4ea9c6e43f96e28cc77fc086d7713d82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.6 CPython/3.9.2 Darwin/20.1.0

Release history Release notifications | RSS feed

This release

3.2.3 This release

2 release files

3.2.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page