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
分支
36k
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 摘要。