Skip to content

Commit 1ea8f1d

Browse files
committed
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.
1 parent 718c790 commit 1ea8f1d

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

‎pyiceberg/catalog/__init__.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,14 @@ def load_catalog(name: str | None = None, **properties: str | None) -> Catalog:
304304
catalog_type: CatalogType | None
305305
provided_catalog_type = conf.get(TYPE)
306306

307-
if catalog_impl := properties.get(PY_CATALOG_IMPL):
307+
if catalog_impl := conf.get(PY_CATALOG_IMPL):
308308
if provided_catalog_type:
309309
raise ValueError(
310310
"Must not set both catalog type and py-catalog-impl configurations, "
311311
f"but found type {provided_catalog_type} and py-catalog-impl {catalog_impl}"
312312
)
313313

314-
if catalog := _import_catalog(name, catalog_impl, properties):
314+
if catalog := _import_catalog(name, cast(str, catalog_impl), cast(dict[str, str], conf)):
315315
logger.info("Loaded Catalog: %s", catalog_impl)
316316
return catalog
317317
else:

‎tests/catalog/test_base.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pathlib import PosixPath
2222

2323
import pytest
24+
from pytest_mock import MockerFixture
2425

2526
from pyiceberg.catalog import Catalog, load_catalog
2627
from pyiceberg.catalog.memory import InMemoryCatalog
@@ -54,6 +55,18 @@ def test_load_catalog_impl_does_not_exist() -> None:
5455
assert "Could not initialize Catalog: pyiceberg.does.not.exist.Catalog" in str(exc_info.value)
5556

5657

58+
def test_load_catalog_impl_from_config(mocker: MockerFixture) -> None:
59+
mocker.patch(
60+
"pyiceberg.catalog._ENV_CONFIG.get_catalog_config",
61+
return_value={"py-catalog-impl": "pyiceberg.catalog.memory.InMemoryCatalog", "test.key": "test.value"},
62+
)
63+
64+
catalog = load_catalog("catalog")
65+
66+
assert isinstance(catalog, InMemoryCatalog)
67+
assert catalog.properties["test.key"] == "test.value"
68+
69+
5770
def test_load_catalog_has_type_and_impl() -> None:
5871
with pytest.raises(ValueError) as exc_info:
5972
load_catalog("catalog", **{"py-catalog-impl": "pyiceberg.does.not.exist.Catalog", "type": "sql"})

‎tests/cli/test_console.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@
4444
def test_missing_uri(mocker: MockFixture, empty_home_dir_path: str) -> None:
4545
# mock to prevent parsing ~/.pyiceberg.yaml or {PYICEBERG_HOME}/.pyiceberg.yaml
4646
mocker.patch.dict(os.environ, values={"HOME": empty_home_dir_path, "PYICEBERG_HOME": empty_home_dir_path})
47-
mocker.patch("pyiceberg.catalog._ENV_CONFIG", return_value=Config())
47+
mocker.patch("pyiceberg.catalog._ENV_CONFIG", Config())
4848

4949
runner = CliRunner()
5050
result = runner.invoke(run, ["list"])
5151

5252
assert result.exit_code == 1
53-
assert result.output == "Could not initialize catalog with the following properties: {}\n"
53+
assert "URI missing, please provide using --uri" in result.output
5454

5555

5656
def test_hive_catalog_missing_uri_shows_helpful_error(mocker: MockFixture) -> None:

0 commit comments

Comments
 (0)