Skip to content

gh-146239: Add cross-language method suggestions to AttributeError messages#146240

Open
mvanhorn wants to merge 2 commits intopython:mainfrom
mvanhorn:osc/cross-language-attributeerror-hints
Open

gh-146239: Add cross-language method suggestions to AttributeError messages#146240
mvanhorn wants to merge 2 commits intopython:mainfrom
mvanhorn:osc/cross-language-attributeerror-hints

Conversation

@mvanhorn
Copy link
Contributor

@mvanhorn mvanhorn commented Mar 21, 2026

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:

What user writes Before After
[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

  • Added _CROSS_LANGUAGE_ATTR_HINTS table in Lib/traceback.py mapping common method names from JS/Java/C#/Ruby to Python equivalents for list, str, and dict
  • Added _check_cross_language_hint() function that checks exact builtin type (not subclasses) to avoid false positives
  • Hooked into _compute_suggestion_error() as a fallback after Levenshtein and nested attribute checks
  • Added tests for list, str, dict suggestions, subclass exclusion, and unknown attributes

Design decisions:

  • Fallback only: Levenshtein suggestions take priority. The cross-language table is checked only when Levenshtein finds no match.
  • Exact type check: Only list, str, dict are matched, not subclasses. A CustomList with intentionally different methods won't get false suggestions.
  • Pure Python: The lookup runs only when an error has already occurred, so there is no performance impact on normal execution.

Testing

  • Tests in Lib/test/test_traceback.py covering direct method equivalents for all three types, subclass exclusion, and unknown attribute handling.

Fixes #146239

This contribution was developed with AI assistance (Claude Code).

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add cross-language method suggestions to AttributeError messages

1 participant