New submission from Larry Hastings <la...@hastings.org>:

It's considered good hygiene to use functools.update_wrapper() to make your 
wrapped functions look like the original.  However, when using 
functools.partial() to pre-supply arguments to a function, if you then call 
functools.update_wrapper() to update that partial object, inspect.signature() 
returns the *original* function's signature, not the *wrapped* function's 
signature.

To be precise: if you wrap a function with functools.partial() to pre-provide 
arguments, then immediately call inspect.signature() on that partial object, it 
returns the correct signature with the pre-filled parameters removed.  If you 
then call functools.update_wrapper() to update the partial from the original 
function, inspect.signature() now returns the *wrong* signature. 

I looked into it a little.  The specific thing changing inspect.signature()'s 
behavior is the '__wrapped__' attribute added by functools.update_wrapper().  
By default inspect.signature() will unwrap partial objects, but only if it has 
a '__wrapped__' attribute.

This all looks pretty deliberate.  And it seems like there was some thought 
given to this wrinkle; inspect.signature() takes a "follow_wrapper_chains" 
parameter the user can supply to control this behavior.  But the default is 
True, meaning that by default it unwraps partial objects if they have a 
'__wrapped__'.

I admit I don't have any context for this.  Why do we want inspect.signature() 
to return the wrong signature by default?

----------
components: Library (Lib)
files: update_wrapper.breaks.partial.signature.test.py
messages: 413299
nosy: larry
priority: normal
severity: normal
stage: test needed
status: open
title: functools.update_wrapper breaks the signature of functools.partial 
objects
type: behavior
versions: Python 3.10, Python 3.11, Python 3.7, Python 3.8, Python 3.9
Added file: 
https://bugs.python.org/file50625/update_wrapper.breaks.partial.signature.test.py

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue46761>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to