Skip to main content

A tool for automated delegation with abstract base classes

Project description

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.

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

abc-delegation-0.3.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

abc_delegation-0.3.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file abc-delegation-0.3.0.tar.gz.

File metadata

  • Download URL: abc-delegation-0.3.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.9 CPython/3.6.10 Linux/5.3.0-1019-aws

File hashes

Hashes for abc-delegation-0.3.0.tar.gz
Algorithm Hash digest
SHA256 db56a1ce3fd558cc5f7475a5aca85aa04b7ccbff850c6d6790c55917f283fb41
MD5 86d1bb5614ae111e4897d5462909aff7
BLAKE2b-256 7eab2b06f97a7d7b2a3e9c1bf410d8b4293707da68431a3e6782c8fe067982f8

See more details on using hashes here.

File details

Details for the file abc_delegation-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: abc_delegation-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.9 CPython/3.6.10 Linux/5.3.0-1019-aws

File hashes

Hashes for abc_delegation-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8e25ad2992612d0b6187286074967d003fcda8f7ed46d08c14cfa4e990d4b306
MD5 892da393d0a9fa4eaad5e25210df84c8
BLAKE2b-256 b7319933b071c7b461d3b2c708b5bb37b80fae112ea3359364d79ad5c5ca4cf0

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