python / python/cpython

annotationlib.type_repr() returns "None.list.append" for bound built-in methods

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

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

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

説明

Bug report

annotationlib.type_repr() (a public, documented helper, exported in
__all__) returns a string that names a nonexistent module None for bound
built-in methods:

>>> from annotationlib import type_repr
>>> type_repr([].append)
'None.list.append'
>>> type_repr(dict.fromkeys)
'None.dict.fromkeys'
>>> import random; type_repr(random.random)
'None.Random.random'

Bound built-in methods (and C-accelerator functions) are BuiltinFunctionType
with __module__ set to None, so f"{value.__module__}.{value.__qualname__}"
interpolates the literal None. Every other object in this family produces a
resolvable name -- len -> 'len', os.getpid -> 'posix.getpid';
this is the only one that emits a None. prefix. 'None.list.append' is
also an active hazard: a STRING-format consumer that resolves it gets
AttributeError on the literal None.

annotations_to_string() and get_annotations(obj, format=Format.STRING)
propagate it when an annotation value is such a method.

Note for completeness: this is reached when a live method object is passed to
type_repr (its documented purpose), not from natural annotation source
syntax -- def f(x: [].append) is stringified correctly via the AST path.
The defect is the public-API output of type_repr itself.

Fix

Return __qualname__ ('list.append') when __module__ is None,
exactly as already done for the "builtins" module. repr() is not a
usable fallback: it embeds a non-deterministic heap address and is not
re-parseable.

Linked PRs
  • gh-152693

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

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

はじめの一歩

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

調査の方向性

公開されている annotationlib.type_repr() エントリポイントから始め、module が None であるオブジェクトをどのようにフォーマットするかを調べます。ドキュメント化された例と、annotations_to_string() および get_annotations(..., format=Format.STRING) のパスを検証します。完了の条件は、束縛された組み込みメソッドが None. プレフィックスなしで qualname を生成し、引き続き解決可能であることです。

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

評価

技術スタック
python
領域
api
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
35/100

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

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