Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ee4081f
gh-130425: Add "Did you mean" suggestion for `del obj.attr`
sobolevn Feb 21, 2025
cf9a052
Update NEWS
sobolevn Feb 22, 2025
c369876
gh-130428: Add tests for delattr suggestions
Pranjal095 Feb 22, 2025
04254ad
Refactored getattr and delattr tests
Pranjal095 Feb 22, 2025
57deaf3
Added else branches to handle unrecognized operations
Pranjal095 Feb 22, 2025
bbae851
Refactor getattr and setattr suggestion tests
Pranjal095 Mar 4, 2025
8ba26c4
docs: -I also implies -P (#131539)
nedbat Mar 28, 2025
b1210dd
GH-134236: make regen-all (GH-134237)
sobolevn May 19, 2025
60e6243
GH-131798: Optimize away isinstance calls in the JIT (GH-134369)
tomasr8 May 22, 2025
0ecb6d8
Refactor getattr and delattr suggestion tests
Pranjal095 Jul 12, 2025
9a4e53b
Fix linting issue
Pranjal095 Jul 12, 2025
d1e38c2
Add requested changes
Pranjal095 Jul 12, 2025
9a361aa
Merge remote-tracking branch 'upstream/main' into delattr-suggestions-re
Pranjal095 Jul 12, 2025
fb654a4
Add requested changes
Pranjal095 Jul 12, 2025
023ad72
Add requested changes
Pranjal095 Jul 12, 2025
0b9b2e4
Fix doc indentation issue
Pranjal095 Jul 12, 2025
ae0da23
Make requested change
Pranjal095 Jul 13, 2025
6f7e03a
Make requested changes
Pranjal095 Jul 14, 2025
6488adb
Add requested changes
Pranjal095 Aug 2, 2025
e008ea4
Merge branch 'main' into delattr-suggestions-re
Pranjal095 Aug 5, 2025
68ea8f1
Parametrize getattr/setattr in get_suggestion
encukou Aug 19, 2025
30d8043
Run test_suggestions_for_same_name for delattr too
encukou Aug 19, 2025
8c9c597
Merge pull request #1 from encukou/delattr-suggestions-re
Pranjal095 Aug 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ Other language changes
* Several error messages incorrectly using the term "argument" have been corrected.
(Contributed by Stan Ulbrych in :gh:`133382`.)

* The interpreter now provides helpful suggestions when :func:`delattr` fails due to a missing attribute. When an attribute name that closely resembles an existing attribute is used, the interpreter will suggest the correct attribute name in the error message. For example:

.. doctest::

>>> class A:
... pass
>>> a = A()
>>> a.abcde = 1
>>> del a.abcdf
Traceback (most recent call last):
...
AttributeError: 'A' object has no attribute 'abcdf'. Did you mean: 'abcde'?

(Contributed by [sobolevn] and Pranjal Prajapati in :gh:`136588`.)


New modules
Expand Down
7 changes: 4 additions & 3 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4200,9 +4200,10 @@ class B:
def method(self, name):
getattr(self, name)

self.assertIn("'_bluch'", self.get_suggestion(partial(B().method, '_blach')))
self.assertIn("'_bluch'", self.get_suggestion(partial(B().method, '_luch')))
self.assertIn("'_bluch'", self.get_suggestion(partial(B().method, 'bluch')))
obj = B()
self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, '_blach')))
self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, '_luch')))
self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, 'bluch')))


class DelattrSuggestionTests(BaseSuggestionTests):
Expand Down
2 changes: 1 addition & 1 deletion Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6939,7 +6939,7 @@ store_instance_attr_lock_held(PyObject *obj, PyDictValues *values,
PyErr_Format(PyExc_AttributeError,
"'%.100s' object has no attribute '%U'",
Py_TYPE(obj)->tp_name, name);
_PyObject_SetAttributeErrorContext(obj, name);
(void)_PyObject_SetAttributeErrorContext(obj, name);
return -1;
}

Expand Down
Loading