Skip to content

Commit 70bb464

Browse files
authored
Nicer error message when specifying wrong columns in ref datasets list (#203)
* Nicer error message when specifying wrong columns * Add changelog * Avoid shadowing builtins
1 parent c32e235 commit 70bb464

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

changelog/203.improvement.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improved the error message when running `ref datasets list` with the `--column` argument.
2+
If a column is specified that is not available, the error message now only mentions
3+
the invalid column name(s) and shows a list of available columns.

packages/ref-core/tests/unit/test_metrics.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def test_apply_filters_missing(apply_data_catalog):
351351

352352

353353
@pytest.mark.parametrize(
354-
"input, expected",
354+
"input_path, expected",
355355
(
356356
(Path("/example/test"), Path("test")),
357357
("/example/test", Path("test")),
@@ -360,8 +360,8 @@ def test_apply_filters_missing(apply_data_catalog):
360360
(Path("test/other"), Path("test/other")),
361361
),
362362
)
363-
def test_ensure_relative_path(input, expected):
364-
assert ensure_relative_path(input, root_directory=Path("/example")) == expected
363+
def test_ensure_relative_path(input_path, expected):
364+
assert ensure_relative_path(input_path, root_directory=Path("/example")) == expected
365365

366366

367367
def test_ensure_relative_path_failed():

packages/ref/src/cmip_ref/_config_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def _format_key_exception(exc: BaseException, _: type | None) -> str | None:
3333
return None
3434

3535

36-
def _format_exception(exc: BaseException, type: type | None) -> str:
36+
def _format_exception(exc: BaseException, type: type | None) -> str: # noqa: A002
3737
"""Format an exception into a string description of the error.
3838
3939
Parameters

packages/ref/src/cmip_ref/cli/datasets.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import errno
66
import os
7+
from collections.abc import Iterable
78
from pathlib import Path
89
from typing import Annotated
910

@@ -43,8 +44,17 @@ def list_(
4344
data_catalog = adapter.load_catalog(database, include_files=include_files, limit=limit)
4445

4546
if column:
46-
if not all(col in data_catalog.columns for col in column):
47-
logger.error(f"Column not found in data catalog: {column}")
47+
missing = set(column) - set(data_catalog.columns)
48+
if missing:
49+
50+
def format_(columns: Iterable[str]) -> str:
51+
return ", ".join(f"'{c}'" for c in sorted(columns))
52+
53+
logger.error(
54+
f"Column{'s' if len(missing) > 1 else ''} "
55+
f"{format_(missing)} not found in data catalog. "
56+
f"Choose from: {format_(data_catalog.columns)}"
57+
)
4858
raise typer.Exit(code=1)
4959
data_catalog = data_catalog[column]
5060

ruff.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ select = [
1313
"RUF",
1414
"UP",
1515
"S",
16+
"A",
1617
]
1718
unfixable = [
1819
"PD002",

0 commit comments

Comments
 (0)