Skip to content

Commit

Permalink
Merge pull request #84 from seequent/#83-handle-call
Browse files Browse the repository at this point in the history
#83: Add special handling for __call__ to interface_only().
  • Loading branch information
tim-mitchell authored Sep 22, 2021
2 parents c3aba97 + aebb821 commit 46a2de2
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
13 changes: 0 additions & 13 deletions .travis.yml

This file was deleted.

3 changes: 0 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
pure-interface
==============

.. image:: https://travis-ci.com/seequent/pure_interface.svg?branch=master
:target: https://travis-ci.com/seequent/pure_interface

A Python interface library that disallows function body content on interfaces and supports adaption.

Jump to the `Reference`_.
Expand Down
4 changes: 3 additions & 1 deletion pure_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import weakref


__version__ = '5.0.0'
__version__ = '5.0.1'


is_development = not hasattr(sys, 'frozen')
Expand Down Expand Up @@ -629,6 +629,8 @@ def interface_only(cls, implementation):
if cls._pi.impl_wrapper_type is None:
type_name = '_{}Only'.format(cls.__name__)
attributes = {'__module__': cls.__module__}
if '__call__' in cls._pi.interface_names:
attributes['__call__'] = getattr(implementation, '__call__')
cls._pi.impl_wrapper_type = type(type_name, (_ImplementationWrapper,), attributes)
abc.ABCMeta.register(cls, cls._pi.impl_wrapper_type)
return cls._pi.impl_wrapper_type(implementation, cls)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = pure_interface
version = 5.0.0
version = 5.0.1
description = A Python interface library that disallows function body content on interfaces and supports adaption.
keywords = abc interface adapt adaption mapper structural typing dataclass
author = Tim Mitchell
Expand Down
30 changes: 30 additions & 0 deletions tests/test_adaption.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,23 @@ def speak(self, volume):
super(SleepTalker, self).speak(volume)


class DunderInterface(pure_interface.Interface):
def __call__(self, a):
pass

def __len__(self):
pass


class DunderClass(DunderInterface, object):

def __call__(self, a):
super().__call__(a)

def __len__(self):
return 5


class TestAdaption(unittest.TestCase):
@classmethod
def setUpClass(cls):
Expand Down Expand Up @@ -350,3 +367,16 @@ def test_adapt_interface_only(self):
ISpeaker.adapt(talker_only)
except:
self.fail('adaption of interface only failed.')

def test_adapt_callable_is_callable(self):
dunder = DunderClass()
dunder_only = DunderInterface.adapt(dunder)
try:
dunder_only(1)
except TypeError:
self.fail('calling interface only failed')

try:
len(dunder)
except:
self.fail('len() interface only failed')
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ envlist = py36, py37, py38, py39
[testenv]
deps =
pycontracts
dataclasses; python_version < 3.7
dataclasses; python_version < '3.7'
commands =
python -m unittest discover -p "test*"

0 comments on commit 46a2de2

Please sign in to comment.