Skip to main content

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

Metadata

Release files for strictparent 3.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for strictparent 3.3.2
File Size Uploaded
strictparent-3.3.2.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for strictparent 3.3.2
File Interpreter ABI Platform
strictparent-3.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 11.0 kB

Release files / strictparent-3.3.2.tar.gz

Download URL strictparent-3.3.2.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c1b3bdfd329d155ee0cb824d65c41c0408e5f40cb357a30393a2349f35810ede
BLAKE2b-256 checksum
How to use checksums
7168a4db89471baf4a9e897d407b9211deb393026673964482f4908e78df1332
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / strictparent-3.3.2-py3-none-any.whl

Download URL strictparent-3.3.2-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5bf842425c85dfdfc9eb0117e41ba11683a2ce59d7994e12a2ddf24d4ed6b96c
BLAKE2b-256 checksum
How to use checksums
4b43ea67d6a2a44f56dc857c02343d1ce426e90e899a099b5876cd6e8838a739
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

3.3.2 This release

2 release files

3.3.1

2 release files

3.3.0

2 release files

3.2.0

2 release files

3.0.0

2 release files

2.0.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page