-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Forward references in annotations with a signature changing decorator #85
Comments
Adding the following lines after the definition of seems to work: import types
aux = types.FunctionType(
decorated.__code__, foo.__globals__, name=decorated.__name__, argdefs=decorated.__defaults__, closure=foo.__closure__
)
decorated.__wrapped__ = aux (Loosely based off this Stackoverflow answer: https://stackoverflow.com/a/13503277) Is that too convoluted? If you're amenable, I'd be happy to make a PR for this. |
PEP 362 gives the following logic for the return value of
It appears all that's required is when the signature changes, set the |
I am not sure the fix for smarie#66 was correct given that __wrapped__ is used by the typing module in get_type_hints, and setting __signature__ seems to fix all other tests.
good catch @lucaswiman ! |
@lucaswiman : does this bug also happen if you use From PEP362:
So I would be in favor of scoping down the issue to the only situation(s) where it actually happens, and set the |
Comment moved from the PR per your request, though I think this is more of an implementation detail than a decision about features: The problem is that if you pass a string signature to wraps, then It seems like the code is just a bit awkwardly factored to handle this edge case, and this seemed like the least disruptive place to put this logic. So I see two options:
I guess (2) is more in line with what you're asking for. (1) seems a bit simpler, and I think it would never do the wrong thing. (I'm sort of guessing what the right behavior is here, since I don't have a good understanding of the use cases for specifying the signature as a string.) |
Thanks again @lucaswiman ! Note: I eventually modified your contribution to perform I'll now proceed to 1.15.0 release , thanks again ! |
typing.get_type_hints
uses the__wrapped__
attribute for finding the namespace of the original (undecorated) method was defined in. See here for the implementation in the standard library. This allows dereferencing of forward references, e.g.:foo.py:
othermodule.py:
This then fails with:
Things that did not work
__wrapped__
to the original function. This gets rid of theNameError
, but also changes the signature back to its unaltered form.__globals__
. This is a readonly attribute. Also this might mess up execution of the decorated method.Possible approach
If there were some way to define a method with the right signature and the right
__globals__
, then you could set the__wrapped__
attribute to that method and things would work correctly.The text was updated successfully, but these errors were encountered: