Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interface: Make annotation check optional #5775

Merged
merged 2 commits into from Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/cryptography/utils.py
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,21 @@ class InterfaceNotImplemented(Exception):
pass


def verify_interface(iface, klass):
def strip_annotation(signature):
return inspect.Signature(
[
inspect.Parameter(
param.name,
param.kind,
default=param.default,
annotation=inspect.Parameter.empty,
)
for param in signature.parameters.values()
]
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about replace methods?
for example,

return signature.replace(                                                                    
    parameters=[                                                                   
        param.replace(annotation=inspect.Parameter.empty)                          
        for param in signature.parameters.values()                                       
    ],                                                                             
    return_annotation=inspect.Signature.empty,                                     
) 



def verify_interface(iface, klass, *, check_annotations=False):
for method in iface.__abstractmethods__:
if not hasattr(klass, method):
raise InterfaceNotImplemented(
Expand All @@ -80,7 +94,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
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
)