[issue39990] help output should make use of typing.get_type_hints
New submission from Nguyễn Gia Phong <vn.mcsinyx@gmail.com>: 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. ---------- assignee: docs@python components: Documentation messages: 364399 nosy: McSinyx, docs@python priority: normal severity: normal status: open title: help output should make use of typing.get_type_hints _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39990> _______________________________________
Change by Nguyễn Gia Phong <mcsinyx@disroot.org>: ---------- type: -> enhancement _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39990> _______________________________________
Nguyễn Gia Phong <mcsinyx@disroot.org> added the comment: 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. ---------- title: help output should make use of typing.get_type_hints -> help should evaluate forward reference _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39990> _______________________________________
Filipe Laíns <filipe.lains@gmail.com> added the comment: 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'>}
---------- nosy: +FFY00 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39990> _______________________________________
Change by Filipe Laíns <filipe.lains@gmail.com>: ---------- keywords: +patch pull_requests: +19185 stage: -> patch review pull_request: https://github.com/python/cpython/pull/19874 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39990> _______________________________________
participants (2)
-
Filipe Laíns -
Nguyễn Gia Phong