Files
lancedb/python/python/tests/test_import.py
T
lancedb-gatefixer[bot] e5d4e9bf6e fix(python): normalize __version__ to PEP 440 (#4752)
Fixes #4749

Python's `__version__` forwards Cargo's SemVer spelling, so prereleases
report values such as `0.41.0-beta.17` while the installed package
metadata reports `0.41.0b17`.

Normalize the native version with the existing
`packaging.version.Version` dependency before exposing it in Python.
Keep native version sourcing so imports still work with `lancedb-compat`
and do not require distribution metadata. Regression tests cover
metadata equality and stable, alpha, beta, release-candidate, and
development versions with metadata lookups disabled.

Validation:

- Rebuilt the native extension through the uv-managed development
environment.
- Confirmed the metadata regression fails on the original code with the
reported string mismatch.
- Import tests, package metadata tests, the remote client user-agent
test, and CI Python dependency tests: 14 passed, 2 skipped (Windows-only
wheel check and uninstalled `lancedb-compat`).
- macOS ARM64/Python 3.13 dependency resolution and `uv lock --check`
pass with the platform-specific test dependency pins from current
`main`.
- Python-tree Ruff lint and formatting checks, focused Pyright check,
Rust formatting, and PR diff whitespace checks pass.
- Repository-wide Ruff lint reports existing findings in unchanged
`docs/web-tests` files.

<!-- lance-gatekeeper-fix:v1 agent=3e23555743cc116ea53ca8a7e3c4ebb1
generation=1 -->

Co-authored-by: Gatefixer <313497061+lancedb-gatefixer[bot]@users.noreply.github.com>
2026-10-31 20:41:22 -07:00

86 lines
2.3 KiB
Python

# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The LanceDB Authors
import os
import re
import shutil
import subprocess
import sys
import lancedb._lancedb as _lancedb
import pytest
@pytest.mark.skipif(sys.platform != "linux", reason="ldd is Linux-specific")
def test_native_extension_does_not_link_openssl():
"""OpenSSL-linked wheels abort when imported on RHEL hosts in FIPS mode."""
ldd = shutil.which("ldd")
if ldd is None:
pytest.skip("ldd is not installed")
result = subprocess.run(
[ldd, _lancedb.__file__],
check=True,
capture_output=True,
text=True,
)
openssl_libraries = re.findall(
r"^\s*(lib(?:crypto|ssl)\S*)\s+=>", result.stdout, flags=re.MULTILINE
)
assert not openssl_libraries, (
"the LanceDB native extension must use rustls instead of linking OpenSSL: "
f"{openssl_libraries}"
)
@pytest.mark.parametrize(
"native_version, expected_version",
[
("0.41.0", "0.41.0"),
("0.41.0-alpha.1", "0.41.0a1"),
("0.41.0-beta.17", "0.41.0b17"),
("0.41.0-rc.1", "0.41.0rc1"),
("0.41.0-dev.1", "0.41.0.dev1"),
],
)
def test_top_level_version_comes_from_native_module(native_version, expected_version):
child_code = """
import importlib
import importlib.metadata
import sys
from packaging.version import Version
orig_version = importlib.metadata.version
def fail_version_lookup(_: str) -> str:
if _ in {"lancedb", "lancedb-compat"}:
raise AssertionError("lancedb import should not query distribution metadata")
return orig_version(_)
importlib.metadata.version = fail_version_lookup
import lancedb
import lancedb._lancedb as native_module
import lancedb.db
assert lancedb.__version__ == str(Version(native_module.__version__))
native_module.__version__ = sys.argv[1]
importlib.reload(lancedb)
assert lancedb.__version__ == sys.argv[2]
"""
env = os.environ.copy()
env["PYTHONPATH"] = os.pathsep.join(path for path in sys.path if path)
result = subprocess.run(
[sys.executable, "-c", child_code, native_version, expected_version],
check=False,
capture_output=True,
text=True,
env=env,
)
assert result.returncode == 0, result.stderr or result.stdout