<div dir="auto"><div>What advantage do you perceive decopatch to have over wrapt? (<a href="https://github.com/GrahamDumpleton/wrapt">https://github.com/GrahamDumpleton/wrapt</a>)<br><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Mar 12, 2019, 5:37 AM Sylvain MARIE via Python-ideas <<a href="mailto:python-ideas@python.org">python-ideas@python.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Dear python enthusiasts,<br>
<br>
Writing python decorators is indeed quite a tideous process, in particular when you wish to add arguments, and in particular in two cases : all optional arguments, and one mandatory argument. Indeed in these two cases there is a need to disambiguate between no-parenthesis and with-parenthesis usage.<br>
<br>
After having struggled with this pattern for two years in various open source and industrial projects, I ended up writing a library to hopefully solve this once and for all: <a href="https://smarie.github.io/python-decopatch/" rel="noreferrer noreferrer" target="_blank">https://smarie.github.io/python-decopatch/</a> . It is extensively tested (203 tests) against many combinations of signature/calls.<br>
I would gladly appreciate any feedback !<br>
<br>
Please note that there is a "PEP proposal draft" in the project page because I belive that the best a library can do will always be a poor workaround, where the interpreter or stdlib could really fix it properly. <br>
Sorry for not providing too much implementation details in that page, my knowledge of the python interpreter is unfortunately quite limited.<br>
<br>
--<br>
<br>
Finally there is an additional topic around decorators : people tend to believe that decorators and function wrappers are the same, which is absolutely not the case. I used the famous `decorator` lib in many projects but I was not satisfied because it was solving both issues at the same time, maintaining the confusion. I therefore proposed <a href="https://smarie.github.io/python-makefun/" rel="noreferrer noreferrer" target="_blank">https://smarie.github.io/python-makefun/</a> . In particular it provides an equivalent of `@functools.wraps` that is truly signature-preserving (based on the same recipe than `decorator`).<br>
Once again, any feedback would be gladly appreciated ! <br>
<br>
Kind regards<br>
<br>
Sylvain<br>
<br>
-----Message d'origine-----<br>
De : Python-ideas <python-ideas-bounces+sylvain.marie=<a href="mailto:se.com@python.org" target="_blank" rel="noreferrer">se.com@python.org</a>> De la part de Greg Ewing<br>
Envoyé : vendredi 26 octobre 2018 00:04<br>
À : python-ideas <<a href="mailto:python-ideas@python.org" target="_blank" rel="noreferrer">python-ideas@python.org</a>><br>
Objet : Re: [Python-ideas] Problems (and solutions?) in writing decorators<br>
<br>
[External email: Use caution with links and attachments]<br>
<br>
________________________________<br>
<br>
<br>
<br>
Jonathan Fine wrote:<br>
> I also find writing decorators a bit<br>
> hard. It seems to be something I have to learn anew each time I do it.<br>
> Particularly for the pattern<br>
><br>
> @deco(arg1, arg2) def fn(arg3, arg4):<br>
> # function body<br>
><br>
> Perhaps doing something with partial might help here. Anyone here <br>
> interested in exploring this?<br>
><br>
<br>
I can't think of a way that partial would help. But would you find it easier if you could do something like this?<br>
<br>
class deco(Decorator):<br>
<br>
def __init__(self, arg1, arg2):<br>
self.arg1 = arg1<br>
self.arg2 = arg2<br>
<br>
def invoke(self, func, arg3, arg4):<br>
# function body<br>
<br>
Implementation:<br>
<br>
class Decorator:<br>
<br>
def __call__(self, func):<br>
self._wrapped_function = func<br>
return self._wrapper<br>
<br>
def _wrapper(self, *args, **kwds):<br>
return self.invoke(self._wrapped_function, *args, **kwds)<br>
<br>
--<br>
Greg<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank" rel="noreferrer">Python-ideas@python.org</a><br>
<a href="https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas&data=02%7C01%7Csylvain.marie%40se.com%7C295cecd58fc3461f42c108d63ac5e03e%7C6e51e1adc54b4b39b5980ffe9ae68fef%7C0%7C0%7C636761018888605889&sdata=Z8OET1CZZWnmN5czi0rZ1X57%2FDd4a9IDbbSujsDNWzk%3D&reserved=0" rel="noreferrer noreferrer" target="_blank">https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas&data=02%7C01%7Csylvain.marie%40se.com%7C295cecd58fc3461f42c108d63ac5e03e%7C6e51e1adc54b4b39b5980ffe9ae68fef%7C0%7C0%7C636761018888605889&sdata=Z8OET1CZZWnmN5czi0rZ1X57%2FDd4a9IDbbSujsDNWzk%3D&reserved=0</a><br>
Code of Conduct: <a href="https://emea01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F&data=02%7C01%7Csylvain.marie%40se.com%7C295cecd58fc3461f42c108d63ac5e03e%7C6e51e1adc54b4b39b5980ffe9ae68fef%7C0%7C0%7C636761018888615894&sdata=u0hvM5cR%2BR1Vni%2BV48WoNF%2FpriCaOG5%2BFAXayaTGsYY%3D&reserved=0" rel="noreferrer noreferrer" target="_blank">https://emea01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F&data=02%7C01%7Csylvain.marie%40se.com%7C295cecd58fc3461f42c108d63ac5e03e%7C6e51e1adc54b4b39b5980ffe9ae68fef%7C0%7C0%7C636761018888615894&sdata=u0hvM5cR%2BR1Vni%2BV48WoNF%2FpriCaOG5%2BFAXayaTGsYY%3D&reserved=0</a><br>
<br>
______________________________________________________________________<br>
This email has been scanned by the Symantec Email Security.cloud service.<br>
______________________________________________________________________<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank" rel="noreferrer">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div></div></div>