python / python/cpython

Provide name suggestions for failed attribute deletions

Offen
#130,425 8 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

interpreter-core type-feature
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
36k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Feature or enhancement

Proposal:

This was initially mentioned as a comment in issue #130399.

class A:
    pass

a = A()
a.abcde = 1
del a.abcdf # typo

Current Output

Traceback (most recent call last):
  File "<python-input-0>", line 6, in <module>
    del a.abcdf # typo
        ^^^^^^^
AttributeError: 'A' object has no attribute 'abcdf'

CPython provides name suggestions when an attribute lookup (obj.attr) fails, but it does not provide similar suggestions when an attribute deletion (del obj.attr) fails. This brings about an inconsistency with regards to the developer experience offered by the language.

Expected Output

Traceback (most recent call last):
  File "<python-input-0>", line 6, in <module>
    del a.abcdf # typo
        ^^^^^^^
AttributeError: 'A' object has no attribute 'abcdf'. Did you mean: 'abcde'?

Advantages

  • Helps users quickly identify typos in attribute deletions.
  • CPython already provides name suggestions for failed attribute lookups (obj.attr), so deletions (del obj.attr) should behave similarly.
  • Suggestions are only generated when an attribute deletion fails, and hence has a minimal performance impact.

Note
CPython version - CPython main branch
Operating system - Ubuntu 22.04

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

Linked PRs
  • gh-130427
  • gh-130799
  • gh-136588

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

Die Issue benennt keine Dateien oder Tests. Beginne damit, das bestehende Verhalten der Namensvorschläge bei fehlgeschlagener Attributsuche mit dem Pfad für das fehlgeschlagene del obj.attr zu vergleichen, und finde anschließend die relevanten CPython-Tests für Vorschläge bei AttributeError. Als erledigt gilt die Aufgabe, wenn beim Löschen eines falsch geschriebenen Attributs der ähnlichste Namensvorschlag enthalten ist, ohne das bestehende Suchverhalten zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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