René's URL Explorer Experiment


Title: gh-158140: Do not eagerly import sibling submodules on lazy import by abhinav-phi · Pull Request #158491 · python/cpython · GitHub

Open Graph Title: gh-158140: Do not eagerly import sibling submodules on lazy import by abhinav-phi · Pull Request #158491 · python/cpython

X Title: gh-158140: Do not eagerly import sibling submodules on lazy import by abhinav-phi · Pull Request #158491 · python/cpython

Description: Fixes #158140. Root cause lazy import pkg.a binds the name pkg, but _PyImport_LazyImportModuleLevelObject() created the lazy_import placeholder for the full dotted target pkg.a. Reifying a placeholder calls __import__(), so reading pkg ran the whole pkg → pkg.a chain eagerly. With two lazy imports of submodules of the same package the second statement simply overwrote the first one's pkg binding, so only pkg.b stayed reachable and reading pkg imported pkg.b — exactly the behaviour reported. Fix The placeholder is now created for the root package only (pkg), which is the name the statement actually binds. The existing lazy-submodule registry (register_lazy_on_parent()) already records pkg.a at every level, so pkg.a is imported by _Py_module_getattro_impl() the first time that attribute is read, and nothing before that. Two details keep the rest of the feature's behaviour intact: Names from a plain lazy import a.b are now kept in a separate interpreter-level registry from names coming from lazy from a import b. A from-import name may be a plain attribute of its module (collections.namedtuple), so it must still be resolved after the module dict; a plain lazy import name is always a submodule and is resolved before the dict. Resolving it first is also what keeps lazy import a.b as c returning the submodule even when a variable of the same name shadows the attribute, matching eager import a.b as c. A registered submodule that cannot be imported now raises the same ModuleNotFoundError the eager import would, by re-running the normal (non-lazy) finder so importlib produces its own message instead of duplicating it. It stays quiet for hasattr() / getattr(..., default), whenever the module defines __getattr__, and it does report the error while replaying the lookups of lazy import a.b as c, where _PyEval_ImportFrom() would otherwise suppress it. Repro main.py: lazy import pkg.a lazy import pkg.b print(globals()['pkg']) pkg.a where pkg/__init__.py prints "pkg", pkg/a.py prints "pkg.a" and pkg/b.py prints "pkg.b". Before: pkg pkg.b pkg.a After: pkg pkg.a pkg.b is no longer present in sys.modules afterwards. Tests Lib/test/test_lazy_import/__init__.py: added test_lazy_import_sibling_submodules (two lazy imports in one package; using .a must not import .b) and test_lazy_import_binds_root_placeholder (the placeholder names the root package it reifies to), with a new data/siblingpkg/ fixture package. Both fail on unpatched main and pass with this change. Updated two existing tests that pinned the old placeholder repr () and the name handed to a custom __import__ hook. Both are direct consequences of the placeholder now naming the root package; the properties those tests exist for (an empty fromlist behaving like None, and the fromlist being preserved for custom import hooks) are unchanged. NEWS entry added under Misc/NEWS.d/next/Core_and_Builtins/. Results on a --with-pydebug build of CPython 3.16.0a0 at main (00307b0e693): suite result test_lazy_import 141 tests, all pass test_import 128 pass test_importlib 1219 pass test_module 39 pass test_sys 102 pass test_builtin 157 pass test_capi 1581 pass test_pyclbr 6 pass AI tools were used assistively; I reviewed and tested every change and take responsibility for it. Issue: gh-158140

Open Graph Description: Fixes #158140. Root cause lazy import pkg.a binds the name pkg, but _PyImport_LazyImportModuleLevelObject() created the lazy_import placeholder for the full dotted target pkg.a. Reifying a placehol...

X Description: Fixes #158140. Root cause lazy import pkg.a binds the name pkg, but _PyImport_LazyImportModuleLevelObject() created the lazy_import placeholder for the full dotted target pkg.a. Reifying a placehol...

Opengraph URL: https://github.com/python/cpython/pull/158491

X: @github

direct link

Domain: github.com

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:92abf70a-6ae4-692e-4b6b-bb63781cf322
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-id9F94:1087D4:320A771:42E07EF:6B01F7FF
html-safe-nonce2d1f37a020d6750f30978014d630291c5efbb72840d5882fbb6bc73c7d536615
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5Rjk0OjEwODdENDozMjBBNzcxOjQyRTA3RUY6NkIwMUY3RkYiLCJ2aXNpdG9yX2lkIjoiNDU0MjMwNjgxMDc0MDIwOTY2MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac3ce8788fccb226a2d2d866e836ed4dfa974fc0a7f3d42a1f8a19a67898b610a1
hovercard-subject-tagpull_request:4689377662
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/python/cpython/pull/158491/files
twitter:imagehttps://avatars.githubusercontent.com/u/213936784?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/213936784?s=400&v=4
og:image:altFixes #158140. Root cause lazy import pkg.a binds the name pkg, but _PyImport_LazyImportModuleLevelObject() created the lazy_import placeholder for the full dotted target pkg.a. Reifying a placehol...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetcanary-1
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/pull/158491/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F158491%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F158491%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/pull/158491/files
Reloadhttps://github.com/python/cpython/pull/158491/files
Reloadhttps://github.com/python/cpython/pull/158491/files
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
abhinav-phihttps://github.com/abhinav-phi
python:mainhttps://github.com/python/cpython/tree/main
abhinav-phi:fix-158140-lazy-import-siblinghttps://github.com/abhinav-phi/cpython/tree/fix-158140-lazy-import-sibling
Conversation 1 https://github.com/python/cpython/pull/158491
Commits 1 https://github.com/python/cpython/pull/158491/commits
Checks 52 https://github.com/python/cpython/pull/158491/checks
Files changed https://github.com/python/cpython/pull/158491/files
Please reload this pagehttps://github.com/python/cpython/pull/158491/files
gh-158140: Do not eagerly import sibling submodules on lazy import https://github.com/python/cpython/pull/158491/files#top
Show all changes 1 commit https://github.com/python/cpython/pull/158491/files
f4ae317 gh-158140: Do not eagerly import sibling submodules on lazy import abhinav-phi Sep 30, 2026 https://github.com/python/cpython/pull/158491/commits/f4ae3177538173638e333789aaa84326075d6f55
Clear filters https://github.com/python/cpython/pull/158491/files
Please reload this pagehttps://github.com/python/cpython/pull/158491/files
Please reload this pagehttps://github.com/python/cpython/pull/158491/files
pycore_import.h https://github.com/python/cpython/pull/158491/files#diff-f0d0635a621a67c4105f6b306ecabc7b6f8e1fe1518ae49baf006c26f71520b5
pycore_interp_structs.h https://github.com/python/cpython/pull/158491/files#diff-6075b6142803c1eccc213aa150704fd639443c65311d5999f69a5ea77cb76756
__init__.py https://github.com/python/cpython/pull/158491/files#diff-9c7ed57cb280bfcf280bf31daf9747c91ca26b50196a9f84c1a38f105d83b31e
__init__.py https://github.com/python/cpython/pull/158491/files#diff-30fd54ab335a173134120747c0fd5ea1fc2404cb7ef2d914891191ea316eb1d7
a.py https://github.com/python/cpython/pull/158491/files#diff-4edd5a0e2c678bfdae3db8b0f7088bf35833fe5db636b98aa7aff12ab8e99184
b.py https://github.com/python/cpython/pull/158491/files#diff-1043ac8b42095123e2d62f04a47a85aa37f45ffb4807e8f02dacea353f023395
2026-09-30-00-00-00.gh-issue-158140.DnBBpx.rst https://github.com/python/cpython/pull/158491/files#diff-5c8a387f31f06a223162916a62fa33928169b03486eda9c875489946f9f5e2ba
moduleobject.c https://github.com/python/cpython/pull/158491/files#diff-8c2a0fd137780a0ee11c19aae8e46e8490f7667698cd78a5bde466bc995a08c5
import.c https://github.com/python/cpython/pull/158491/files#diff-28cfc3e2868980a79d93d2ebdc8747dcb9231f3dd7f2caef96e74107d1ea3bf3
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L261
Include/internal/pycore_import.hhttps://github.com/python/cpython/pull/158491/files#diff-f0d0635a621a67c4105f6b306ecabc7b6f8e1fe1518ae49baf006c26f71520b5
View file https://github.com/abhinav-phi/cpython/blob/f4ae3177538173638e333789aaa84326075d6f55/Include/internal/pycore_import.h
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/158491/{{ revealButtonHref }}
https://github.com/python/cpython/pull/158491/files#diff-f0d0635a621a67c4105f6b306ecabc7b6f8e1fe1518ae49baf006c26f71520b5
https://github.com/python/cpython/pull/158491/files#diff-f0d0635a621a67c4105f6b306ecabc7b6f8e1fe1518ae49baf006c26f71520b5
Include/internal/pycore_interp_structs.hhttps://github.com/python/cpython/pull/158491/files#diff-6075b6142803c1eccc213aa150704fd639443c65311d5999f69a5ea77cb76756
View file https://github.com/abhinav-phi/cpython/blob/f4ae3177538173638e333789aaa84326075d6f55/Include/internal/pycore_interp_structs.h
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/158491/{{ revealButtonHref }}
https://github.com/python/cpython/pull/158491/files#diff-6075b6142803c1eccc213aa150704fd639443c65311d5999f69a5ea77cb76756
https://github.com/python/cpython/pull/158491/files#diff-6075b6142803c1eccc213aa150704fd639443c65311d5999f69a5ea77cb76756
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L299
Lib/test/test_lazy_import/__init__.pyhttps://github.com/python/cpython/pull/158491/files#diff-9c7ed57cb280bfcf280bf31daf9747c91ca26b50196a9f84c1a38f105d83b31e
View file https://github.com/abhinav-phi/cpython/blob/f4ae3177538173638e333789aaa84326075d6f55/Lib/test/test_lazy_import/__init__.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/158491/{{ revealButtonHref }}
https://github.com/python/cpython/pull/158491/files#diff-9c7ed57cb280bfcf280bf31daf9747c91ca26b50196a9f84c1a38f105d83b31e
https://github.com/python/cpython/pull/158491/files#diff-9c7ed57cb280bfcf280bf31daf9747c91ca26b50196a9f84c1a38f105d83b31e
https://github.com/python/cpython/pull/158491/files#diff-9c7ed57cb280bfcf280bf31daf9747c91ca26b50196a9f84c1a38f105d83b31e
https://github.com/python/cpython/pull/158491/files#diff-9c7ed57cb280bfcf280bf31daf9747c91ca26b50196a9f84c1a38f105d83b31e
https://github.com/python/cpython/pull/158491/files#diff-9c7ed57cb280bfcf280bf31daf9747c91ca26b50196a9f84c1a38f105d83b31e
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L299
Lib/test/test_lazy_import/data/siblingpkg/__init__.pyhttps://github.com/python/cpython/pull/158491/files#diff-30fd54ab335a173134120747c0fd5ea1fc2404cb7ef2d914891191ea316eb1d7
View file https://github.com/abhinav-phi/cpython/blob/f4ae3177538173638e333789aaa84326075d6f55/Lib/test/test_lazy_import/data/siblingpkg/__init__.py
Open in desktop https://desktop.github.com
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L299
Lib/test/test_lazy_import/data/siblingpkg/a.pyhttps://github.com/python/cpython/pull/158491/files#diff-4edd5a0e2c678bfdae3db8b0f7088bf35833fe5db636b98aa7aff12ab8e99184
View file https://github.com/abhinav-phi/cpython/blob/f4ae3177538173638e333789aaa84326075d6f55/Lib/test/test_lazy_import/data/siblingpkg/a.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/158491/{{ revealButtonHref }}
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L299
Lib/test/test_lazy_import/data/siblingpkg/b.pyhttps://github.com/python/cpython/pull/158491/files#diff-1043ac8b42095123e2d62f04a47a85aa37f45ffb4807e8f02dacea353f023395
View file https://github.com/abhinav-phi/cpython/blob/f4ae3177538173638e333789aaa84326075d6f55/Lib/test/test_lazy_import/data/siblingpkg/b.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/158491/{{ revealButtonHref }}
Misc/NEWS.d/next/Core_and_Builtins/2026-09-30-00-00-00.gh-issue-158140.DnBBpx.rsthttps://github.com/python/cpython/pull/158491/files#diff-5c8a387f31f06a223162916a62fa33928169b03486eda9c875489946f9f5e2ba
View file https://github.com/abhinav-phi/cpython/blob/f4ae3177538173638e333789aaa84326075d6f55/Misc/NEWS.d/next/Core_and_Builtins/2026-09-30-00-00-00.gh-issue-158140.DnBBpx.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/158491/{{ revealButtonHref }}
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L267
Objects/moduleobject.chttps://github.com/python/cpython/pull/158491/files#diff-8c2a0fd137780a0ee11c19aae8e46e8490f7667698cd78a5bde466bc995a08c5
View file https://github.com/abhinav-phi/cpython/blob/f4ae3177538173638e333789aaa84326075d6f55/Objects/moduleobject.c
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/158491/{{ revealButtonHref }}
https://github.com/python/cpython/pull/158491/files#diff-8c2a0fd137780a0ee11c19aae8e46e8490f7667698cd78a5bde466bc995a08c5
https://github.com/python/cpython/pull/158491/files#diff-8c2a0fd137780a0ee11c19aae8e46e8490f7667698cd78a5bde466bc995a08c5
https://github.com/python/cpython/pull/158491/files#diff-8c2a0fd137780a0ee11c19aae8e46e8490f7667698cd78a5bde466bc995a08c5
https://github.com/python/cpython/pull/158491/files#diff-8c2a0fd137780a0ee11c19aae8e46e8490f7667698cd78a5bde466bc995a08c5
https://github.com/python/cpython/pull/158491/files#diff-8c2a0fd137780a0ee11c19aae8e46e8490f7667698cd78a5bde466bc995a08c5
https://github.com/python/cpython/pull/158491/files#diff-8c2a0fd137780a0ee11c19aae8e46e8490f7667698cd78a5bde466bc995a08c5
Please reload this pagehttps://github.com/python/cpython/pull/158491/files
Please reload this pagehttps://github.com/python/cpython/pull/158491/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.