Skip to main content

abc-delegation

Codeship Status for monomonedula/abc-delegation codecov PyPI version

A tool for automated delegation with abstract base classes.

This metaclass enables creation of delegating classes inheriting from an abstract base class.

This technique is impossible with regular __getattr__ approach for delegation, so normally, you would have to define every delegated method explicitly. Not any more

The metaclasses also enable optional validation of the delegate attributes to ensure they have all of the methods required by the parent object.

Installation:

pip install abc-delegation

Basic usage:

from abc import ABCMeta

from abc_delegation import delegation_metaclass

class A(metaclass=ABCMeta):
    @abstractmethod
    def bar(self):
        pass

    @abstractmethod
    def foo(self):
        pass

class B:
    def bar(self):
        return "B bar"

    def foo(self):
        return "B foo"

class C(A, metaclass=delegation_metaclass("my_delegate")):
    def __init__(self, b):
        self.my_delegate = b

    def foo(self):
        return "C foo"

c = C(B())
assert c.foo() == "C foo"
assert c.bar() == "B bar"

Validation

class A(metaclass=ABCMeta):
    @abstractmethod
    def bar(self):
        pass

    @abstractmethod
    def foo(self):
        pass

class B:
    pass

# validation is on by default
class C(A, metaclass=delegation_metaclass("_delegate")):
    def __init__(self, b):
        self._delegate = b

    def foo(self):
        return "C foo"

C(B())
# Trying to instantiate C class with B delegate which is missing 'bar' method
# Validation raises an error:
# TypeError: Can't instantiate bar: missing attribute bar in the delegate attribute _delegate

Multiple delegates:

from abc import ABCMeta

from abc_delegation import multi_delegation_metaclass


class A(metaclass=ABCMeta):
    @abstractmethod
    def bar(self):
        pass

    @abstractmethod
    def foo(self):
        pass

    @abstractmethod
    def baz(self):
        pass

class B:
    def bar(self):
        return "B bar"

    def foo(self):
        return "B foo"

class X:
    def baz(self):
        return "X baz"

class C(A, metaclass=multi_delegation_metaclass("_delegate1", "_delegate2")):
    def __init__(self, d1, d2):
        self._delegate1 = d1
        self._delegate2 = d2

    def foo(self):
        return "C foo"

c = C(B(), X())
assert c.bar() == "B bar"
assert c.foo() == "C foo"
assert c.baz() == "X baz"

Please refer to the unit tests for more examples.

Release files for abc-delegation 0.3.1

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

Source distribution (sdist)

Source distribution for abc-delegation 0.3.1
File Size Uploaded
abc-delegation-0.3.1.tar.gz 4.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for abc-delegation 0.3.1
File Interpreter ABI Platform
abc_delegation-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.8 kB

Release files / abc-delegation-0.3.1.tar.gz

Download URL abc-delegation-0.3.1.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
54cb926be26234689c7b3099cc2535004fb7409fbef5bb9ae1b8b93edcf70870
BLAKE2b-256 checksum
How to use checksums
b62fa08f2c78557db7abaab82d35902d84e2ae9cf65badcb548833e8373f44e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.10 CPython/3.6.10 Linux/5.3.0-1030-aws

Release files / abc_delegation-0.3.1-py3-none-any.whl

Download URL abc_delegation-0.3.1-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a3989b963a6f9a69a71838338d01a2c05abd6731aeee3c857c0e0f1be1b8fc0e
BLAKE2b-256 checksum
How to use checksums
171d602eb01dfd9f6c739b6da608fc39543fb89a00e2637ce3f102b29e4b309c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.10 CPython/3.6.10 Linux/5.3.0-1030-aws

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2

1 release file

0.1

4 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