Skip to content

Commit fb654a4

Browse files
committed
Add requested changes
1 parent 9a361aa commit fb654a4

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

Doc/whatsnew/3.15.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,20 @@ Other language changes
175175
* Several error messages incorrectly using the term "argument" have been corrected.
176176
(Contributed by Stan Ulbrych in :gh:`133382`.)
177177

178+
* 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:
179+
180+
.. doctest::
181+
182+
>>> class A:
183+
... pass
184+
>>> a = A()
185+
>>> a.abcde = 1
186+
>>> del a.abcdf
187+
Traceback (most recent call last):
188+
...
189+
AttributeError: 'A' object has no attribute 'abcdf'. Did you mean: 'abcde'?
190+
191+
(Contributed by [sobolevn] and Pranjal Prajapati in :gh:`136588`.)
178192

179193

180194
New modules

Lib/test/test_traceback.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4200,9 +4200,10 @@ class B:
42004200
def method(self, name):
42014201
getattr(self, name)
42024202

4203-
self.assertIn("'_bluch'", self.get_suggestion(partial(B().method, '_blach')))
4204-
self.assertIn("'_bluch'", self.get_suggestion(partial(B().method, '_luch')))
4205-
self.assertIn("'_bluch'", self.get_suggestion(partial(B().method, 'bluch')))
4203+
obj = B()
4204+
self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, '_blach')))
4205+
self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, '_luch')))
4206+
self.assertIn("'_bluch'", self.get_suggestion(partial(obj.method, 'bluch')))
42064207

42074208

42084209
class DelattrSuggestionTests(BaseSuggestionTests):

Objects/dictobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6939,7 +6939,7 @@ store_instance_attr_lock_held(PyObject *obj, PyDictValues *values,
69396939
PyErr_Format(PyExc_AttributeError,
69406940
"'%.100s' object has no attribute '%U'",
69416941
Py_TYPE(obj)->tp_name, name);
6942-
_PyObject_SetAttributeErrorContext(obj, name);
6942+
(void)_PyObject_SetAttributeErrorContext(obj, name);
69436943
return -1;
69446944
}
69456945

0 commit comments

Comments
 (0)