gh-146239: Add cross-language method suggestions to AttributeError messages#146240
Closed
mvanhorn wants to merge 2 commits intopython:mainfrom
Closed
gh-146239: Add cross-language method suggestions to AttributeError messages#146240mvanhorn wants to merge 2 commits intopython:mainfrom
mvanhorn wants to merge 2 commits intopython:mainfrom
Conversation
…ror messages (python#146239) When an attribute name from another programming language is used on a builtin type (e.g., list.push from JavaScript, str.toUpperCase from Java), Python now suggests the equivalent Python method (e.g., append, upper). This runs as a fallback after the existing Levenshtein-based suggestion matching.
Use the blurb-compatible naming convention: YYYY-MM-DD-HH-MM-SS.gh-issue-NNNNN.NONCE.rst
Member
|
See issue for the rationale for closing this for now. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When an attribute name from another programming language is used on a builtin type, Python now suggests the equivalent Python method as a fallback after the existing Levenshtein-based suggestion matching.
Why this matters
Python's Levenshtein-based error suggestions work well for typos (
items.appnd->append) but miss cross-language method confusion, where developers use method names from JavaScript, Java, C#, or Ruby that are too different for edit distance to catch.Before/after examples:
[1,2,3].push(4)no attribute 'push'(no suggestion)Did you mean '.append'?"hello".toUpperCase()no attribute 'toUpperCase'(no suggestion)Did you mean '.upper'?"hello".trim()no attribute 'trim'(no suggestion)Did you mean '.strip'?{"a": 1}.keySet()no attribute 'keySet'(no suggestion)Did you mean '.keys'?{"a": 1}.entrySet()no attribute 'entrySet'(no suggestion)Did you mean '.items'?This fits within the ongoing work to improve Python's error messages (#146171, #145241).
Changes
_CROSS_LANGUAGE_ATTR_HINTStable inLib/traceback.pymapping common method names from JS/Java/C#/Ruby to Python equivalents forlist,str, anddict_check_cross_language_hint()function that checks exact builtin type (not subclasses) to avoid false positives_compute_suggestion_error()as a fallback after Levenshtein and nested attribute checksDesign decisions:
list,str,dictare matched, not subclasses. ACustomListwith intentionally different methods won't get false suggestions.Testing
Lib/test/test_traceback.pycovering direct method equivalents for all three types, subclass exclusion, and unknown attribute handling.Fixes #146239
This contribution was developed with AI assistance (Claude Code).