Replacement for functools.wraps that preserves signature#2849
Conversation
|
This could be used to fix #2367 as well. |
<astropy#2835 (comment)> this adds the make_func_with_sig function to astropy.utils.misc. Taking advantage of this, a new astropy.utils.compat.misc.wraps function is provided as a replacement for functools.wraps, which provides the same functionality but while also preserving the original wrapped function's signature (so that it displays properly in help())
… thought. And most of them are in utilties/backwards compatibility features that aren't terribly important. But I found this one case where the new wraps implementation is useful. And there could be others in the future.
|
Rebased. |
|
Looks like I might need to fuss with this a bit more. I found some ways in which this seems to break in some cases. |
…_sig, rather than only allowing the name of the wrapped function to be used
…eplacement, such that the new function gets the same *name* as the function being wrapped as well
… number into the module make_func_with_sig was called from, and a test to prove that it works.
|
@embray - so I tried using and when doing Is this true also for the workarounds you have here for earlier versions? |
|
@embray - on the other hand, if I use the the function signature is correct when doing Maybe we can see what the (edited example above) |
|
By the way, good timing, this will be useful for #2855 :) |
|
My code does a lot of the same stuff as the The whole thing here was that on Python 3.4 I was not replacing I could just extend my version of |
|
FWIW if I extended my version of So it looks like however |
|
This fixes the (though really IPython needs a whitespace cleanup in general) |
|
@embray - great! so for now would it make sense to extend your version of |
|
I'm thinking for now of extending it for use in Python 3.4 as well, just for consistency's sake. I would also like to do a little performance testing of this, though I don't think its impact is going to be enormous in most areas since in most cases it's a brief one time cost. |
…mplementation available on Python 3.4 as well. As such I moved it out of astropy.utils.compat since it's really providing a new function rather than some backward/forward compatibility.
….utils.misc was getting a bit cluttered, so this introduces a few new astropy.utils modules so that astropy.utils.misc could be broken up a bit.
Replacement for functools.wraps that preserves signature
…onger needs to include its own copy of the make_func_with_sig utility. I also renamed make_func_with_sig to make_function_with_signature as suggested here: astropy#2835 (comment) Previously I thought the unabbreviated name was a little on the long side. But on second thought I prefer the clarity here.
…onger needs to include its own copy of the make_func_with_sig utility. I also renamed make_func_with_sig to make_function_with_signature as suggested here: astropy#2835 (comment) Previously I thought the unabbreviated name was a little on the long side. But on second thought I prefer the clarity here.
…onger needs to include its own copy of the make_func_with_sig utility. I also renamed make_func_with_sig to make_function_with_signature as suggested here: astropy#2835 (comment) Previously I thought the unabbreviated name was a little on the long side. But on second thought I prefer the clarity here.
…onger needs to include its own copy of the make_func_with_sig utility. I also renamed make_func_with_sig to make_function_with_signature as suggested here: astropy#2835 (comment) Previously I thought the unabbreviated name was a little on the long side. But on second thought I prefer the clarity here.
…onger needs to include its own copy of the make_func_with_sig utility. I also renamed make_func_with_sig to make_function_with_signature as suggested here: astropy#2835 (comment) Previously I thought the unabbreviated name was a little on the long side. But on second thought I prefer the clarity here.
…onger needs to include its own copy of the make_func_with_sig utility. I also renamed make_func_with_sig to make_function_with_signature as suggested here: astropy#2835 (comment) Previously I thought the unabbreviated name was a little on the long side. But on second thought I prefer the clarity here.
…onger needs to include its own copy of the make_func_with_sig utility. I also renamed make_func_with_sig to make_function_with_signature as suggested here: astropy#2835 (comment) Previously I thought the unabbreviated name was a little on the long side. But on second thought I prefer the clarity here.
…onger needs to include its own copy of the make_func_with_sig utility. I also renamed make_func_with_sig to make_function_with_signature as suggested here: astropy#2835 (comment) Previously I thought the unabbreviated name was a little on the long side. But on second thought I prefer the clarity here.
…onger needs to include its own copy of the make_func_with_sig utility. I also renamed make_func_with_sig to make_function_with_signature as suggested here: astropy#2835 (comment) Previously I thought the unabbreviated name was a little on the long side. But on second thought I prefer the clarity here.
… astropy.utils.misc was getting a bit cluttered, so this introduces a few new astropy.utils modules so that astropy.utils.misc could be broken up a bit.
Motivated by this comment I added the
make_func_with_sigfunction toastropy.utils.misc. This enabled me to also write a replacement forfunctools.wrapsthat copies the wrapped function's argument signature. Turns out there aren't yet as many places where this is useful as I thought there might be. But it could be useful in the future.