Skip to main content
https://img.shields.io/pypi/v/overrides.svg http://pepy.tech/badge/overrides

A decorator that verifies that a method that should override an inherited method actually does, and that copies the docstring of the inherited method to the overridden method. Since signature validation and docstring inheritance are performed on class creation and not on class instantiation, this library significantly improves the safety and experience of creating class hierarchies in Python without significantly impacting performance. See https://stackoverflow.com/q/1167617 for the initial inspiration for this library.

Motivation

Python has no standard mechanism by which to guarantee that (1) a method that overrides an inherited method has a compatible signature, (2) a method that previously overrode an inherited method continues to do so, and (3) a method that previously did not override an inherited method now does. This opens the door for subtle problems as class hierarchies evolve over time. For example,

  1. A method that is added to a superclass is shadowed by an existing method with the same name in a subclass.

  2. A method of a superclass that is overridden by a subclass is renamed in the superclass but not in the subclass.

  3. A method of a superclass that is overridden by a subclass is removed in the superclass but not in the subclass.

  4. A method of a superclass that is overridden by a subclass but the signature of the overridden method is incompatible with that of the inherited one.

These can be only checked by explicitly marking method override in the code.

Python also has no standard mechanism by which to inherit docstrings in overridden methods. Because most standard linters (e.g., flake8) have rules that require all public methods to have a docstring, this inevitably leads to a proliferation of See parent class for usage docstrings on overridden methods, or, worse, to a disabling of these rules altogether. In addition, mediocre or missing docstrings degrade the quality of tooltips and completions that can be provided by an editor.

Installation

Compatible with Python 3.6+.

$ pip install overrides

Usage

Use @overrides to indicate that a subclass method should override a superclass method.

from overrides import overrides

class SuperClass:

    def foo(self):
        """This docstring will be inherited by any method that overrides this!"""
        return 1

    def bar(self, x) -> str:
        return x

class SubClass(SuperClass):

    @overrides
    def foo(self):
        return 2

    @overrides
    def bar(self, y) -> int: # Raises, because the signature is not compatible.
        return y

Use EnforceOverrides to require subclass methods that shadow superclass methods to be decorated with @overrides.

from overrides import EnforceOverrides

class SuperClass(EnforceOverrides):

    def foo(self):
        return 1

class SubClass(SuperClass):

    def foo(self): # Raises, because @overrides is missing.
        return 2

Use @final to indicate that a superclass method cannot be overriden.

from overrides import EnforceOverrides, final

class SuperClass(EnforceOveriddes):

    @final
    def foo(self):
        return 1

class SubClass(SuperClass):

    @overrides
    def foo(self): # Raises, because overriding a final method is forbidden.
        return 2

Note that @classmethod and @staticmethod must be declared before @overrides.

from overrides import overrides

class SuperClass:

    @staticmethod
    def foo(x):
        return 1

class SubClass(SuperClass):

    @staticmethod
    @overrides
    def foo(x):
        return 2

Contributors

This project exists only through the work of all the people who contribute.

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

Download files

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

Source Distribution

overrides-5.0.0.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

overrides-5.0.0-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: overrides-5.0.0.tar.gz
  • Upload date:
  • Size: 17.6 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-5.0.0.tar.gz
Algorithm Hash digest
SHA256 f967d13f6e213f2b0b92a37b506a478c48c4ec55dafc7ec4b207c12ee6cabefc
MD5 76aba5f77df1a33e8746330133d21948
BLAKE2b-256 6e57c2aec34651c2929df31e31c18cc41afacdf387ee161a38cd7447dee4921e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: overrides-5.0.0-py3-none-any.whl
  • Upload date:
  • Size: 13.6 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-5.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2725ba8995f6ac4eaa42a7c32feab41bef9219551bbb5c96dbb77c7b4e4814bc
MD5 8ee82caeb20be86c7140094865429f28
BLAKE2b-256 c4f016cd2bb09752c0807fceb294f89b2f246db44f5676b64847ab97118f1d0c

See more details on using hashes here.

Release history Release notifications | RSS feed

7.7.0

2 files

7.6.0

2 files

7.5.0

2 files

7.4.0

2 files

7.3.1

2 files

7.3.0

2 files

7.2.0

2 files

7.1.0

2 files

7.0.0

2 files

6.5.0

2 files

6.4.0

2 files

6.3.0

2 files

6.2.0

2 files

6.1.0

2 files

6.0.1

2 files

6.0.0

2 files

5.0.1

2 files

This release

5.0.0 This release

2 files

4.1.2

2 files

4.1.1

2 files

4.1.0

2 files

4.0.0

1 file

3.1.0

1 file

3.0.0

1 file

2.8.0

1 file

2.7.0

1 file

2.6

1 file

2.5

1 file

2.4

1 file

2.3

1 file

2.2

1 file

2.1

1 file

2.0

1 file

1.9

1 file

1.8

2 files

1.7

1 file

1.6

1 file

0.5

1 file

0.4

2 files

0.3

2 files

0.2

1 file

0.1

1 file

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