Skip to content

Commit 0fbaffe

Browse files
author
Gereon Elvers
committed
Fix CI: scope sys.modules pops in overlay tests under monkeypatch.delitem
test_overlay_modules_are_discovered_without_registration and test_overlay_modules_can_override_public_modules used plain sys.modules.pop to clear pnpl entries before re-importing them through a tmp_path overlay. That tmp_path overlay only contains a few overlay files (no top-level pnpl/__init__.py), so re-importing pnpl resolved it as a namespace package — and because the pop was permanent, that namespace-loaded pnpl lingered in sys.modules after the test and poisoned every later test that re-imported pnpl, pnpl.datasets, or pnpl.tasks. Locally, pytest happens to add the project root to sys.path which lets PathFinder shortcut to the real pnpl/__init__.py before the namespace fallback kicks in, masking the leak. CI did not get that shortcut, so the four namespace-related tests have been failing since refactor was merged into main. Switching the pops to monkeypatch.delitem(sys.modules, ..., raising=False) makes pytest restore the original modules at teardown, so the leak is gone. Verified: pytest passes 13/13 from a non-project cwd, which previously reproduced the four CI failures.
1 parent 5b5fb3f commit 0fbaffe

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

‎tests/test_namespace_paths.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,13 @@ def test_overlay_modules_are_discovered_without_registration(tmp_path, monkeypat
5858

5959
monkeypatch.syspath_prepend(str(tmp_path))
6060

61+
# Pop via ``monkeypatch.delitem`` so teardown restores the original
62+
# modules. Plain ``sys.modules.pop`` is permanent — a tmp_path-loaded
63+
# ``pnpl`` (which lacks ``__init__.py`` at the overlay root and so
64+
# resolves as a namespace package) would linger in ``sys.modules`` and
65+
# poison every later test that re-imports pnpl.
6166
for modname in ["pnpl.tasks.private_overlay", "pnpl.tasks", "pnpl"]:
62-
sys.modules.pop(modname, None)
67+
monkeypatch.delitem(sys.modules, modname, raising=False)
6368

6469
module = importlib.import_module("pnpl.tasks.private_overlay")
6570
assert module.VALUE == "overlay"
@@ -76,13 +81,16 @@ def test_overlay_modules_can_override_public_modules(tmp_path, monkeypatch):
7681

7782
monkeypatch.syspath_prepend(str(tmp_path))
7883

84+
# See note in the previous test — ``monkeypatch.delitem`` lets
85+
# teardown restore the regular ``pnpl`` modules. Without it, the
86+
# namespace-loaded ``pnpl`` from this test bleeds into later tests.
7987
for modname in [
8088
"pnpl.datasets.libribrain2025.remote_constants",
8189
"pnpl.datasets.libribrain2025",
8290
"pnpl.datasets",
8391
"pnpl",
8492
]:
85-
sys.modules.pop(modname, None)
93+
monkeypatch.delitem(sys.modules, modname, raising=False)
8694

8795
libribrain = importlib.import_module("pnpl.datasets.libribrain2025")
8896
assert str(overlay_root) == list(libribrain.__path__)[0]

0 commit comments

Comments
 (0)