Repository navigation
Reject an hf_xet that is only an empty namespace package - #11205
Conversation
Follow-up to #11167. An hf_xet/ directory with no __init__.py and no extension in it, beside an intact hf_xet-*.dist-info, is a NAMESPACE package. The suspicion was raised correctly (the extension really is missing), but the confirm step imported the package, that import succeeded because a namespace package always does, and the fix cleared itself and did nothing. huggingface_hub does not import the package, it imports names out of it (from hf_xet import PyXetDownloadInfo, download_files), so it still routed every download to Xet. Reproduced against huggingface_hub 0.36.2 with the package contents deleted and the directory left behind: find_spec namespace extension_is_missing True bare import succeeds routes_to_xet True user_error ImportError: cannot import name 'PyXetDownloadInfo' from 'hf_xet' (unknown location) which is the exact misleading failure the original change exists to prevent. A namespace package has __file__ None and carries no code, so requiring a real __file__ is enough to tell it from an installed one, without pinning a symbol list that moves between huggingface_hub versions. Same scenario after: routes_to_xet False, and the shell is dropped from sys.modules rather than left for the next importer. The healthy, wrong-tag, already-imported and absent scenarios were re-run against a real huggingface_hub and are unchanged. The stand-in for a successful import in the test helper returned a bare object(), which has no __file__; it now returns a module, which is what a real import gives.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9e050bfe66
ℹ️ 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".
| module = importlib.import_module("hf_xet") | ||
| if getattr(module, "__file__", None) is not None: | ||
| return |
There was a problem hiding this comment.
Validate a cached namespace package before returning
When the broken namespace package is already cached in sys.modules, this new validation is never reached because the earlier if "hf_xet" in sys.modules: return still assumes every cached module is functional. This occurs naturally if a Hugging Face download previously executed from hf_xet import ...: the missing-symbol import fails but leaves the successfully imported namespace shell cached. Importing Unsloth afterward therefore leaves Xet enabled, and subsequent downloads continue to fail. Apply the __file__ check to the cached module before short-circuiting as well.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Genuine, and the mechanism is exactly as you describe. Reproduced before fixing, with the Hub used before unsloth on huggingface_hub 0.36.2:
hub attempt ImportError: cannot import name 'PyXetDownloadInfo' from 'hf_xet' (unknown location)
shell_left_in_sys_modules True
shell __file__ None
env (unset)
routes_to_xet True
from hf_xet import ... leaves the package cached because the package import itself succeeded and only the attribute lookup failed, so the presence of a cached module proved nothing. Fixed in 4f06ced: the cached object now gets the same test as a freshly imported one, and the shell is evicted rather than trusted. Same scenario after: env 1, routes_to_xet False.
Both call sites share one _hf_xet_module_is_real helper so they cannot drift apart.
Worth recording that this also exposed a bad stand-in in my own test: test_an_imported_hf_xet_short_circuits_before_any_lookup used a bare types.ModuleType, which has no __file__ and so was modelling the very shell this rejects. It failed when the check went in, correctly, and now models a real module. The healthy, wrong-tag, already-imported and absent scenarios were re-run against a real huggingface_hub and are unchanged; 79 tests pass, and the new one fails with the short-circuit restored.
huggingface_hub's own `from hf_xet import PyXetDownloadInfo, download_files`
leaves hf_xet in sys.modules even when those symbols are missing: the PACKAGE
imported fine and only the attribute lookup failed. So for a user whose first
download happens before `import unsloth` (a notebook cell ordering, or any
script that reaches the Hub first), the shell was already cached and the
`if "hf_xet" in sys.modules: return` short circuit skipped every check.
Reproduced against huggingface_hub 0.36.2, Hub used first:
hub attempt ImportError: cannot import name 'PyXetDownloadInfo'
from 'hf_xet' (unknown location)
cached True, __file__ None
env (unset)
routes_to_xet True
A cached module is not proof of a working one, so the cached object gets the
same test as a freshly imported one and the shell is evicted rather than
trusted. Same scenario after: env 1, routes_to_xet False.
The test that pins the short circuit used a bare types.ModuleType as its stand
in for a working hf_xet, which has no __file__ and is therefore exactly the
shell this rejects; it now models a real module. The healthy, wrong-tag,
already-imported and absent scenarios were re-run against a real
huggingface_hub and are unchanged.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…1205) * Reject an hf_xet that is only an empty namespace package Follow-up to unslothai#11167. An hf_xet/ directory with no __init__.py and no extension in it, beside an intact hf_xet-*.dist-info, is a NAMESPACE package. The suspicion was raised correctly (the extension really is missing), but the confirm step imported the package, that import succeeded because a namespace package always does, and the fix cleared itself and did nothing. huggingface_hub does not import the package, it imports names out of it (from hf_xet import PyXetDownloadInfo, download_files), so it still routed every download to Xet. Reproduced against huggingface_hub 0.36.2 with the package contents deleted and the directory left behind: find_spec namespace extension_is_missing True bare import succeeds routes_to_xet True user_error ImportError: cannot import name 'PyXetDownloadInfo' from 'hf_xet' (unknown location) which is the exact misleading failure the original change exists to prevent. A namespace package has __file__ None and carries no code, so requiring a real __file__ is enough to tell it from an installed one, without pinning a symbol list that moves between huggingface_hub versions. Same scenario after: routes_to_xet False, and the shell is dropped from sys.modules rather than left for the next importer. The healthy, wrong-tag, already-imported and absent scenarios were re-run against a real huggingface_hub and are unchanged. The stand-in for a successful import in the test helper returned a bare object(), which has no __file__; it now returns a module, which is what a real import gives. * Check a cached hf_xet too, not just a freshly imported one huggingface_hub's own `from hf_xet import PyXetDownloadInfo, download_files` leaves hf_xet in sys.modules even when those symbols are missing: the PACKAGE imported fine and only the attribute lookup failed. So for a user whose first download happens before `import unsloth` (a notebook cell ordering, or any script that reaches the Hub first), the shell was already cached and the `if "hf_xet" in sys.modules: return` short circuit skipped every check. Reproduced against huggingface_hub 0.36.2, Hub used first: hub attempt ImportError: cannot import name 'PyXetDownloadInfo' from 'hf_xet' (unknown location) cached True, __file__ None env (unset) routes_to_xet True A cached module is not proof of a working one, so the cached object gets the same test as a freshly imported one and the shell is evicted rather than trusted. Same scenario after: env 1, routes_to_xet False. The test that pins the short circuit used a bare types.ModuleType as its stand in for a working hf_xet, which has no __file__ and is therefore exactly the shell this rejects; it now models a real module. The healthy, wrong-tag, already-imported and absent scenarios were re-run against a real huggingface_hub and are unchanged.
Follow-up to #11167, from a Codex item on that PR that arrived in the same minute it merged, so it never got triaged. It is a real gap and it is on main now.
The hole
An
hf_xet/directory with no__init__.pyand no compiled extension in it, sitting beside an intacthf_xet-*.dist-info, is a namespace package. That is a reachable shape: an interruptedpip uninstall, an antivirus quarantine of the.pyd, a partially synced network or OneDrive folder.fix_broken_hf_xet_wheelgot the suspicion right (the extension really is missing) and then threw it away. The confirm step imported the package, a namespace package always imports, so the guard cleared itself and did nothing.huggingface_hub never imports the package. It imports names out of it:
so
is_xet_available()stayed True and every download still went to Xet.Reproduced
Against huggingface_hub 0.36.2 in a throwaway venv, package contents deleted and the directory left behind:
which is precisely the misleading failure #11167 exists to prevent.
The fix
A namespace package has
__file__None and carries no code, so requiring a real__file__separates it from an installed package without pinning a symbol list that moves between huggingface_hub versions. The shell is also dropped fromsys.modulesrather than left for the next importer.Same scenario after:
Nothing else moved
The no-fire scenarios were re-run against a real huggingface_hub, not mocks:
78 tests pass.
test_fires_when_hf_xet_is_only_an_empty_namespace_packagefails with the change reverted.One test-helper correction rides along: the stand-in for a successful import returned a bare
object(), which has no__file__, so it modelled the very shell the confirm step now rejects. It returns a module now, which is what a real import gives.What I could not prove
is_xet_available() and not constants.HF_HUB_DISABLE_XET, plus its realfrom hf_xet import ....