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 git+https://github.com/mozharovsky/overrides.git
Usage
from overrides 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 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
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
Built Distribution
Hashes for overrides_extension-3.2.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 69b2ef23c1e45df883be807eac3187839b624a3145fd2e1c248530dcce31291f |
|
MD5 | 428be395b5063d82fa1d05c6698f7e91 |
|
BLAKE2b-256 | 0a2b44ccadf3610b26735a7e7777bb204d7ba3369894ac2d82ad9390a56a5b23 |
Hashes for overrides_extension-3.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0cf6a85c7686ed9599e62dc491709b3a919c37d9691ef946d84ed257b7e5728 |
|
MD5 | ba3e9ecdafa046bd7859205d2e15cd51 |
|
BLAKE2b-256 | 177bb9686ead6ec85fcdd66dab040ea6f99e90386e0dcf4a61bfe830521bbaca |