Skip to content

Provide name suggestions for failed attribute deletions #130425

Open
@Pranjal095

Description

@Pranjal095

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions