Repository navigation
fix(test): stop leaving stub modules registered in sys.modules - #40129
Conversation
Five test files put a stand-in module into sys.modules when the real one was not yet imported, and never took it out. Three of them registered a two-function stub as kiro_crew.slack.handler. An autouse fixture imports _PHASE_EMOJIS from that module before every test, so with the stub in place every test of the file fails in setup with ImportError. test_send_message_per_part.py imports one of the three and fails the same way. Main is green because an xdist worker has imported the real handler while collecting an earlier file, so the stub is never installed; each of the four files run alone fails. Two more registered a MagicMock as kiro_crew.dashboard.server. Their own tests pass, and every later test in the process that imports the dashboard gets the mock. The stubs are removed. The real modules import in this suite: the autouse fixture already needs the handler for every test, and both dashboard files pass without the mock. Related: #39702
First Principles Review (Opus 5.5) — ⏭️ skippedRevision |
UX Review (Opus 5.5) — ⏭️ skippedRevision |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing blocks: this is a test-only change that deletes stub modules nothing ever removed, so the tests now import the real modules and no longer depend on which files ran first in the process. Notes
[DESIGN-REVIEWED] 73dff1f Description read[DESCRIPTION-READ] 133acf7ea4ed52076f7a3336e8284807413050d79313d132a3ef351d653b1bbe That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
Opus 5.5 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
GPT 6.1 Review — ✅ no blocking findingsGPT 6.1 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
Code change approver recommendation: ApproveWhy: the decider estimates a 5% chance that this change introduces a defect, within the 12% limit for approving on its own, and every check passes. Test-only change. It deletes stand-in modules that five test files registered in sys.modules and never removed: three for kiro_crew.slack.handler, two MagicMocks for kiro_crew.dashboard.server. Every test that depends on is_tracked_channel or is_allowed_user already patches the real kiro_crew.slack.handler attribute, and the product code imports those functions at call time, so the patches take effect. With no patch, the real functions return False for test inputs (empty tracked-channel set, owner-only check), which matches the stand-ins. The edited files have no leftover uses of sys or types, and the local imports that remain are still used. No tests or assertions are removed, and every CI check is green, including Windows, the backend shards and new-test stability. Tier: 1, every tier gate passed. An automated recommendation, not a review: a maintainer still decides. It approves on its own only when the estimated chance of a defect is at most 12% and the change has at most 400 changed lines. Decider v3c (217fe607) · commit |
KiroCrewCaptain
left a comment
There was a problem hiding this comment.
Approved automatically by the code change approver, not by a person. The decider estimates a 5% chance that this change introduces a defect, within the 12% limit for approving on its own, and the change has at most 400 changed lines.
Test-only change. It deletes stand-in modules that five test files registered in sys.modules and never removed: three for kiro_crew.slack.handler, two MagicMocks for kiro_crew.dashboard.server. Every test that depends on is_tracked_channel or is_allowed_user already patches the real kiro_crew.slack.handler attribute, and the product code imports those functions at call time, so the patches take effect. With no patch, the real functions return False for test inputs (empty tracked-channel set, owner-only check), which matches the stand-ins. The edited files have no leftover uses of sys or types, and the local imports that remain are still used. No tests or assertions are removed, and every CI check is green, including Windows, the backend shards and new-test stability.
Tier: 1, every tier gate passed.
Criteria: no merge conflict, no owner block, no change request, the default branch, no security-sensitive path, no design document to agree on, the readiness status, the review lanes, the P0 history, the linked issues' tier, the review lessons, no concern label, the file count, a safe change category, not its own change, the decider, the security checklist, the change matches its title, no competing change.
Decider v3c (217fe607). To overrule it, request changes or dismiss this review.
Problem / Motivation
Goal: Five test files no longer leave a stand-in module registered in
sys.modules, so each passes when run by itself and none breaks the tests that run after it in the same process.Four files fail on main when run alone, every test erroring in setup:
ImportError: cannot import name '_PHASE_EMOJIS' from 'kiro_crew.slack.handler' (unknown location)test_cron_origin_injection.py,test_send_message_targeted.pyandtest_send_message_session_link.pyeach begin withif "kiro_crew.slack.handler" not in sys.modules:and then register a two-functiontypes.ModuleTypeunder that name, never removing it. The autouse fixture_clean_emojis(test/conftest_slack.py) imports_PHASE_EMOJISfromkiro_crew.slack.handlerbefore every test, finds the stub, and fails.test_send_message_per_part.pyimports its helpers fromtest_send_message_targeted.pyand fails the same way.Two more files,
test_enable_deps_resolution.pyandtest_lifecycle_hooks.py, register aMagicMockaskiro_crew.dashboard.serverthe same way. Their own tests pass; every later test in the process that imports the dashboard gets the mock.Why it matters
Main is green only by order: an xdist worker has imported the real modules while collecting earlier files, so the
not in sys.modulesbranch never runs. Anyone running one of these files alone gets a wall of errors that has nothing to do with their change, and any regrouping of files into processes (a leaf-scope run, a smaller shard) can turn it red in CI. It is the same class as #40124: a test's stand-in outliving the test.Not a goal
kiro_crew.slack.handler, the dashboard, or the_clean_emojisfixture: the product code and the fixture are correct; the stubs are the defect.sys.modules.test_pip_deps_consistency.pyandtest_publish_personal_drive_registration.py, which also assign intosys.modules: the second restores the original, and neither showed a failure in the check below.What changed (motivation → approach → change)
Symptom:
ImportErrorin setup when a file runs alone; a mock dashboard for later tests. Cause: a stub registered under a real module's name at import or inside a test, with no restore. The stubs' stated reason was "importable even when heavy transitive deps aren't installed" and "avoid circular import"; neither holds in this suite: the autouse fixture already imports the real handler for every test, and both dashboard files pass with the real module. Change: the five stub registrations are deleted, with the imports they needed (sys,types) and the# noqa: E402marks that only existed because code sat above the imports. One test that looked the stub up insys.modulesto patchis_tracked_channelnow patches the real module, inside the samewith.Backwards compatibility
Compatible: test files only; nothing that runs in the product changes.
Removes nothing: five stub registrations are removed from test modules; no test, assertion or fixture is removed.
Tests
Proof: not provable -- test-only change
Each command is
python -m pytest -n0 --no-cov -q -p no:cacheprovider <file>; the last two rows addtest/test_power.py::test_should_prevent_sleep_no_sessions, which imports the dashboard.test/test_cron_origin_injection.pytest/test_send_message_per_part.pytest/test_send_message_session_link.pytest/test_send_message_targeted.pytest/test_enable_deps_resolution.py+ the dashboard testtest/test_lifecycle_hooks.py+ the dashboard testAll six files together with
test/test_flake_pattern_ratchet.pyunder-n 4: 275 passed. Black, isort, flake8, comment-history, brand and file-line-cap gates pass.test/test_lifecycle_hooks.pyis on the black baseline and is edited without reformatting.Manual verification
N/A — the table above is the whole behaviour, and each row is a pytest command.
Real-machine verification
Machine: own development host (EC2, AMD EPYC 9R14), Amazon Linux 2023, x86_64, 32 vCPU / 123 GiB
Before (main d465efc):
python -m pytest -n0 --no-cov -q -p no:cacheprovider test/test_send_message_targeted.py-> 87 errors,ImportError: cannot import name '_PHASE_EMOJIS' from 'kiro_crew.slack.handler' (unknown location)After (head 73dff1f): the same command -> 87 passed
Cleanup: own computer
Related Issues
Related: #39702
Sibling sweep
Swept:
grep -rnE 'sys\.modules\["kiro_crew\.[a-z_.]*"\] = |sys\.modules\.setdefault\("kiro_crew' test/*.py(assignments not made throughmonkeypatch), then each hit's file run alone and followed by the dashboard testSibling: test/test_cron_origin_injection.py -- changed here
Sibling: test/test_send_message_targeted.py -- changed here
Sibling: test/test_send_message_session_link.py -- changed here
Sibling: test/test_enable_deps_resolution.py -- changed here
Sibling: test/test_lifecycle_hooks.py -- changed here
Sibling: test/test_send_message_per_part.py -- not affected -- registers nothing itself; it failed through its import of test_send_message_targeted.py and passes now
Sibling: test/test_publish_personal_drive_registration.py -- not affected -- the assignment at line 76 puts the original module back; with the dashboard test after it: 19 passed
Sibling: test/test_pip_deps_consistency.py -- not affected -- run with the dashboard test after it: 11 passed
The search covers
test/*.pyonly, not the app suites undersrc/kiro_crew/apps/builtins.Pattern harvest
Rule candidate: semgrep
Pattern: a test module assigns into
sys.modulesunder a realkiro_crew.*name outsidemonkeypatch.setitem, so the stand-in is never removedChecklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commitsContribution License Agreement
Contributed under the project's Apache-2.0 license.
AI-assisted, opened by Claude Code