Title: Lazy imports and resolving type hints · Issue #156924 · python/cpython · GitHub
Open Graph Title: Lazy imports and resolving type hints · Issue #156924 · python/cpython
X Title: Lazy imports and resolving type hints · Issue #156924 · python/cpython
Description: Feature or enhancement Proposal: annotationlib implements an optimization to resolve forward references in ForwardRef.evaluate(), by doing a simple lookup in the namespace: cpython/Lib/annotationlib.py Lines 193 to 210 in 112560e if arg....
Open Graph Description: Feature or enhancement Proposal: annotationlib implements an optimization to resolve forward references in ForwardRef.evaluate(), by doing a simple lookup in the namespace: cpython/Lib/annotationli...
X Description: Feature or enhancement Proposal: annotationlib implements an optimization to resolve forward references in ForwardRef.evaluate(), by doing a simple lookup in the namespace: cpython/Lib/annotationli...
Opengraph URL: https://github.com/python/cpython/issues/156924
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Lazy imports and resolving type hints","articleBody":"# Feature or enhancement\n\n### Proposal:\n\n`annotationlib` implements an optimization to resolve forward references in `ForwardRef.evaluate()`, by doing a simple lookup in the namespace:\n\nhttps://github.com/python/cpython/blob/112560ee34a70c02baebdc11519d1f77e90ecab8/Lib/annotationlib.py#L193-L210\n\nThis gives slightly inconsistent results with lazy imports that fail to resolve:\n\n```python\n# lazy_mod.py\n\nAlias = list\n\nraise Exception()\n```\n\n```python\nfrom annotationlib import Format, get_annotations\n\nlazy from lazy_mod import Alias\n\nclass A:\n a: Alias\n\nclass B:\n a: Alias[int]\n\nget_annotations(A, format=Format.FORWARDREF)\n#> {'a': \u003clazy_import 'failing_mod.Alias'>}\nget_annotations(B, format=Format.FORWARDREF)\n#> {'a': ForwardRef('Alias[int]', is_class=True, owner=\u003cclass '__main__.B'>)}\n```\n\nI'm wondering if for `A`, it should instead wrap `a`'s annotation in a `ForwardRef`, so that users can then call `evaluate()` on it (e.g. with `Format.VALUE`) so that the exception from `lazy_mod` gets raised accordingly. Alternatively, the lazy import instance could be kept as is, but it would be great to document the `resolve()` method on [`types.LazyImportType`](https://docs.python.org/3.15/library/types.html#types.LazyImportType).\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-156940\n* gh-157224\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/Viicos","@type":"Person","name":"Viicos"},"datePublished":"2026-09-04T10:13:44Z","url":"https://github.com/python/cpython/issues/156924"}
| 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:37b85135-2583-424b-036d-2d51e78d3da7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 85D4:21AC3F:3E81575:4F1AEE9:6B01F256 |
| html-safe-nonce | fc56d1b934ca30916c1c59e88b0d97d3f12b2bfe22a8b902a66a1393f70eb245 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NUQ0OjIxQUMzRjozRTgxNTc1OjRGMUFFRTk6NkIwMUYyNTYiLCJ2aXNpdG9yX2lkIjoiMjM2NDI3ODUxMDY3NzI1ODgzOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5e704e5e5a713c4a63a76fdad6e350ceb799da11e3b2e8910dbd87ddae484dd2 |
| hovercard-subject-tag | issue:5346859877 |
| 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/156924/issue_layout |
| twitter:image | https://opengraph.githubassets.com/07e291de5cbd319469bb0fb28ea0ae81727d57460acc6d5f623998824d3ece6e/python/cpython/issues/156924 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/07e291de5cbd319469bb0fb28ea0ae81727d57460acc6d5f623998824d3ece6e/python/cpython/issues/156924 |
| og:image:alt | Feature or enhancement Proposal: annotationlib implements an optimization to resolve forward references in ForwardRef.evaluate(), by doing a simple lookup in the namespace: cpython/Lib/annotationli... |
| 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 | 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