Skip to content

Commit 0ee67a7

Browse files
authored
Skip catalog loading for the version command (#3146)
# Rationale for this change `pyiceberg version` should print the installed PyIceberg version even when `.pyiceberg.yaml` or catalog-related environment variables are invalid. CLI group callback eagerly loads the catalog for every subcommand, so `version` can fail before it prints anything. This change skips catalog loading for the `version` subcommand and adds a regression test for that behavior. ## Are these changes tested? Yes. - Added `test_version_does_not_load_catalog` in `tests/cli/test_console.py` ## Are there any user-facing changes? Yes. - `pyiceberg version` now prints the version without requiring valid catalog configuration.
1 parent 8691d94 commit 0ee67a7

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

pyiceberg/cli/console.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ def run(
101101
else:
102102
ctx.obj["output"] = JsonOutput(verbose=verbose)
103103

104+
if ctx.invoked_subcommand == "version":
105+
return
106+
104107
try:
105108
ctx.obj["catalog"] = load_catalog(catalog, **properties)
106109
except Exception as e:

tests/cli/test_console.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from click.testing import CliRunner
2626
from pytest_mock import MockFixture
2727

28+
from pyiceberg import __version__
2829
from pyiceberg.catalog.memory import InMemoryCatalog
2930
from pyiceberg.cli.console import run
3031
from pyiceberg.io import WAREHOUSE
@@ -61,6 +62,17 @@ def test_hive_catalog_missing_uri_shows_helpful_error(mocker: MockFixture) -> No
6162
assert "'uri'" not in result.output
6263

6364

65+
def test_version_does_not_load_catalog(mocker: MockFixture) -> None:
66+
mock_load_catalog = mocker.patch("pyiceberg.cli.console.load_catalog", side_effect=Exception("should not be called"))
67+
68+
runner = CliRunner()
69+
result = runner.invoke(run, ["version"])
70+
71+
assert result.exit_code == 0
72+
assert result.output == f"{__version__}\n"
73+
mock_load_catalog.assert_not_called()
74+
75+
6476
@pytest.fixture(autouse=True)
6577
def env_vars(mocker: MockFixture) -> None:
6678
mocker.patch.dict(os.environ, MOCK_ENVIRONMENT)

0 commit comments

Comments
 (0)