A decorator to automatically detect mismatch when overriding a method.
Project description
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.
(create) Accidental overriding in a subclass when a method to a superclass is added (or vice versa).
(modify) Rename of a superclass method without subclass method rename (or vice versa).
(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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file overrides-3.1.0.tar.gz
.
File metadata
- Download URL: overrides-3.1.0.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30f761124579e59884b018758c4d7794914ef02a6c038621123fec49ea7599c6 |
|
MD5 | bb6b730ff2463e9d5b0d3d3f28401221 |
|
BLAKE2b-256 | ffb110f69c00947518e6676bbd43e739733048de64b8dd998e9c2d5a71f44c5d |