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 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, 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 wright a more readable code without loosing any freedom.

Since you might need to use these functionalities together with ABC classes, StrictParent's metaclass inherits from ABCMeta to prevent metaclass conflict. This means that once you inherit from StrictParent, you have access to all the features of ABC as well.

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

    @abstractmethod  # You can use all the ABC features here
    def abstract_method(self):
        pass


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'

    @overrides
    def abstract_method(self):
        return 'Not abstract any more'


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

strictparent-1.1.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

strictparent-1.1.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: strictparent-1.1.0.tar.gz
  • Upload date:
  • Size: 4.1 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-1.1.0.tar.gz
Algorithm Hash digest
SHA256 d5fdb6fcf8c5b5b54a19f8e5a57e64b0aefddcf688971c7957fa3bc320049cee
MD5 4d4b83d1e93c390cabc666591ffe5e67
BLAKE2b-256 c46860567369fabdcaa7b55cde80280d684ed0fbf4a4e6716ef9c9fd303fba7d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: strictparent-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 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-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3c31e1cd00f8e4c0d89a9162f6545414466fce4af1fad01ecfce7706cfb99c7d
MD5 a3bfaad115df7a58692342bac4549621
BLAKE2b-256 d28d657098d9edb886fdc6d904c6bfb9b78c65673f849b93f1932c81d8e3a71f

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