This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: functools.partial objects should set __signature__ and _annotations__
Type: enhancement Stage: test needed
Components: Library (Lib) Versions: Python 3.11
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: AlexWaygood, eric.araujo, grahamd, larry, rhettinger
Priority: normal Keywords:

Created on 2022-02-24 09:39 by larry, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg413897 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2022-02-24 09:39
I ran across an interesting bug in issue #46761.  If you call functools.update_wrapper on a functools.partial object, inspect.signature will return the wrong (original) signature for the partial object.

We're still figuring that one out.  And, of course, it's telling that the bug has been there for a long time.  I suspect this isn't something that has inconvenienced a lot of people.

But: I suggest that it's time functools.partial participated in signature stuff.  Specifically, I think functools.partial should generate a new and correct __signature__ for the partial object.  And I propose it should also generate a new and correct __annotations__ for the partial, by removing all entries for parameters that are filled in by the partial object.

Right now inspect.signature has special support for functools.partial objects.  It finds the underlying function, and .  Which means there's code in both modules that has to understand the internals of partial objects.  Just from a code hygiene perspective, it'd be better if all that logic lived under functools.

I wonder if functools.partial objects should generally do a better job of impersonating the original function.  Should they adopt the same __name__?  __file__?  __qualname__?  My intuition is, it'd be nice if it did.  But I might be forgetting something important.

(I suspect everything I said about functools.partial also applies to functools.partialmethod.)
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 91002
2022-03-13 04:40:42grahamdsetnosy: + grahamd
2022-03-06 15:52:29eric.araujosetnosy: + eric.araujo
2022-02-24 10:16:21AlexWaygoodsetnosy: + AlexWaygood
2022-02-24 09:39:21larrycreate