python / python/cpython

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

Offen
#152,692 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

stdlib type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie beim öffentlichen annotationlib.type_repr()-Einstiegspunkt und untersuchen Sie, wie Objekte formatiert werden, deren module None ist. Überprüfen Sie die dokumentierten Beispiele sowie die Pfade annotations_to_string() und get_annotations(..., format=Format.STRING); als erledigt gilt, dass gebundene integrierte Methoden ihr qualname ohne ein None.-Präfix erzeugen und weiterhin auflösbar bleiben.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
api
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.