Add sglang sglang check env missing package safe - #7
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the environment check to catch importlib.metadata.PackageNotFoundError instead of ModuleNotFoundError when retrieving package versions, and adds a new unit test file to verify this behavior. The review feedback suggests two improvements to the test code: dynamically locating the source file using importlib.util.find_spec to avoid fragile relative paths, and defining the test class directly inside the test function for cleaner and more idiomatic code.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| resource_module.getrlimit = lambda _limit: (1024, 1024) | ||
| monkeypatch.setitem(sys.modules, "resource", resource_module) | ||
|
|
||
| module_path = Path(__file__).resolve().parents[3] / "python" / "sglang" / "check_env.py" |
There was a problem hiding this comment.
Using a hardcoded relative path (parents[3]) to locate the source file is fragile and can break if the test file is moved or if the tests are run against an installed package. It is more robust to locate the package dynamically using importlib.util.find_spec.
| module_path = Path(__file__).resolve().parents[3] / "python" / "sglang" / "check_env.py" | |
| sglang_spec = importlib.util.find_spec("sglang") | |
| if sglang_spec and sglang_spec.submodule_search_locations: | |
| module_path = Path(sglang_spec.submodule_search_locations[0]) / "check_env.py" | |
| else: | |
| module_path = Path(__file__).resolve().parents[3] / "python" / "sglang" / "check_env.py" |
| class _UnitEnv: | ||
| def get_info(self): | ||
| return {} | ||
|
|
||
| def get_topology(self): | ||
| return {} | ||
|
|
||
|
|
||
| def test_get_package_versions_handles_missing_packages(monkeypatch): | ||
| check_env = _load_check_env(monkeypatch) | ||
| env = type("UnitEnv", (_UnitEnv, check_env.BaseEnv), {})() | ||
| env.package_list = ["sglang", "missing-package-for-unit-test"] |
There was a problem hiding this comment.
Instead of creating a global dummy class _UnitEnv and dynamically constructing the test class using type(), it is much cleaner and more idiomatic to define the test class directly inside the test function. This keeps the test self-contained and improves readability.
| class _UnitEnv: | |
| def get_info(self): | |
| return {} | |
| def get_topology(self): | |
| return {} | |
| def test_get_package_versions_handles_missing_packages(monkeypatch): | |
| check_env = _load_check_env(monkeypatch) | |
| env = type("UnitEnv", (_UnitEnv, check_env.BaseEnv), {})() | |
| env.package_list = ["sglang", "missing-package-for-unit-test"] | |
| def test_get_package_versions_handles_missing_packages(monkeypatch): | |
| check_env = _load_check_env(monkeypatch) | |
| class UnitEnv(check_env.BaseEnv): | |
| def get_info(self): | |
| return {} | |
| def get_topology(self): | |
| return {} | |
| env = UnitEnv() | |
| env.package_list = ["sglang", "missing-package-for-unit-test"] |
- Handle missing packages in check_env - Make check_env missing package test robust
Summary
Validation
Review notes