Gate the torchcodec audio extras to platforms that have a wheel (#7587)

This commit is contained in:
Daniel Han 2026-07-28 20:56:11 -07:00 committed by GitHub
commit fa95054399
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 55 additions and 3 deletions

View file

@ -11,12 +11,21 @@ import sys
import types
from pathlib import Path
import pytest
REPO_ROOT = Path(__file__).resolve().parents[2]
PYPROJECT = REPO_ROOT / "pyproject.toml"
IMPORT_FIXES_PATH = REPO_ROOT / "unsloth" / "import_fixes.py"
def _tomllib():
if sys.version_info >= (3, 11):
import tomllib
return tomllib
return pytest.importorskip("tomli")
def _load_import_fixes_module():
spec = importlib.util.spec_from_file_location(
"unsloth_import_fixes_under_test",
@ -127,3 +136,41 @@ def test_import_fixes_loads_on_python39_syntax():
"""Regression: module must import on 3.9 (postponed annotations for str | None)."""
fixes = _load_import_fixes_module()
assert callable(fixes._torchcodec_version_mismatch_hint)
def test_audio_extras_are_gated_to_platforms_with_a_torchcodec_wheel():
"""torchcodec publishes no sdist and no wheel for Linux aarch64, Windows ARM64 or
Intel Mac, so an ungated pin makes pip fail the whole install on those hosts instead
of just skipping audio -- and the cu*/rocm*/intel torch 2.10 extras pull it in.
The marker must match PLATFORM_LACKS_TORCHCODEC_WHEEL in install_python_stack.py.
"""
markers = pytest.importorskip("packaging.markers")
tomllib = _tomllib()
extras = tomllib.loads(PYPROJECT.read_text(encoding = "utf-8"))["project"][
"optional-dependencies"
]
audio = {n: d for n, d in extras.items() if n.startswith("audio-torch")}
assert audio, "expected audio-torch* extras"
supported = [
{"sys_platform": "linux", "platform_machine": "x86_64"},
{"sys_platform": "win32", "platform_machine": "AMD64"},
{"sys_platform": "darwin", "platform_machine": "arm64"},
]
unsupported = [
{"sys_platform": "linux", "platform_machine": "aarch64"},
{"sys_platform": "win32", "platform_machine": "ARM64"},
{"sys_platform": "darwin", "platform_machine": "x86_64"},
]
for name, deps in audio.items():
for dep in deps:
_, _, marker_text = dep.partition(";")
assert marker_text.strip(), f"{name}: {dep!r} has no marker"
marker = markers.Marker(marker_text.strip())
env = {"python_version": "3.12"}
for case in supported:
assert marker.evaluate({**env, **case}), f"{name} must install on {case}"
for case in unsupported:
assert not marker.evaluate(
{**env, **case}
), f"{name} has no wheel for {case} and must not be resolved there"