Skip to content

Studio tests: compare the hub source restore against the inherited source - #16236

Merged
danielhanchen merged 1 commit into
mainfrom
dh/fix-hub-source-test-inherited-env
Nov 1, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
dh/fix-hub-source-test-inherited-env

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

tests/test_hub_source_env_reset_between_tests.py checks that conftest restores the hub source env between tests. Its second test asserts the source is Hugging Face, not the source the process started with. With UNSLOTH_STUDIO_HUB_SOURCE=modelscope set in the shell (a developer or CI host that uses the ModelScope mirror), conftest correctly restores modelscope after the first test, and the second test then fails:

UNSLOTH_STUDIO_HUB_SOURCE=modelscope python -m pytest -o addopts= tests/test_hub_source_env_reset_between_tests.py
1 failed, 1 passed

Change

Test only. The module reads the inherited source at collection. The first test switches to the other source (ModelScope when Hugging Face was inherited, Hugging Face when ModelScope was), written into os.environ the way the settings code writes it. The second test asserts the env is back to the inherited value.

Tests

  • Old test file: 1 failed, 1 passed with UNSLOTH_STUDIO_HUB_SOURCE=modelscope; 2 passed when unset.
  • New test file: 2 passed with the source unset, empty, modelscope and huggingface.
  • It still catches a missing restore: with the conftest restore fixture disabled, the new file gives 1 failed, 1 passed for modelscope, huggingface and unset.
  • Hub area (test_hub_s*.py, test_*modelscope*.py): 139 passed, 2 failed both with and without the ModelScope env. The 2 failures (test_video_untrusted_repo_safetensors_single_file_is_accepted, test_cache_gate_asks_hugging_face_while_modelscope_serves) come from av and uvicorn missing in the local test env and are unrelated to this change.

Not covered

Only run serially. Under --dist loadgroup the two tests share the existing xdist group and are unchanged in that respect.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 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-11-01T13:30:55.782727Z 569146d Manual request
ℹ️ 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

(Python 3.13, g-r) fails on this PR's own new test, in run 41144856522:

tests/test_hub_source_env_reset_between_tests.py:39: in test_the_next_test_starts_on_the_source_it_inherited
    assert os.environ.get(hub_settings.SOURCE_ENV) == _INHERITED
E   AssertionError: assert 'huggingface' == None

The leak this test targets did not happen here. Test 1 sets _OTHER, which is modelscope when nothing is inherited, and the value seen in test 2 was huggingface. The cause is that _INHERITED is read when this module is collected. Collection continues afterwards, and importing main runs _apply_hub_settings() (studio/backend/main.py:310), which writes UNSLOTH_STUDIO_HUB_SOURCE=huggingface into the process. So by the time the tests run, the inherited value is huggingface, not None. _restore_hub_settings_env in conftest is restoring correctly; it snapshots at the start of each test.

Suggested fix: record the inherited value inside test 1, before it switches source (for example in a module-level dict that test 2 reads), instead of at import. Alternatively, have test 2 assert active_source() != _OTHER and os.environ.get(SOURCE_ENV) != _OTHER only. Either keeps the regression check without depending on what other modules import during collection.

@danielhanchen danielhanchen self-assigned this Nov 1, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 1, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 569146dca2

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 3df6e15 into main Nov 1, 2026
@danielhanchen
danielhanchen deleted the dh/fix-hub-source-test-inherited-env branch November 1, 2026 13:37
danielhanchen added a commit that referenced this pull request Nov 1, 2026
…, not at collection (#16430)

## Summary

`(Python 3.13, g-r)` fails on several unrelated PRs (#16413, #16414,
#16415):

```
test_hub_source_env_reset_between_tests.py::test_the_next_test_starts_on_the_source_it_inherited
E   AssertionError: assert 'huggingface' == None
E    +  where 'huggingface' = get('UNSLOTH_STUDIO_HUB_SOURCE')
```

The test reads `_INHERITED = os.environ.get(SOURCE_ENV)` at module
import, during collection. Collection goes on after that. Any module
collected later that imports the app (for example
`tests/test_liveness_reports_*.py` with a module-level `from main import
...`) runs `apply_hub_settings()`, and that always writes
`UNSLOTH_STUDIO_HUB_SOURCE`. The conftest fixture
`_restore_hub_settings_env` then correctly restores the value each test
started with (`huggingface`), but the test compares it against the stale
collection-time value (`None`). The fixture is right. The test's
baseline is taken too early, and whether it fails depends on which files
share the shard. #16236 made it compare against the inherited source,
which made it sensitive to this.

## Change

The first test records the source env and the "other" source at the
moment it starts. That is exactly what the fixture saves and restores.
The second test compares against those, and fails with a clear message
if it runs without the first one. Nothing else changes.

## Testing

- **Repro of the CI failure:** run this file plus a later-collected
stand-in module that sets `UNSLOTH_STUDIO_HUB_SOURCE=huggingface` at
import, the way the app import does.
  - main: fails with the same `assert 'huggingface' == None`;
  - this branch: 3 passed.
- This file alone: 2 passed. With `UNSLOTH_STUDIO_HUB_SOURCE=modelscope`
in the environment (the case #16236 was for): 2 passed.
- Sabotage: with the conftest restore loop disabled, the second test
fails again. The test still catches a missing restore.
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