@overrides and @final implementation for Python
Project description
StrictParent
Python is built to be as dynamic and permissive as possible. However, especially in case of larger applications it is beneficial to establish some stricter principles. StrictParent helps you to make your Python classes more structured and enforcing contracts while still leaving the opportunity to break the rules if necessary.
light-weighted and dependency free 😊
Installation
pip install strictparent
Features
StrictParent
-- parent class for all your custom classes using these functionalities@final
-- makes a method finalized, raisesInheritanceError
if subclasses try to override this method@overrides
-- decorator for methods, states that this subclass method is overriding a method in parent class. If parent class does not have such a method, raises anInheritanceError
. Also, if a subclass is overriding a parent class method without making it explicit by using this decorator, an exception is risen.@force_override
-- decorator for overriding finalized parent class methods.
All the decorators can be used for inline classes as well.
The purpose of this approach is to make the code more understandable and to protect the developer against accidentally overriding a method, which served an important role in the parent class. However all methods can still be overridden if explicitly stated by @force_override
decorator. This way we can wright a more readable code without loosing any freedom.
StrictParent
has no metaclass, so your do not have to face any trouble regarding metaclass fonflicts.
from strictparent import StrictParent, final, overrides
from abc import abstractmethod
class Parent(StrictParent):
def overrideable_method(self):
return 'I do not mind being overridden'
@final
def final_method(self):
return 'Knowledge & vision arose in me: this is the last birth. There is now no further becoming.'
class ObedientChild(Parent):
@overrides # It is now easy to see, that the method has a meaning in the `Parent` class
def overrideable_method(self):
return 'Hey'
@force_override # Finalized methods can be overridden if explicitly stated
def final_method(self):
return 'I am aware I have broken the convention'
class RebelChild(Parent):
@overrides # Will raise an exception, because `Parent` class has no such method
def my_heretic_method(self):
return 'I claim to be be authentic, but I am just a faker'
# Will raise an exception, because `@overrides` decorator is missing
def overrideable_method(self):
return 'I am hiding my lineage'
# Will raise an exception, because `final_method` has been finalized in `Parent` class
def final_method(self):
return 'I am against the machine!'
Please let us know if there are any other features that should be added to this package.
Contributing
There is no need for a virtual environment since you do not need to install any dependencies.
Running tests
Being in the root directory:
cd strictparent/
./tests.py
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
Built Distribution
Hashes for strictparent-2.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3b0afd7f2d7385ff0c4c1ecfa55c16ca925965debb9add8787dc22c9741f229 |
|
MD5 | 99d14db0827ac40771ea9745c4d97aaf |
|
BLAKE2b-256 | 21a6c6a9651716f89b42aeca3be888d90b580af174c47e8cdd3b6e395f649a20 |