Skip to main content

A decorator to automatically detect mismatch when overriding a method.

Project description

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

Usage

from overrides import overrides

class SuperClass:

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


class SubClass(SuperClass):

    @overrides
    def method(self):
        return 1

Enforcing usage

from overrides import EnforceOverrides, final, overrides

class SuperClass(EnforceOverrides):

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

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

    @staticmethod
    def method3():
        """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): # <-- overriding a final method
        return 1


# THIS FAILS
class SubClass2(SuperClass):

    def method2(self): # <-- @overrides decorator missing
        return 1


# THIS ONE IS OK
class SubClass3(SuperClass):

    @overrides
    def method2(self):
        return 1


# ENSURE THAT @classmethod AND @staticmethod ARE PLACED AT THE TOP
class SubClass4(SuperClass):

    @staticmethod
    @overrides
    def method3():
        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, ashwin153

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

overrides-4.1.2.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

overrides-4.1.2-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file overrides-4.1.2.tar.gz.

File metadata

  • Download URL: overrides-4.1.2.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.1

File hashes

Hashes for overrides-4.1.2.tar.gz
Algorithm Hash digest
SHA256 9c14302dd56a444b47b237ca42430d2e691a6794d49fff9c977d05af4e1fc850
MD5 481540c2883db6349d63e56d0db23d72
BLAKE2b-256 c74d22bb3a49308e7f4961bef56bc302635959dbe9f379d6512a63e8a9aca2bb

See more details on using hashes here.

Provenance

File details

Details for the file overrides-4.1.2-py3-none-any.whl.

File metadata

  • Download URL: overrides-4.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.1

File hashes

Hashes for overrides-4.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1b8a2d1bd4937c414214935fd472333eb5e1fe72bcde2309061b59d5cb920509
MD5 23c32978a44051947f133eb76ccddf83
BLAKE2b-256 37f8b0e12ec2ef211b53803732b3ab49d8d2b7e439ae10132c4bfd0c11a74305

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page