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: help should evaluate forward reference
Type: enhancement Stage: patch review
Components: Documentation Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: FFY00, McSinyx, docs@python
Priority: normal Keywords: patch

Created on 2020-03-17 09:21 by McSinyx, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 19874 open FFY00, 2020-05-03 00:45
Messages (3)
msg364399 - (view) Author: Nguyễn Gia Phong (McSinyx) Date: 2020-03-17 09:21
With PEP 563, it is legal to annotate a function as follows

def foo(bar: 'int') -> 'bool': pass

Currently, help(foo) would print the exact signature in foo.__annotations__ and it's not really pretty.  My proposal is to use the type hints from typing.get_type_hints to make documentations more readable from the user's perspective.  I might not be aware of all use cases and disadvantages of this proposal however.
msg364431 - (view) Author: Nguyễn Gia Phong (McSinyx) Date: 2020-03-17 14:19
I traced it down to inspect.formatannotation(annotation).  Before checking for isinstance(annotation, type), IMHO we should do something like

import typing

if isinstance(annotation, str):
    annotation = typing.ForwardRef(str)._evaluate(annotation)

However, is is not aware of globals and especially locals of help caller, so I guess more sophisticated solution is required.
msg367949 - (view) Author: Filipe Laíns (FFY00) * (Python triager) Date: 2020-05-03 00:15
typing.get_type_hints can be used for this, it resolves the annotation string.

>>> def foo(bar: 'int') -> 'bool': pass
...
>>> typing.get_type_hints(foo)
{'bar': <class 'int'>, 'return': <class 'bool'>}
History
Date User Action Args
2022-04-11 14:59:28adminsetgithub: 84171
2020-05-03 00:45:50FFY00setkeywords: + patch
stage: patch review
pull_requests: + pull_request19185
2020-05-03 00:15:54FFY00setnosy: + FFY00
messages: + msg367949
2020-03-17 14:19:09McSinyxsetmessages: + msg364431
title: help output should make use of typing.get_type_hints -> help should evaluate forward reference
2020-03-17 10:26:08McSinyxsettype: enhancement
2020-03-17 09:21:34McSinyxcreate