Title: Generate `AttributeError` messages from their context · Issue #153785 · python/cpython · GitHub
Open Graph Title: Generate `AttributeError` messages from their context · Issue #153785 · python/cpython
X Title: Generate `AttributeError` messages from their context · Issue #153785 · python/cpython
Description: Feature or enhancement Proposal: People very often raise AttributeError(name) (~40 occurrences in CPython repo) or raise AttributeError (bare, this is rarer), but when displaying the traceback this misses either the object targeted or bo...
Open Graph Description: Feature or enhancement Proposal: People very often raise AttributeError(name) (~40 occurrences in CPython repo) or raise AttributeError (bare, this is rarer), but when displaying the traceback this...
X Description: Feature or enhancement Proposal: People very often raise AttributeError(name) (~40 occurrences in CPython repo) or raise AttributeError (bare, this is rarer), but when displaying the traceback this...
Opengraph URL: https://github.com/python/cpython/issues/153785
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Generate `AttributeError` messages from their context","articleBody":"# Feature or enhancement\n\n### Proposal:\n\nPeople very often `raise AttributeError(name)` (~40 occurrences in CPython repo) or `raise AttributeError` (bare, this is rarer), but when displaying the traceback this misses either the object targeted or both the object and the attribute name.\n\nThat information is already there. `AttributeError.obj` and `AttributeError.name` is automatically set via `_PyObject_SetAttributeErrorContext` and now is only used to calculate attribute suggestions. Let's improve error messages with it too.\n\nConsider a few examples where the user could raise a bare `AttributeError` or `AttributeError(\"attr\")`, how we handle it (`-`), and how we *could* handle it (`+`):\n\n```py\nimport traceback\n\nclass Foo1:\n def __getattr__(self, name):\n raise AttributeError(name)\n\ntry:\n Foo1().missing1\nexcept AttributeError as a:\n print(f\"-{traceback.format_exc().splitlines()[-1]}\")\n print(f\"+AttributeError: {a.obj} has no attribute {a.name!r}\\n\")\n\nclass Foo2:\n def __getattr__(self, name):\n raise AttributeError\n\ntry:\n Foo2().missing2\nexcept AttributeError as a:\n print(f\"-{traceback.format_exc().splitlines()[-1]}\")\n print(f\"+AttributeError: {a.obj} has no attribute {a.name!r}\\n\")\n\nimport mod1 # def __getattr__(name): raise AttributeError(name)\ntry:\n mod1.missing3\nexcept AttributeError as a:\n print(f\"-{traceback.format_exc().splitlines()[-1]}\")\n print(f\"+AttributeError: {a.obj} has no attribute {a.name!r}\\n\")\n\nimport mod2 # def __getattr__(name): raise AttributeError\ntry:\n mod2.missing4\nexcept AttributeError as a:\n print(f\"-{traceback.format_exc().splitlines()[-1]}\")\n print(f\"+AttributeError: {a.obj} has no attribute {a.name!r}\\n\")\n```\n\n```diff\n-AttributeError: missing1\n+AttributeError: \u003c__main__.Foo1 object at 0x109f2c8a0> has no attribute 'missing1'\n\n-AttributeError\n+AttributeError: \u003c__main__.Foo2 object at 0x109f2c8a0> has no attribute 'missing2'\n\n-AttributeError: missing3\n+AttributeError: \u003cmodule 'mod1' from './Python/cpython/mod1.py'> has no attribute 'missing3'\n\n-AttributeError\n+AttributeError: \u003cmodule 'mod2' from './Python/cpython/mod2.py'> has no attribute 'missing4'\n```\n\nIf we generate messages from collected context, we'll also fix some edge cases in the stdlib and snake knows where else. Consider this `functools.singledispatchmethod` edge case:\n\n```py\nfrom functools import singledispatchmethod\n \nclass C:\n @singledispatchmethod\n def sdm(self, a):\n pass\n \nC().sdm.__code__\n```\n\nwhich currently fails with `-` but could be `+`:\n\n```diff\n-AttributeError: . Did you mean: '__call__'?\n+AttributeError: '_singledispatchmethod_get' object has no attribute '__code__'. Did you mean: '__call__'?\n```\n\n\n### Has this already been discussed elsewhere?\n\nThis is a minor feature, which does not need previous discussion elsewhere\n\n### Links to previous discussion of this feature:\n\n_No response_\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-153786\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/johnslavik","@type":"Person","name":"johnslavik"},"datePublished":"2026-07-15T23:40:10Z","url":"https://github.com/python/cpython/issues/153785"}
| 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:8695d20e-253c-b878-4ca6-c6c948740624 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C4B8:FC5BE:381956D:4A7E2E6:6B0211E9 |
| html-safe-nonce | dc6237e60d54e56b9b1b8d20a83691748a79438c5fa15044180b35f881add85a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNEI4OkZDNUJFOjM4MTk1NkQ6NEE3RTJFNjo2QjAyMTFFOSIsInZpc2l0b3JfaWQiOiIzNjkxMzQzMDgwNjYyODk3MTI5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 833512fd175609c20bf63697d8e03efc1881656da107318aa47d467ca2b42fab |
| hovercard-subject-tag | issue:4897204284 |
| 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/153785/issue_layout |
| twitter:image | https://opengraph.githubassets.com/19c8e63aa5b561539e50f0ccff6d899293bec9294124b0ea646b3f4655586f46/python/cpython/issues/153785 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/19c8e63aa5b561539e50f0ccff6d899293bec9294124b0ea646b3f4655586f46/python/cpython/issues/153785 |
| og:image:alt | Feature or enhancement Proposal: People very often raise AttributeError(name) (~40 occurrences in CPython repo) or raise AttributeError (bare, this is rarer), but when displaying the traceback this... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | johnslavik |
| 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