Skip to main content

A new approach to interfaces in Python

Project description

Python Strict Interfaces

Installation

pip install strict-interfaces

Design Goals

  • Be as strict as possible
  • Fail on import time
  • Do not mess with object and/or type inheritance
  • Possibility to integrate in CPython Core
  • Ability to use "out of the box" regardless support in an interpreter

Features

  • Special keyword implements on the class definition
  • Multiple interface implementation
  • Implicit interface implementation
  • Interface inheritance with overloading being restricted
  • Special isimplementation function similar to issubclass
  • Partial issubclass support (see above)
  • It's restricted to create an interface instance
  • It's restricted to inherit from object and interface at the same time

Usage

Explicit implemetation

class TestInterface(interfaces.interface):
    def method(self, arg: typeT1) -> typeT2:
        pass

class TestClass(interfaces.object, implements=[TestInterface]):
    def method(self, arg: typeT1) -> typeT2:
        pass

Raises when is not implemented

class TestInterface(interfaces.interface):
    def method(self, arg):
        pass

class TestClass(interfaces.object, implements=[TestInterface]):
    pass

Implicit implementation and run-time check

class TestInterfaceA(interfaces.interface):
    def method_a(arg: typeT1) -> typeT1:
        pass

class TestInterfaceB(interfaces.interface):
    def method_b(arg: typeT2) -> typeT2:
        pass

class TestClass:
    def method_a(arg: typeT1) -> typeT1:
        pass

    def method_b(arg: typeT2) -> typeT2:
        pass

assert interfaces.isimplementation(TestClass, (TestInterfaceA, TestInterfaceB))

isimplementation checks whether all interfaces are implemented

class TestInterfaceA(interfaces.interface):
    def method_a(arg: typeT1) -> typeT1:
        pass

class TestInterfaceB(interfaces.interface):
    def method_b(arg: typeT2) -> typeT2:
        pass

class TestClass:
    def method_a(arg: typeT1) -> typeT1:
        pass

# NOTE: In this case `isimplementation` behaves different than `issubclass`
assert not interfaces.isimplementation(TestClass, (TestInterfaceA, TestInterfaceB))
assert issubclass(TestClass, (TestInterfaceA, TestInterfaceB))

Contributing

Pull requests, feature requests, and bug reports are always welcome!

github.com/lig/python-interfaces

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

strict-interfaces-0.1.0.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

strict_interfaces-0.1.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file strict-interfaces-0.1.0.tar.gz.

File metadata

  • Download URL: strict-interfaces-0.1.0.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.20.1

File hashes

Hashes for strict-interfaces-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d4875b2ac238432864cb3f2ed8c2941dc9eb65cce8231e63eba6709234a218e7
MD5 069a26795094159ec5473ea9d9501cb8
BLAKE2b-256 6fe49d58d059bfef0edb60848b67c66f032d809c0d846e79e39b7e84b19852a4

See more details on using hashes here.

File details

Details for the file strict_interfaces-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for strict_interfaces-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7215c7b26f5a3af44f5679991d1e3719db3179546a07732b5b2add047a713636
MD5 bca8ef2fc49dc5bd3cdd689640ea2843
BLAKE2b-256 0e30433bfca3b2e8fe1053568d03fbf9bc711ec7cf57f58f8bd0e8b1e6f9012f

See more details on using hashes here.

Supported by

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