Title: Add cross-language method suggestions for builtin AttributeError · Issue #146406 · python/cpython · GitHub
Open Graph Title: Add cross-language method suggestions for builtin AttributeError · Issue #146406 · python/cpython
X Title: Add cross-language method suggestions for builtin AttributeError · Issue #146406 · python/cpython
Description: Proposal When an AttributeError occurs on a builtin type (list, str, dict) and the existing Levenshtein-based suggestion system finds no match, check a static table of common method names from other programming languages. Before: >>> [1,...
Open Graph Description: Proposal When an AttributeError occurs on a builtin type (list, str, dict) and the existing Levenshtein-based suggestion system finds no match, check a static table of common method names from othe...
X Description: Proposal When an AttributeError occurs on a builtin type (list, str, dict) and the existing Levenshtein-based suggestion system finds no match, check a static table of common method names from othe...
Opengraph URL: https://github.com/python/cpython/issues/146406
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add cross-language method suggestions for builtin AttributeError","articleBody":"## Proposal\n\nWhen an `AttributeError` occurs on a builtin type (`list`, `str`, `dict`) and the existing Levenshtein-based suggestion system finds no match, check a static table of common method names from other programming languages.\n\n**Before:**\n```python\n>>> [1, 2, 3].push(4)\nAttributeError: 'list' object has no attribute 'push'\n```\n\n**After:**\n```python\n>>> [1, 2, 3].push(4)\nAttributeError: 'list' object has no attribute 'push'. Did you mean '.append' instead of '.push'?\n```\n\n## Discourse discussion\n\nThis was discussed at https://discuss.python.org/t/cross-language-method-suggestions-for-attributeerror/106632 (420 views, 25 likes, 3 core devs participated).\n\nDesign decisions from the discussion:\n\n- **Flat table approach** per @pf_moore ([post 14](https://discuss.python.org/t/106632/14), 4 likes): each (type, attr) entry carries the exact suggestion text. No runtime introspection of related types needed.\n\n- **list.add() suggests using a set** per @storchaka ([post 8](https://discuss.python.org/t/106632/8)) and @tjreedy ([post 12](https://discuss.python.org/t/106632/12)): \"It can also mean that you passed a list instead of set.\" The table maps this to a wrong-type hint rather than suggesting append.\n\n- **Option 1: static table for builtins only** - community consensus. 11 entries covering JS/Java/C#/Ruby. Inclusion criteria: evidence of real confusion, not catchable by Levenshtein, from a top-4 language.\n\n- **Scope guardrails** per @dr_carlos ([post 3](https://discuss.python.org/t/106632/3)): \"only add entries with clear evidence of real confusion, not just because two methods do similar things.\"\n\n## Examples\n\n| What user writes | Before | After |\n|---|---|---|\n| `[].push(4)` | no suggestion | Did you mean '.append'? |\n| `\"\".toUpperCase()` | no suggestion | Did you mean '.upper'? |\n| `{}.keySet()` | no suggestion | Did you mean '.keys'? |\n| `[].add(1)` | no suggestion | Did you mean to use a set? |\n| `\"\".trimStart()` | no suggestion | Did you mean '.lstrip'? |\n\nEntries where Levenshtein already provides a match (indexOf->index, trim->strip) are intentionally excluded from the table since they already work.\n\n## Implementation\n\n~60 lines in `Lib/traceback.py`: a dict mapping (builtin_type, wrong_name) to suggestion text, a small lookup function, and a 4-line hook in `TracebackException.__init__` that runs only when Levenshtein found nothing. Tests cover all entries plus priority ordering and subclass exclusion.\n\n## Has this already been discussed elsewhere?\n\nYes, on Discourse: https://discuss.python.org/t/106632\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-146407\n* gh-149927\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/mvanhorn","@type":"Person","name":"mvanhorn"},"datePublished":"2026-03-25T07:13:57Z","url":"https://github.com/python/cpython/issues/146406"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:af204ff5-4699-3d81-9b7b-9d06e3e415ed |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BCDC:148EE1:AFB9F8:DD52E5:6B0222E3 |
| html-safe-nonce | 69ed99db2711f604d85802480fb3460c40b04a769d56bc04f422d2ca3492961a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQ0RDOjE0OEVFMTpBRkI5Rjg6REQ1MkU1OjZCMDIyMkUzIiwidmlzaXRvcl9pZCI6IjYzMjYxMTYzOTY0NjM5NTY3MDciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 374a5a2e001764b69be2675294ccd7e921184c9eeed1fc8f091a74f66bcd078a |
| hovercard-subject-tag | issue:4133181967 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/146406/issue_layout |
| twitter:image | https://opengraph.githubassets.com/503a5edacda7c03c8eb06d632952d0eb06d0626ba500584d5649b45bb1d0e04f/python/cpython/issues/146406 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/503a5edacda7c03c8eb06d632952d0eb06d0626ba500584d5649b45bb1d0e04f/python/cpython/issues/146406 |
| og:image:alt | Proposal When an AttributeError occurs on a builtin type (list, str, dict) and the existing Levenshtein-based suggestion system finds no match, check a static table of common method names from othe... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mvanhorn |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width