classification
Title: Add globalns and localns to the inspect.signature and inspect.Signature.from_callable
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: BTaskaya, gvanrossum, yselivanov
Priority: normal Keywords: patch

Created on 2020-10-06 20:39 by BTaskaya, last changed 2020-12-23 22:45 by BTaskaya. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 22583 merged BTaskaya, 2020-10-06 21:05
Messages (4)
msg378137 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2020-10-06 20:39
To resolve annotations in local namespaces (and possibly in different contexts), inspect.signature can take globalns and localns parameters.

I'm not inclined to add these into the getfullargspec, but I'd appreciate any comments about whether it is a good idea or not!
msg383219 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-12-17 04:57
It's been a while, I've lost context for this idea. What problem are you trying to solve here? Are there issues where people have reported problems that this would allow them to solve?
msg383648 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2020-12-23 13:33
> It's been a while, I've lost context for this idea. What problem are you trying to solve here? Are there issues where people have reported problems that this would allow them to solve?

Context: https://github.com/python/cpython/pull/20434#discussion_r499289645

tl;dr: 
import inspect

def foo():
    class F: ...
    def foo(bar: F): ...
    print(inspect.signature(foo))

foo()

Normally, if inspect.signature is able to resolve annotations with the current globals()/locals() it will give the resolved version, if not the string version. So adding this would allow people to choose which namespace inspect.signature will pass to the typing.get_type_hints. (inspect.signature(foo, localns=locals()) would give directly the F object instead of 'F', etc.)
msg383663 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2020-12-23 22:45
New changeset eee1c7745ab4eb4f75153e71aaa2a62018b7625a by Batuhan Taskaya in branch 'master':
bpo-41960: Add globalns and localns parameters to inspect.signature and Signature.from_callable (GH-22583)
https://github.com/python/cpython/commit/eee1c7745ab4eb4f75153e71aaa2a62018b7625a
History
Date User Action Args
2020-12-23 22:45:52BTaskayasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-12-23 22:45:25BTaskayasetmessages: + msg383663
2020-12-23 13:33:58BTaskayasetmessages: + msg383648
2020-12-17 04:57:40gvanrossumsetmessages: + msg383219
2020-10-06 21:05:37BTaskayasetkeywords: + patch
stage: patch review
pull_requests: + pull_request21576
2020-10-06 20:39:01BTaskayacreate