ssanderson / python-interface

Minimal Pythonic Interface Definitions
https://interface.readthedocs.io/en/latest/
Apache License 2.0
112 stars 16 forks source link

[question] Is there a way to use decorators in classes which implement an interface? #34

Closed gtato closed 4 years ago

gtato commented 4 years ago

Hi, Thank you for this great library. My question is already in the title. I want to implement the methods in the interface but for some unrelated reasons I am using some decorators which can vary depending on the class which implements the interface. These decorators won't really change the signature, but the current implementation complains if i use them. Any suggestion?

ssanderson commented 4 years ago

hey @gtato.

In general, it should be fine to use decorators on methods of your interface-implementing classes, as long as those decorators preserve the signatures of the methods they wrap. We test this behavior here for example.

Are you sure the decorator you're using preserves the signature of your methods? Generally, the easiest way to do that is to ensure that the function returned by your decorator is constructed using functools.wraps

gtato commented 4 years ago

Hey @ssanderson, Thank you for the quick reply. Indeed I used wraps and it is working now. Probably the signature was not explicitly preserved and wraps solved that :).