From e31a1dab1f1a00f40b43a4f2b779d645ad4b4c53 Mon Sep 17 00:00:00 2001 From: Infrahub Date: Fri, 10 Jul 2026 12:35:58 +0000 Subject: [PATCH] fix(ctl): import pyarrow lazily in the JSON importer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pyarrow was imported at module top-level in the line-delimited JSON importer, which is reached from ctl.cli_commands at CLI startup. That forced every infrahubctl command to require pyarrow (the 'ctl' extra), so a slim install without it — e.g. the Infrahub server image — could not run even `infrahubctl schema load`. Import pyarrow lazily inside LineDelimitedJSONImporter.import_data, the only code path that uses it, and raise a clear install hint if it is missing. Now only `infrahubctl object load` needs the 'ctl' extra. Co-Authored-By: Claude Opus 4.8 (1M context) --- changelog/+lazy-pyarrow-import.fixed.md | 1 + infrahub_sdk/transfer/importer/json.py | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 changelog/+lazy-pyarrow-import.fixed.md diff --git a/changelog/+lazy-pyarrow-import.fixed.md b/changelog/+lazy-pyarrow-import.fixed.md new file mode 100644 index 000000000..5aadb7133 --- /dev/null +++ b/changelog/+lazy-pyarrow-import.fixed.md @@ -0,0 +1 @@ +Import `pyarrow` lazily in the line-delimited JSON importer so that `infrahubctl` commands other than `object load` no longer require the `ctl` extra (and its heavy `pyarrow` dependency) to be installed. diff --git a/infrahub_sdk/transfer/importer/json.py b/infrahub_sdk/transfer/importer/json.py index 32928df97..8b0c1e730 100644 --- a/infrahub_sdk/transfer/importer/json.py +++ b/infrahub_sdk/transfer/importer/json.py @@ -6,7 +6,6 @@ from pathlib import Path from typing import TYPE_CHECKING, Any -import pyarrow.json as pa_json import ujson from rich.progress import Progress @@ -53,6 +52,17 @@ def wrapped_task_output(self, start: str, end: str = "[green]done") -> Generator self.console.print(f"{end}") async def import_data(self, import_directory: Path, branch: str) -> None: + # pyarrow is a heavy, optional dependency used only to read the line-delimited JSON + # export. Import it lazily so the rest of infrahubctl works without it being installed; + # only `infrahubctl object load` reaches this code path. + try: + import pyarrow.json as pa_json # noqa: PLC0415 + except ModuleNotFoundError as exc: + raise ModuleNotFoundError( + "Loading objects requires pyarrow, install the 'ctl' extra of the infrahub-sdk " + "package, `pip install 'infrahub-sdk[ctl]'` or run `uv sync --extra ctl`." + ) from exc + node_file = import_directory / "nodes.json" relationship_file = import_directory / "relationships.json" for f in (node_file, relationship_file):