Title: Raise consistent `NameError` in `ForwardRef.evaluate()` · Issue #135646 · python/cpython · GitHub
Open Graph Title: Raise consistent `NameError` in `ForwardRef.evaluate()` · Issue #135646 · python/cpython
X Title: Raise consistent `NameError` in `ForwardRef.evaluate()` · Issue #135646 · python/cpython
Description: Feature or enhancement Proposal: The new ForwardRef.evaluate() implementation had a fast path introduced in #124337 to not call eval(): cpython/Lib/annotationlib.py Lines 177 to 187 in cb39410 if arg.isidentifier() and not keyword.iskeyw...
Open Graph Description: Feature or enhancement Proposal: The new ForwardRef.evaluate() implementation had a fast path introduced in #124337 to not call eval(): cpython/Lib/annotationlib.py Lines 177 to 187 in cb39410 if a...
X Description: Feature or enhancement Proposal: The new ForwardRef.evaluate() implementation had a fast path introduced in #124337 to not call eval(): cpython/Lib/annotationlib.py Lines 177 to 187 in cb39410 if a...
Opengraph URL: https://github.com/python/cpython/issues/135646
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Raise consistent `NameError` in `ForwardRef.evaluate()`","articleBody":"# Feature or enhancement\n\n### Proposal:\n\nThe new `ForwardRef.evaluate()` implementation had a fast path introduced in https://github.com/python/cpython/pull/124337 to _not_ call `eval()`:\n\nhttps://github.com/python/cpython/blob/cb394101110e13a27e08bbf2fe9f38d847db004c/Lib/annotationlib.py#L177-L187\n\nHowever, when the `NameError` is raised, the format is different from the one raised by `eval()`:\n\nThe exception message is defined as:\n\nhttps://github.com/python/cpython/blob/cb394101110e13a27e08bbf2fe9f38d847db004c/Python/ceval_macros.h#L308\n\nand raised this way:\n\nhttps://github.com/python/cpython/blob/cb394101110e13a27e08bbf2fe9f38d847db004c/Python/ceval.c#L3339-L3351\n\nTo have consistent exceptions raised, would it be possible to change the Python fast path implementation to match the C eval code?\n\n```diff\ndiff --git a/Lib/annotationlib.py b/Lib/annotationlib.py\nindex 731817a9973..c83a1573ccd 100644\n--- a/Lib/annotationlib.py\n+++ b/Lib/annotationlib.py\n@@ -27,6 +27,9 @@ class Format(enum.IntEnum):\n \n \n _sentinel = object()\n+# Following `NAME_ERROR_MSG` in `ceval_macros.h`:\n+_NAME_ERROR_MSG = \"name '{name:.200}' is not defined\"\n+\n \n # Slots shared by ForwardRef and _Stringifier. The __forward__ names must be\n # preserved for compatibility with the old typing.ForwardRef class. The remaining\n@@ -184,7 +187,7 @@ def evaluate(\n elif is_forwardref_format:\n return self\n else:\n- raise NameError(arg)\n+ raise NameError(_NAME_ERROR_MSG.format(name=arg), name=arg)\n else:\n code = self.__forward_code__\n try:\n```\n\nThis requires `_NAME_ERROR_MSG` to be in sync with the one from `ceval_macros.h`.\n\nOr at least, the `NameError` should have its `name` property set (especially as type stubs shows `NameError.name` as `str` currently).\n\ncc @JelleZijlstra.\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 --\u003e\n### Linked PRs\n* gh-135663\n* gh-135673\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/Viicos","@type":"Person","name":"Viicos"},"datePublished":"2025-06-17T22:49:54.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/135646/cpython/issues/135646"}
| 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:1215eb44-e863-7e27-b805-7ec964a9773f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DADC:2C3E22:A82302:D9805B:696AF7A7 |
| html-safe-nonce | d99f340879e9e5985b9b8c34a3e3c147cf9a71a5855c2d17caf9394c073db8bc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQURDOjJDM0UyMjpBODIzMDI6RDk4MDVCOjY5NkFGN0E3IiwidmlzaXRvcl9pZCI6IjY2MTE0MDc1Njc1NTQ1NDE0NzkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 6b4838c47a2593044ef41f8188aff021de72f95f47b9f383eb785333ca7279c5 |
| hovercard-subject-tag | issue:3154943025 |
| 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/135646/issue_layout |
| twitter:image | https://opengraph.githubassets.com/aa9b4822410175f0e9c6aaba6047dfc85ba93bf100ae6405d4fee98776d180ee/python/cpython/issues/135646 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/aa9b4822410175f0e9c6aaba6047dfc85ba93bf100ae6405d4fee98776d180ee/python/cpython/issues/135646 |
| og:image:alt | Feature or enhancement Proposal: The new ForwardRef.evaluate() implementation had a fast path introduced in #124337 to not call eval(): cpython/Lib/annotationlib.py Lines 177 to 187 in cb39410 if a... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Viicos |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| 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 | 82560a55c6b2054555076f46e683151ee28a19bc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width