Skip to content

Use sys.version_info in _python_version - #2875

Open
SammyTourani wants to merge 1 commit into
apple:mainfrom
SammyTourani:fix/issue-2874
Open

SammyTourani wants to merge 1 commit into
apple:mainfrom
SammyTourani:fix/issue-2874

Conversation

@SammyTourani

Copy link
Copy Markdown

Fixes #2874

  • coremltools/models/utils.py: _python_version() now returns tuple(sys.version_info[:3]). sys.version_info gives major, minor and micro as ints on every build, so there is nothing to parse. On final releases the return value is unchanged. The existing callers only compare major and minor (_python_version() < (3, 6), _python_version()[:2] <= (3, 8)).
  • coremltools/test/ml_program/test_utils.py: new TestPythonVersion test. It sets sys.version and sys.version_info to a final release, a release candidate and a development build, then checks the returned tuple. Without the fix, the release-candidate and development-build cases fail with the ValueError from the report.

coremltools/test/optimize/torch/utils.py has an identical copy of _python_version, but nothing imports it. I left it unchanged so this change stays on the reported code path.

Verification

Tested on macOS 26 (arm64) with Python 3.13.15, torch 2.8.0, numpy 2.1.0 and pytest 9.1.1. The native extensions came from the 9.0 wheel; their sources on main are unchanged since 9.0. Each command was run from the repo root, first with the parent commit's utils.py and then with the fix.

Command: python -m pytest coremltools/test/ml_program/test_utils.py::TestPythonVersion
Result: before: 2 failed, 1 passed in 1.36s (ValueError: invalid literal for int() with base 10: '0rc2' and '0a1+'). After: 3 passed in 0.93s.

Command: python -m pytest coremltools/test/ml_program/test_utils.py
Result: before: 2 failed, 34 passed, 7 skipped, 8 xfailed, 142 warnings in 13.57s. After: 36 passed, 7 skipped, 8 xfailed, 142 warnings in 13.63s. The skips and xfails come from the file's existing markers (rdar://157488825, rdar://138957606).

Command: python -m pytest coremltools/converters/mil/frontend/torch/test/test_torch_ops.py::TestSlice coremltools/converters/mil/frontend/torch/test/test_torch_conversion_api.py::TestTorchInputs::test_int8_tenors coremltools/test/ml_program/experimental/test_perf_utils.py::TestTorchMLModelBenchmarker
Result: 178 passed, 6 xfailed, 4 xpassed, 199 warnings both before and after. These are the existing tests that call _python_version. All 188 ran and none were skipped. The xfail/xpass results come from existing markers (#2189, rdar://155895085).

Also checked:

  • On a real pre-release interpreter (CPython 3.15.0b3), from coremltools.models.utils import _python_version; _python_version() raised ValueError: invalid literal for int() with base 10: '0b3' before the fix and returns (3, 15, 0) after.
  • With sys.version set to the report's 3.15.0rc2 string, importlib.import_module("coremltools.converters.mil.frontend.milproto.test_load") reproduces the traceback from the issue before the fix (TestSlice → _python_version → ValueError: ... '0rc2') and imports cleanly after.
  • On final-release CPython 3.9.6, 3.11.15, 3.12.12, 3.13.15 and 3.14.6, the old and new function bodies return identical tuples.
_python_version parsed sys.version and converted each dotted component
to int, which raises ValueError on pre-release and development builds
such as 3.15.0rc2 or 3.15.0a1+. Read the numbers from sys.version_info
instead.

Fixes apple#2874

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant