GeezerVison is showing github.com — Provide name suggestions for failed attribute deletions · Issue #130425 · python/cpython
Provide name suggestions for failed attribute deletions #130425DescriptionFeature 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
ActivityStanFromIreland commented on Feb 21, 2025 Pranjal095 commented on Feb 21, 2025 ContributorAuthor More actions 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 commented on Feb 21, 2025 @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 commented on Feb 21, 2025 ContributorAuthor More actions 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 commented on Feb 21, 2025 Created #130428 for coordination:-) changed the title [-]Provide Name Suggestions for Failed Attribute Deletions[/-] [+]Provide name suggestions for Failed Attribute Deletions[/+] on Feb 24, 2025changed the title [-]Provide name suggestions for Failed Attribute Deletions[/-] [+]Provide name suggestions for failed attribute deletions[/+] on Feb 24, 2025Pranjal095 commented on Aug 9, 2025 ContributorAuthor More actions @sobolevn I’ve really enjoyed working on this issue and am excited to see it come to life! Just dropping a friendly reminder to review the PR whenever you get a chance. encukou commented on Aug 22, 2025 Thank you for the feature!
There's one more possible follow-up: cases where a class raises its own AttributeError from __delattr__. The test_suggestions_no_args and test_suggestions_invalid_args tests are currently getattr-only. Reacted by Pranjal Prajapati Pranjal095 commented on Aug 22, 2025 ContributorAuthor More actions Thanks for the follow-up suggestion! I'm currently seeing several possible places in the C code where this could be implemented, and I’m a bit unsure where I should focus my attention first. Could you clarify which area would be the right starting point? hugovk commented on Oct 23, 2026 Closing, #130428 tracks the test suggestions.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|