Skip to content

Keep the loggers stub a package so tests/studio can be collected in one process - #10531

Merged
danielhanchen merged 2 commits into
mainfrom
fix/loggers-stub-shadows-package
Sep 8, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
fix/loggers-stub-shadows-package

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

tests/studio cannot be collected in a single pytest process on main:

$ python3 -m pytest tests/studio --collect-only -q
ERROR tests/studio/test_mlx_context_platform_matrix.py
E   ModuleNotFoundError: No module named 'loggers.media_progress'; 'loggers' is not a package
8331 tests collected, 1 error

That file passes its own 60 tests when run alone, so the failure is entirely about what else is in the session.

Cause

tests/studio/load_freeze/test_load_orchestrator.py installs a stub at import time:

_loggers_stub = types.ModuleType("loggers")
_loggers_stub.get_logger = lambda name: _logging.getLogger(name)
sys.modules.setdefault("loggers", _loggers_stub)

pytest imports every test module during collection, and that sys.modules entry outlives the module that installed it. A stub built by ModuleType has no __path__, so from that point on every loggers.<submodule> import in the session fails, and it fails as 'loggers' is not a package -- which reads like a missing dependency rather than one test file shadowing a package.

Not a recent regression: this arrived with #5669. Worth noting that the structlog block immediately below it already guards against this exact hazard, in a comment that says so:

# structlog is a hard studio.txt requirement imported only lazily, so a bare setdefault would shadow the real package.

The care was applied to structlog and not to loggers.

Fix

The stub itself is reasonable -- it exists so this file need not import structlog and the rest of the logging chain for the one get_logger it uses. It just has to remain a package:

_loggers_stub.__path__ = [str(_STUDIO_BACKEND / "loggers")]

_STUDIO_BACKEND is already on sys.path two lines above, so submodule imports resolve to the real files while get_logger stays the cheap stub.

Guard

tests/studio/test_loggers_package_not_shadowed.py asserts the invariant at run time, which is the point: collection has finished by then, so every module-level stub in the session is already installed. It checks both that the entry is a package and that a real submodule imports through it.

Verified by mutation: drop the __path__ line and both tests fail; restore it and they pass.

Scope

79 other test files use the same stub pattern without __path__. Left alone deliberately -- they sit in sessions where nothing imports a real loggers submodule, studio/backend/tests and tests/python both collect clean, and rewriting them would be churn against no defect. If one of those sessions later grows a test that needs the real package, this guard is what will say so.

Verification

  • tests/studio collection: 8331 + 1 error -> 8407 collected, no errors.
  • tests/studio tests/python studio/backend/tests/test_torchao_select.py as one process: 10595 passed, 333 skipped, 0 failed.
  • tests/studio/load_freeze on its own: 23 passed, unchanged.
  • ruff check tests: clean.

…ne process

tests/studio/load_freeze/test_load_orchestrator.py installs a ModuleType("loggers") stub into
sys.modules at import time. That entry outlives the module, and pytest imports every test
module during collection, so any later test reaching a real submodule died at COLLECTION:

    ModuleNotFoundError: No module named 'loggers.media_progress'; 'loggers' is not a package

which reads like a missing dependency rather than one file shadowing a package for the whole
session. It took tests/studio/test_mlx_context_platform_matrix.py with it, so the directory
could not be collected in one pytest process at all: 8331 collected + 1 error, and the file
passes its own 60 tests when run alone.

The stub itself is reasonable -- it avoids pulling structlog and the rest of the chain in for a
test that only needs get_logger. It just has to stay a PACKAGE, so __path__ now points at the
real studio/backend/loggers, which the sys.path insert two lines above already reaches.
Submodule imports resolve to the real files and get_logger stays the cheap stub.

This is the same hazard the structlog block immediately below already guards against, in a
comment that spells it out: "a bare setdefault would shadow the real package". The care was
applied to structlog and not to loggers. From #5669; not a recent regression.

tests/studio/test_loggers_package_not_shadowed.py asserts the invariant at RUN time, after
collection has installed every module-level stub in the session. Verified by mutation: drop
the __path__ line and both its tests fail; restore it and they pass.

79 other test files use the same stub pattern without __path__. They are left alone: they sit
in sessions where nothing imports a real loggers submodule, studio/backend/tests and
tests/python both collect clean, and rewriting them would be churn for no defect.

tests/studio + tests/python now run as one process: 10595 passed, 333 skipped.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 8, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-08T16:41:33.441741Z 917a578 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@danielhanchen
danielhanchen merged commit 62da4f0 into main Sep 8, 2026
70 of 73 checks passed
@danielhanchen
danielhanchen deleted the fix/loggers-stub-shadows-package branch September 8, 2026 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant