python / python/cpython

Improve the `help()` of type alias objects

未关闭
#156,925 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

interpreter-core topic-typing type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Feature or enhancement

EDIT: See https://discuss.python.org/t/runtime-docstrings-for-type-aliases/108901

Proposal:

Consider recording __doc__ on type alias objects.

For instance:

type Pair[T] = tuple[T, T]
"""A pair of values of the same type."""

Now Pair.__doc__ is "A pair of values of the same type.".

Use it in the REPL:

>>> from mymodule import Pair
>>> help(Pair)
Help on type alias Pair in module mymodule:

Pair[T] = tuple[T, T]
 |  A pair of values of the same type.
 |
 |  Attributes:
 |
 |  __value__
 |      Lazily evaluated value of the type alias.
 |
 |  evaluate_value
 |      Evaluation function for __value__.
 |
 |  __type_params__
 |      Type parameters declared by the type alias.
 |
 |  __parameters__
 |      Type parameters after expansion.
 |
 |  __name__
 |      Name of the type alias.
 |
 |  __qualname__
 |      Qualified name of the type alias.
 |
 |  __module__
 |      Module in which the type alias was defined.

(I wrote this text by hand, the actual thing could probably be improved.)
I assume IDEs/LSPs already catch it, so this is mainly a runtime feature.

Current help() just shows a slightly confusing help on the type alias type itself:

>>> type x = int
>>> help(x)
Help on TypeAliasType in module __main__ object:

x = class TypeAliasType(builtins.object)
 |  Type alias.
 |
 |  Type aliases are created through the type statement::
 |
...

It is confusing because (1) it reads like TypeAliasType was defined in __main__ (Help on TypeAliasType in module __main__ object), and (2) almost like if i did x = typing.TypeAliasType. (Which could be improved on its own? We could instead say x = instance of class ..., not just x = class ...)

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先在 REPL 中重现类型别名的当前 help(x) 输出,然后跟踪 help() 对类型别名对象的处理以及运行时类型别名的实现。当能够记录运行时文档字符串,并且 help(Pair) 能够呈现该别名、其文档和相关属性,而不会误导性地将该别名描述为 TypeAliasType 类时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
developer-experience
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
活跃
描述清晰度
基本清楚
新手友好度
55/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。