gh-146406: Add cross-language method suggestions for builtin AttributeError#146407
Open
mvanhorn wants to merge 2 commits intopython:mainfrom
Open
gh-146406: Add cross-language method suggestions for builtin AttributeError#146407mvanhorn wants to merge 2 commits intopython:mainfrom
mvanhorn wants to merge 2 commits intopython:mainfrom
Conversation
When Levenshtein-based suggestions find no match for an AttributeError
on list, str, or dict, check a static table of common method names from
JavaScript, Java, C#, and Ruby.
For example, [].push() now suggests .append(), "".toUpperCase() suggests
.upper(), and {}.keySet() suggests .keys().
The list.add() case suggests using a set instead of suggesting .append(),
since .add() is a set method and the user may have passed a list where
a set was expected (per discussion with Serhiy Storchaka, Terry Reedy,
and Paul Moore).
Design: flat (type, attr) -> suggestion text table, no runtime
introspection. Only exact builtin types are matched to avoid false
positives on subclasses.
Discussion: https://discuss.python.org/t/106632
Contributor
Author
Member
|
I will make a review of this PR when I have time (by the end of the week), so fellow core devs, please hold off any merge, TiA! |
Member
|
Thanks for taking this on! From a language perspective, I think these can be shorter. Instead of: I think it is enough to say: or even: |
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
AttributeErroron a builtin type (list,str,dict) has noLevenshtein-based suggestion, check a static table of common method names
from other languages.
Before:
After:
Discourse discussion
Discussed at https://discuss.python.org/t/106632 (420 views, 25 likes, 15 posts, 3 core devs).
Design decisions from the thread:
Flat table per @pf_moore (post 14, 4 likes):
list.add() suggests set, not append per @Storchaka (post 8):
Reinforced by @tjreedy (post 12):
Scope guardrails per @dr_carlos (post 3): only add entries backed by real confusion evidence, not just because methods are similar.
Static table for builtins only (Option 1) - community consensus. 11 entries covering JavaScript, Java, C#, and Ruby.
Verification
Before (system Python 3.13, no cross-language hints):
After (this PR):
Levenshtein still takes priority (trim->strip, indexOf->index already work and are not in the table). Only exact builtin types are matched - subclasses are not affected.
Changes
Lib/traceback.py: Added_CROSS_LANGUAGE_HINTSdict (11 entries),_get_cross_language_hint()function, and a 4-line fallback hook inTracebackException.__init__that runs only when Levenshtein found nothing.Lib/test/test_traceback.py: 15 test cases covering all entries, priority ordering, unknown attrs, and subclass exclusion.Misc/NEWS.d/: NEWS entry.Evidence
elseif->elif(gh-132449),import x from y->from x import y(gh-98931)Fixes #146406