From 1ea8f1d54defccc8d718c590a67c50ab0e57896e Mon Sep 17 00:00:00 2001 From: chenjian2664 Date: Sat, 7 Nov 2026 09:14:39 +0800 Subject: [PATCH] Read py-catalog-impl from the catalog configuration load_catalog only looked for py-catalog-impl in its keyword arguments, so the documented .pyiceberg.yaml and PYICEBERG_CATALOG__* settings were ignored and loading failed with "URI missing". When the implementation was passed as an argument, the custom catalog also lost the properties from the configuration. Read it from the merged configuration and pass that configuration to the custom catalog. test_missing_uri patched the configuration with a MagicMock rather than a Config, so it now patches an empty Config again. --- pyiceberg/catalog/__init__.py | 4 ++-- tests/catalog/test_base.py | 13 +++++++++++++ tests/cli/test_console.py | 4 ++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/pyiceberg/catalog/__init__.py b/pyiceberg/catalog/__init__.py index ce0902f338..6336c5ebb3 100644 --- a/pyiceberg/catalog/__init__.py +++ b/pyiceberg/catalog/__init__.py @@ -304,14 +304,14 @@ def load_catalog(name: str | None = None, **properties: str | None) -> Catalog: catalog_type: CatalogType | None provided_catalog_type = conf.get(TYPE) - if catalog_impl := properties.get(PY_CATALOG_IMPL): + if catalog_impl := conf.get(PY_CATALOG_IMPL): if provided_catalog_type: raise ValueError( "Must not set both catalog type and py-catalog-impl configurations, " f"but found type {provided_catalog_type} and py-catalog-impl {catalog_impl}" ) - if catalog := _import_catalog(name, catalog_impl, properties): + if catalog := _import_catalog(name, cast(str, catalog_impl), cast(dict[str, str], conf)): logger.info("Loaded Catalog: %s", catalog_impl) return catalog else: diff --git a/tests/catalog/test_base.py b/tests/catalog/test_base.py index 1a47478313..8e1891a908 100644 --- a/tests/catalog/test_base.py +++ b/tests/catalog/test_base.py @@ -21,6 +21,7 @@ from pathlib import PosixPath import pytest +from pytest_mock import MockerFixture from pyiceberg.catalog import Catalog, load_catalog from pyiceberg.catalog.memory import InMemoryCatalog @@ -54,6 +55,18 @@ def test_load_catalog_impl_does_not_exist() -> None: assert "Could not initialize Catalog: pyiceberg.does.not.exist.Catalog" in str(exc_info.value) +def test_load_catalog_impl_from_config(mocker: MockerFixture) -> None: + mocker.patch( + "pyiceberg.catalog._ENV_CONFIG.get_catalog_config", + return_value={"py-catalog-impl": "pyiceberg.catalog.memory.InMemoryCatalog", "test.key": "test.value"}, + ) + + catalog = load_catalog("catalog") + + assert isinstance(catalog, InMemoryCatalog) + assert catalog.properties["test.key"] == "test.value" + + def test_load_catalog_has_type_and_impl() -> None: with pytest.raises(ValueError) as exc_info: load_catalog("catalog", **{"py-catalog-impl": "pyiceberg.does.not.exist.Catalog", "type": "sql"}) diff --git a/tests/cli/test_console.py b/tests/cli/test_console.py index ebf1a5a90b..669c3faa93 100644 --- a/tests/cli/test_console.py +++ b/tests/cli/test_console.py @@ -44,13 +44,13 @@ def test_missing_uri(mocker: MockFixture, empty_home_dir_path: str) -> None: # mock to prevent parsing ~/.pyiceberg.yaml or {PYICEBERG_HOME}/.pyiceberg.yaml mocker.patch.dict(os.environ, values={"HOME": empty_home_dir_path, "PYICEBERG_HOME": empty_home_dir_path}) - mocker.patch("pyiceberg.catalog._ENV_CONFIG", return_value=Config()) + mocker.patch("pyiceberg.catalog._ENV_CONFIG", Config()) runner = CliRunner() result = runner.invoke(run, ["list"]) assert result.exit_code == 1 - assert result.output == "Could not initialize catalog with the following properties: {}\n" + assert "URI missing, please provide using --uri" in result.output def test_hive_catalog_missing_uri_shows_helpful_error(mocker: MockFixture) -> None: