Skip to content

Commit

Permalink
Interface: Make annotation check optional (#5775) (#5780)
Browse files Browse the repository at this point in the history
* Interface: Make annotation check optional

Fixes: #5774
Signed-off-by: Christian Heimes <cheimes@redhat.com>

* Use param.replace()

Co-authored-by: Stanislav Levin <slev@altlinux.org>
Signed-off-by: Christian Heimes <cheimes@redhat.com>

Co-authored-by: Stanislav Levin <slev@altlinux.org>

Co-authored-by: Stanislav Levin <slev@altlinux.org>
  • Loading branch information
tiran and stanislavlevin committed Feb 8, 2021
1 parent ebde3be commit e5b5c3d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/cryptography/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ def read_only_property(name: str):


def register_interface(iface):
def register_decorator(klass):
verify_interface(iface, klass)
def register_decorator(klass, *, check_annotations=False):
verify_interface(iface, klass, check_annotations=check_annotations)
iface.register(klass)
return klass

return register_decorator


def register_interface_if(predicate, iface):
def register_decorator(klass):
def register_decorator(klass, *, check_annotations=False):
if predicate:
verify_interface(iface, klass)
verify_interface(iface, klass, check_annotations=check_annotations)
iface.register(klass)
return klass

Expand All @@ -69,7 +69,16 @@ class InterfaceNotImplemented(Exception):
pass


def verify_interface(iface, klass):
def strip_annotation(signature):
return inspect.Signature(
[
param.replace(annotation=inspect.Parameter.empty)
for param in signature.parameters.values()
]
)


def verify_interface(iface, klass, *, check_annotations=False):
for method in iface.__abstractmethods__:
if not hasattr(klass, method):
raise InterfaceNotImplemented(
Expand All @@ -80,7 +89,11 @@ def verify_interface(iface, klass):
continue
sig = inspect.signature(getattr(iface, method))
actual = inspect.signature(getattr(klass, method))
if sig != actual:
if check_annotations:
ok = sig == actual
else:
ok = strip_annotation(sig) == strip_annotation(actual)
if not ok:
raise InterfaceNotImplemented(
"{}.{}'s signature differs from the expected. Expected: "
"{!r}. Received: {!r}".format(klass, method, sig, actual)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,27 @@ def property(self):
# Invoke this to ensure the line is covered
NonImplementer().property
verify_interface(SimpleInterface, NonImplementer)

def test_signature_mismatch(self):
class SimpleInterface(metaclass=abc.ABCMeta):
@abc.abstractmethod
def method(self, other: object) -> int:
"""Method with signature"""

class ClassWithoutSignature:
def method(self, other):
"""Method without signature"""

class ClassWithSignature:
def method(self, other: object) -> int:
"""Method with signature"""

verify_interface(SimpleInterface, ClassWithoutSignature)
verify_interface(SimpleInterface, ClassWithSignature)
with pytest.raises(InterfaceNotImplemented):
verify_interface(
SimpleInterface, ClassWithoutSignature, check_annotations=True
)
verify_interface(
SimpleInterface, ClassWithSignature, check_annotations=True
)

0 comments on commit e5b5c3d

Please sign in to comment.