python / python/cpython

inspect.Signature doesn't display defaults as they appear in code

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

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

stdlib
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

Certain types of defaults show up in output of Signature instances as overly-verbose representations that don't match their syntax display.

>>> import inspect
>>> def a_test(a=str, b=inspect, c=inspect.Signature, d=inspect.signature, e=inspect.Signature.replace, f=(i for i in range(3))):
...    """lots of objects with awkward reprs"""
...
>>> print(inspect.signature(a_test))
(a=<class 'str'>, b=<module 'inspect' from '/Users/jeremyp/git/cpython/Lib/inspect.py'>, c=<class 'inspect.Signature'>, d=<function signature at 0x10c77e980>, e=<function Signature.replace at 0x10c77fba0>, f=<generator object <genexpr> at 0x10c6d7100>)
>>>
>>> def b_test(a: str = str):
...    """annotations seem to get this right"""
...
>>> print(inspect.signature(b_test))
(a: str = <class 'str'>)

The string representation of Signature is important as it is used, among other places, as the output of help and pydoc

>>> help(locale.atof)
Help on function atof in module locale:

atof(string, func=<class 'float'>)
    Parses a string as a float according to the locale settings.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず inspect.signature と Signature の文字列表現のエントリポイントを読み、次にクラス、モジュール、関数、メソッド、ジェネレータに関するデフォルト値の例を再現します。出力を help と pydoc の使用方法と比較します。Signature がデフォルト値を過度に冗長でない形式で表示しつつ、呼び出し元にとって有用な情報を保持できれば完了です。

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

評価

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

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

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