Files
lancedb/python/python/tests/test_import.py
T
Ozawa SongandXuanwo 4b373b2070 fix(python)!: support compat-only imports (#3973)
Read `__version__` from the native `_lancedb` module so `import lancedb`
works when only `lancedb-compat` is installed. Add the type declaration
and a subprocess regression test.

Ruff and Linux-targeted Pyright pass. Import tests: 1 passed and 1
Linux-only test skipped on Windows. Runtime tests used the official
same-version Windows native module; a fresh source build still needs CI.

BREAKING CHANGE: `lancedb.__version__` now uses the native version
string.
Prereleases may report `0.41.0-beta.13` instead of `0.41.0b13`.

Fixes #3950

---------

Co-authored-by: Xuanwo <github@xuanwo.io>
2026-10-28 00:29:22 +08:00

68 lines
1.8 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}"
)
def test_top_level_version_comes_from_native_module():
child_code = """
import importlib.metadata
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__ == native_module.__version__
"""
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],
check=False,
capture_output=True,
text=True,
env=env,
)
assert result.returncode == 0, result.stderr or result.stdout