Title: `lazy import a.b as c` incorrectly binds to `a.b.b` · Issue #157614 · python/cpython · GitHub
Open Graph Title: `lazy import a.b as c` incorrectly binds to `a.b.b` · Issue #157614 · python/cpython
X Title: `lazy import a.b as c` incorrectly binds to `a.b.b` · Issue #157614 · python/cpython
Description: Bug report (Edited by @johnslavik) To reproduce: import importlib.metadata import importlib.metadata as x lazy import importlib.metadata as lazy_x print("type(x): ", type(x).__name__) # reifies to importlib.metadata print("type(lazy_x):"...
Open Graph Description: Bug report (Edited by @johnslavik) To reproduce: import importlib.metadata import importlib.metadata as x lazy import importlib.metadata as lazy_x print("type(x): ", type(x).__name__) # reifies to ...
X Description: Bug report (Edited by @johnslavik) To reproduce: import importlib.metadata import importlib.metadata as x lazy import importlib.metadata as lazy_x print("type(x): ", type(x).__name__) # r...
Opengraph URL: https://github.com/python/cpython/issues/157614
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`lazy import a.b as c` incorrectly binds to `a.b.b`","articleBody":"# Bug report\n\n(Edited by @johnslavik)\n\nTo reproduce:\n\n```python\nimport importlib.metadata\nimport importlib.metadata as x\nlazy import importlib.metadata as lazy_x\n\nprint(\"type(x): \", type(x).__name__) # reifies to importlib.metadata\nprint(\"type(lazy_x):\", type(lazy_x).__name__) # reifies to importlib.metadata.metadata\n\nx.distributions() # OK\nlazy_x.distributions() # AttributeError: 'function' object has no attribute 'distributions'\n```\n\n`x` is the `importlib.metadata` module, as expected. `lazy_x` is instead reified to `importlib.metadata.metadata`, the function.\n\nThe bug is in cached name resolution.\n\nFor `lazy import a.b as c`, the issue reproduces when:\n\n1. `a.b` is already in `sys.modules`.\n2. `a.b` has an attribute named `b`.\n3. `c` is reified.\n\n### Possible root cause\n\nThe problem may be in `_PyEval_LazyImportFrom`: [`[Python/ceval.c:3305-3361](https://github.com/python/cpython/blob/cf312cfa64dcaa1700531ec8bc76d56ddd65fb15/Python/ceval.c#L3305-L3361)`](https://github.com/python/cpython/blob/cf312cfa64dcaa1700531ec8bc76d56ddd65fb15/Python/ceval.c#L3305-L3361) (`3.15` at `cf312cfa64dcaa1700531ec8bc76d56ddd65fb15`, `v3.15.0rc2-22-gcf312cf`).\n\nThe `sys.modules` fast path at lines 3313-3331 runs before the base-module computation at 3333-3360 and looks up `name` directly in `d->lz_from`.\n\nIn the `IMPORT_FROM` chain generated for `import a.b.c as x`, however, `d->lz_from` does not appear to be the module the attribute should be taken from: on the first step it is the full dotted name, and on later steps it is one component short of the base.\n\n### CPython versions tested on:\n\n3.15\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-157626\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/grgalex","@type":"Person","name":"grgalex"},"datePublished":"2026-09-16T10:32:22Z","url":"https://github.com/python/cpython/issues/157614"}
| 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:c31a464f-722f-edc2-14d7-2788b4806dfd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B52A:B53AF:3532235:46D0503:6B020261 |
| html-safe-nonce | c0440e27feb43a13c8cd645588b70e0df874bf0137eb103cded474a9da4be995 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNTJBOkI1M0FGOjM1MzIyMzU6NDZEMDUwMzo2QjAyMDI2MSIsInZpc2l0b3JfaWQiOiI3ODc0NDg5MjQwOTU5MTg5NjAxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 8d1e3ee9858322267d1ed63126f0dadc96e8e3aeddfc60a42343ecdced97a9cc |
| hovercard-subject-tag | issue:5473359343 |
| 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/157614/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6c5d950da443ce4053c42770c9347beae9bc47b8a488178581626057925990c4/python/cpython/issues/157614 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6c5d950da443ce4053c42770c9347beae9bc47b8a488178581626057925990c4/python/cpython/issues/157614 |
| og:image:alt | Bug report (Edited by @johnslavik) To reproduce: import importlib.metadata import importlib.metadata as x lazy import importlib.metadata as lazy_x print("type(x): ", type(x).__name__) # reifies to ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | grgalex |
| 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