Title: test.support.warnings_helper.check_warnings() raises KeyError: 'warnings' under -X lazy_imports=all · Issue #156570 · python/cpython · GitHub
Open Graph Title: test.support.warnings_helper.check_warnings() raises KeyError: 'warnings' under -X lazy_imports=all · Issue #156570 · python/cpython
X Title: test.support.warnings_helper.check_warnings() raises KeyError: 'warnings' under -X lazy_imports=all · Issue #156570 · python/cpython
Description: Bug report test.support.warnings_helper.check_warnings() raises KeyError: 'warnings' under -X lazy_imports=all, taking down every test that uses it. Reproducer: ./python -X lazy_imports=all -m test test_sundry test_time Lib/test/support/...
Open Graph Description: Bug report test.support.warnings_helper.check_warnings() raises KeyError: 'warnings' under -X lazy_imports=all, taking down every test that uses it. Reproducer: ./python -X lazy_imports=all -m test...
X Description: Bug report test.support.warnings_helper.check_warnings() raises KeyError: 'warnings' under -X lazy_imports=all, taking down every test that uses it. Reproducer: ./python -X lazy_imports=all...
Opengraph URL: https://github.com/python/cpython/issues/156570
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"test.support.warnings_helper.check_warnings() raises KeyError: 'warnings' under -X lazy_imports=all","articleBody":"# Bug report\n\n`test.support.warnings_helper.check_warnings()` raises `KeyError: 'warnings'`\nunder `-X lazy_imports=all`, taking down every test that uses it.\n\nReproducer:\n\n ./python -X lazy_imports=all -m test test_sundry test_time\n\n`Lib/test/support/warnings_helper.py:176`:\n\n # Because test_warnings swap the module, we need to look up in the\n # sys.modules dictionary.\n wmod = sys.modules['warnings']\n\n`warnings` is not in `sys.modules` at interpreter startup, with or without lazy\nimports, so that key is populated solely by the module-level `import warnings`\nat the top of this same file. Reading the module out of `sys.modules` by\nsubscript means the global name is never touched, so under lazy imports the\nimport is never resolved and the key is never created.\n\n41 files under `Lib/test/` use `check_warnings()` / `check_no_warnings()`.\n\nOne-line fix — the default argument is evaluated on every call, which resolves\nthe lazy import before the lookup, and the module swap done by `test_warnings`\nstill wins:\n\n wmod = sys.modules.get('warnings', warnings)\n\nVerified on `main` (d9154924138): with that change `test_sundry`, `test_time`\nand `test_warnings` pass under `-X lazy_imports=all`; `test___all__`,\n`test_sundry`, `test_time`, `test_warnings` and `test_support` still pass\nwithout the flag.\n\n`test_sundry` and `test_time` are not in the exclusion list proposed in\nGH-151105, so this is a failure mode the planned CI would not have covered.\n\nThe same pattern appears in the tests themselves. After the helper is fixed,\n`test___all__` still fails at `Lib/test/test___all__.py:49`:\n\n if not hasattr(sys.modules[modname], \"__all__\"):\n\nwith `KeyError: '_osx_support'` — the module is imported by\n`exec(\"import %s\" % modname)` a few lines above, which under lazy imports\nleaves `sys.modules` untouched. Lines 71 and 76 do the same thing.\n\n`grep -rn 'sys\\.modules\\[' Lib/test/` reports 126 subscript reads. Most are\nlegitimate identity checks after an eager import; the ones worth auditing are\nthose where the key is expected to be filled by an import in the same file, or\nby an `exec`ed import statement.\n\nRelated to #149640.\n\n### CPython versions tested on:\n\nCPython main branch (d9154924138)\n\n### Operating systems tested on:\n\nLinux\n\n---\n\n🤖 Investigated and drafted with [Claude Code](https://claude.com/claude-code),\nmodel Claude Opus 5 (1M context).\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-156573\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/ashm-dev","@type":"Person","name":"ashm-dev"},"datePublished":"2026-08-29T12:01:47Z","url":"https://github.com/python/cpython/issues/156570"}
| 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:af56e59a-8a2d-1350-d7d1-9d11ecf25618 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8588:3B07B3:1D9346:2559F9:6B01F255 |
| html-safe-nonce | e57ef352e2e95f49d19dbd5746cf192836a10177791d31f3ecfc293d59210630 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NTg4OjNCMDdCMzoxRDkzNDY6MjU1OUY5OjZCMDFGMjU1IiwidmlzaXRvcl9pZCI6IjgzNzU4ODY1ODI1MjAxODI2MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | fe433fd7469dcdb0a0abc2eb13e842f10d96142d652312795d464ef48951adc2 |
| hovercard-subject-tag | issue:5286419305 |
| 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/156570/issue_layout |
| twitter:image | https://opengraph.githubassets.com/107ab9977c4d4393c266ef33b7f47c73944e27420b65240ba75fcbb7ed392772/python/cpython/issues/156570 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/107ab9977c4d4393c266ef33b7f47c73944e27420b65240ba75fcbb7ed392772/python/cpython/issues/156570 |
| og:image:alt | Bug report test.support.warnings_helper.check_warnings() raises KeyError: 'warnings' under -X lazy_imports=all, taking down every test that uses it. Reproducer: ./python -X lazy_imports=all -m test... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ashm-dev |
| 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