Skip to content

Commit 8a39e32

Browse files
committed
Add What's New entry for cross-language AttributeError hints
1 parent 6d58cdc commit 8a39e32

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Doc/whatsnew/3.15.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,35 @@ Improved error messages
420420
^^^^^^^^^^^^^^
421421
AttributeError: 'Container' object has no attribute 'area'. Did you mean '.inner.area' instead of '.area'?
422422
423+
* When an :exc:`AttributeError` on a builtin type has no close match via
424+
Levenshtein distance, the error message now checks a static table of common
425+
method names from other languages (JavaScript, Java, Ruby, C#) and suggests
426+
the Python equivalent:
427+
428+
.. doctest::
429+
430+
>>> [1, 2, 3].push(4) # doctest: +ELLIPSIS
431+
Traceback (most recent call last):
432+
...
433+
AttributeError: 'list' object has no attribute 'push'. Did you mean '.append'?
434+
435+
>>> 'hello'.toUpperCase() # doctest: +ELLIPSIS
436+
Traceback (most recent call last):
437+
...
438+
AttributeError: 'str' object has no attribute 'toUpperCase'. Did you mean '.upper'?
439+
440+
When the Python equivalent is a language construct rather than a method,
441+
the hint describes the construct directly:
442+
443+
.. doctest::
444+
445+
>>> {}.put("a", 1) # doctest: +ELLIPSIS
446+
Traceback (most recent call last):
447+
...
448+
AttributeError: 'dict' object has no attribute 'put'. Use dict[key] = value for item assignment
449+
450+
(Contributed by Matt Van Horn in :gh:`146406`.)
451+
423452

424453
Other language changes
425454
======================

0 commit comments

Comments
 (0)