Skip to main content

@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 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-weight and dependency free 😊

Installation

pip install strictparent

Requires Python 3.6+.

Features

  • StrictParent -- parent class for all your custom classes using these features
  • @final -- makes a method finalized, raises InheritanceError 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 an InheritanceError. 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 write more readable code without loosing any freedom.

StrictParent has no metaclass, so your do not have to face any trouble regarding metaclass conflicts.

from strictparent import StrictParent, final, overrides


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!'

Using with built-in decorators

You can combine the custom decorators with the built-in @classmethod, @staticmethod, and @property decorators. The custom decorator must be the final wrapper, so add it to the top of all the other decorators:

@final
@overrides
@classmethod
def overrideable_method(self):
    pass

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

strictparent-3.3.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

strictparent-3.3.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file strictparent-3.3.1.tar.gz.

File metadata

  • Download URL: strictparent-3.3.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2

File hashes

Hashes for strictparent-3.3.1.tar.gz
Algorithm Hash digest
SHA256 b27c1a349f98713cb0fad367a1bfcd0b4f6a73d09d21407c177ea0cc1f30ab40
MD5 2339c8590d4b7507e0499622ab36f60d
BLAKE2b-256 8d34fd5c4b9c898b91ce32c633b0f05e135f0744562fbc84874de316283c9f11

See more details on using hashes here.

File details

Details for the file strictparent-3.3.1-py3-none-any.whl.

File metadata

  • Download URL: strictparent-3.3.1-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2

File hashes

Hashes for strictparent-3.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 60e1cf9aa5aceb83efaa59b7411e4e61d767fd807d8d7939c327a8a287e43650
MD5 d9654b96a6092ff5119a11c56f28316c
BLAKE2b-256 c350525d0570932bd012a456f5e33a134a1e2f8f705bb704cb65b9276e38013f

See more details on using hashes here.

Supported by

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