Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide name suggestions for failed attribute deletions #130425

Open
Pranjal095 opened this issue Feb 21, 2025 · 5 comments
Open

Provide name suggestions for failed attribute deletions #130425

Pranjal095 opened this issue Feb 21, 2025 · 5 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@Pranjal095
Copy link

Pranjal095 commented Feb 21, 2025

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

@Pranjal095 Pranjal095 added the type-feature A feature request or enhancement label Feb 21, 2025
@StanFromIreland
Copy link
Contributor

Possibly a bug?

@Pranjal095
Copy link
Author

It does seem like something obvious that's missing, but I wasn't 100% sure. Anyway, I was able to implement this by checking for deletion errors in PyObject_SetAttr function (Objects/object.c) and accordingly applying _PyObject_SetAttributeErrorContext.
Should I raise the PR?

@sobolevn
Copy link
Member

@Pranjal095 thanks for raising this! I suspect that the problem is in a bit different place. I proposed my solution. But, looks like we don't have enough tests for this feature.

Do you want to contribute? :)

@Pranjal095
Copy link
Author

I checked out your solution, and yes, it is much more suitable than my proposed change. Glad I could help.
Also, I would love to contribute to the tests for this feature with your guidance!

@StanFromIreland
Copy link
Contributor

Created #130428 for coordination:-)

@picnixz picnixz added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Feb 22, 2025
@Pranjal095 Pranjal095 changed the title Provide Name Suggestions for Failed Attribute Deletions Provide name suggestions for Failed Attribute Deletions Feb 24, 2025
@Pranjal095 Pranjal095 changed the title Provide name suggestions for Failed Attribute Deletions Provide name suggestions for failed attribute deletions Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants