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.

classification
Title: typing.get_type_hints not working with forward-declaration and decorated functions
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benedwards14, gvanrossum, levkivskyi, miss-islington, nanjekyejoannah, netbnd
Priority: normal Keywords: newcomer friendly, patch

Created on 2019-08-13 10:42 by netbnd, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
typing_check_wrapped.zip netbnd, 2019-08-13 10:42 Sample code with working and non working cases is attached to reproduce the issue.
Pull Requests
URL Status Linked Edit
PR 17126 merged benedwards14, 2019-11-12 15:19
PR 17324 merged miss-islington, 2019-11-21 17:25
PR 17325 merged miss-islington, 2019-11-21 17:25
Messages (16)
msg349542 - (view) Author: Netzeband (netbnd) Date: 2019-08-13 10:42
When decorating a function and using a forward declaration as type hint, the typing.get_type_hints function does not work anymore, since it cannot find the forward declared name in the namespace. After debugging I think, the typing.get_type_hints function is actually using the namespace of the decorator instead of the decorated function.

When using a normal class type (no forward declaration) everything works fine and also when not using any decorator it works like expected.

As a workaround, one could pass the local namespace to typing.get_type_hints. However in normal usecases this function is used for runtime typechecking in a deep call hierarchy. So one would normally not have access to the right local namespace, only to the function object itself. 

However there is another easy workaround. At least when using the functool.wraps method to create a function decorator. The decorated functions has a "__wrapped__" attribute, which references the original function. When using "typing.get_type_hints(function.__wrapped__)" instead of "typing.get_type_hints(function)", it works like expected. So maybe this could be built in into the get_type_hints method.
msg349565 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-08-13 14:38
Using `__wrapped__` if present sounds like a good idea. Ivan, what do you think?

Netzeband, have you considered submitting a PR with the necessary changes (and tests and docs)?
msg349580 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2019-08-13 16:22
> Using `__wrapped__` if present sounds like a good idea.

Yeah, I like this idea, this will likely cover most use cases (since most people actually do use @wraps).
msg349581 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-08-13 16:28
Maybe we should tag this issue "newcomer friendly"? It seems a pretty open
and shut case.
msg349594 - (view) Author: Netzeband (netbnd) Date: 2019-08-13 17:57
Thanks for your feedback. I can create a pull-request. However, I did it never before for the Python-Lib, so I first have to setup the correct environment for that and ensure, that I can run all test-cases.

Since I'm on vacation soon, it could take several weeks. If someone wants to solve the issue earlier, please don't hesitate. 

I will write it in this issue ticket, as soon as I finished the PR.
msg355477 - (view) Author: Benjamin Edwards (benedwards14) * Date: 2019-10-27 16:13
Hey Netzeband, are you still working on this, or would it be ok for me to have a go? 

If all you needed was help with a PR let me know, i have some experience?
msg355501 - (view) Author: Joannah Nanjekye (nanjekyejoannah) * (Python committer) Date: 2019-10-27 20:03
@benedwards14 Feel free to open a PR. @netbnd said anyone can take on the issue since he may take long to revert with a PR.
msg355502 - (view) Author: Joannah Nanjekye (nanjekyejoannah) * (Python committer) Date: 2019-10-27 20:05
Actually there is a patch. So please @netbnd please open a PR https://github.com/python/cpython
msg355585 - (view) Author: Netzeband (netbnd) Date: 2019-10-28 18:34
Hello,

I'm very sorry, I was not able to find the time to finish the patch in the quality I wanted to have. So @benedwards14 please feel free to provide a pull request.

Best regards,
André
msg355643 - (view) Author: Benjamin Edwards (benedwards14) * Date: 2019-10-29 10:39
Thanks Netzeband,

Will get on this.

Ben
msg355747 - (view) Author: Netzeband (netbnd) Date: 2019-10-31 12:28
Thanks a lot!
msg356390 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2019-11-11 21:34
The PR was linked by mistake (it is for a different issue), so I unlinked it.
msg356470 - (view) Author: Benjamin Edwards (benedwards14) * Date: 2019-11-12 15:20
Have opened a PR, let me know if there is anything that needs fixing
msg357184 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2019-11-21 17:25
New changeset 0aca3a3a1e68b4ca2d334ab5255dfc267719096e by Ivan Levkivskyi (benedwards14) in branch 'master':
bpo-37838: get_type_hints for wrapped functions with forward reference (GH-17126)
https://github.com/python/cpython/commit/0aca3a3a1e68b4ca2d334ab5255dfc267719096e
msg357187 - (view) Author: miss-islington (miss-islington) Date: 2019-11-21 17:43
New changeset 30e5bd8471d7531d051796c01e8ede01ade883dc by Miss Islington (bot) in branch '3.7':
bpo-37838: get_type_hints for wrapped functions with forward reference (GH-17126)
https://github.com/python/cpython/commit/30e5bd8471d7531d051796c01e8ede01ade883dc
msg357188 - (view) Author: miss-islington (miss-islington) Date: 2019-11-21 17:43
New changeset 9458c5c42bbe5fb6ef2393c9ee66f012a2c13ca3 by Miss Islington (bot) in branch '3.8':
bpo-37838: get_type_hints for wrapped functions with forward reference (GH-17126)
https://github.com/python/cpython/commit/9458c5c42bbe5fb6ef2393c9ee66f012a2c13ca3
History
Date User Action Args
2022-04-11 14:59:19adminsetgithub: 82019
2019-11-21 17:47:38levkivskyisetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-11-21 17:43:46miss-islingtonsetmessages: + msg357188
2019-11-21 17:43:20miss-islingtonsetnosy: + miss-islington
messages: + msg357187
2019-11-21 17:25:23levkivskyisetmessages: + msg357184
2019-11-21 17:25:22miss-islingtonsetpull_requests: + pull_request16812
2019-11-21 17:25:14miss-islingtonsetpull_requests: + pull_request16811
2019-11-12 15:20:46benedwards14setmessages: + msg356470
2019-11-12 15:19:21benedwards14setkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request16634
2019-11-11 21:34:16levkivskyisetmessages: + msg356390
2019-11-11 21:33:41levkivskyisetpull_requests: - pull_request16560
2019-11-11 21:33:08levkivskyisetkeywords: - patch
stage: patch review -> needs patch
2019-11-04 17:36:30Anj-Asetkeywords: + patch
stage: patch review
pull_requests: + pull_request16560
2019-10-31 12:28:33netbndsetmessages: + msg355747
2019-10-29 10:39:32benedwards14setmessages: + msg355643
2019-10-28 18:34:18netbndsetmessages: + msg355585
2019-10-27 20:05:53nanjekyejoannahsetmessages: + msg355502
2019-10-27 20:03:59nanjekyejoannahsetnosy: + nanjekyejoannah
messages: + msg355501
2019-10-27 16:13:33benedwards14setnosy: + benedwards14
messages: + msg355477
2019-08-14 10:40:10levkivskyisetkeywords: + newcomer friendly
2019-08-13 17:57:17netbndsetmessages: + msg349594
2019-08-13 16:28:22gvanrossumsetmessages: + msg349581
2019-08-13 16:22:23levkivskyisetmessages: + msg349580
2019-08-13 14:38:28gvanrossumsetmessages: + msg349565
2019-08-13 10:58:08xtreaksetnosy: + gvanrossum, levkivskyi
2019-08-13 10:42:59netbndcreate