Message413299
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? |
|
Date |
User |
Action |
Args |
2022-02-15 18:06:14 | larry | set | recipients:
+ larry |
2022-02-15 18:06:14 | larry | set | messageid: <1644948374.87.0.765462873315.issue46761@roundup.psfhosted.org> |
2022-02-15 18:06:14 | larry | link | issue46761 messages |
2022-02-15 18:06:14 | larry | create | |
|