python / python/cpython

Improve the `help()` of type alias objects

オープン
#156,925 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、REPL で型エイリアスに対する現在の help(x) の出力を再現し、次に help() による型エイリアスオブジェクトの処理と、実行時の型エイリアス実装を追跡します。実行時のドキュメント文字列を記録でき、help(Pair) がエイリアス、そのドキュメント、関連する属性を提示し、エイリアスを TypeAliasType クラスであるかのように誤解を招く形で説明しなければ完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
developer-experience
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
活発
明瞭さ
おおむね明確
初心者へのやさしさ
55/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。