From fbe29a689b6b63d677cdfe6f15f1ca379c9645c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Sat, 11 Jan 2025 19:02:26 +0100 Subject: [PATCH] feat: improve error message on check_columns_exist (#1799) * improve check column exists message * remove join * order missing --- narwhals/utils.py | 4 ++-- tests/utils_test.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/narwhals/utils.py b/narwhals/utils.py index 9743b80e3..73f0e20ab 100644 --- a/narwhals/utils.py +++ b/narwhals/utils.py @@ -1062,6 +1062,6 @@ def generate_repr(header: str, native_repr: str) -> str: def check_column_exists(columns: list[str], subset: list[str] | None) -> None: - if subset is not None and any(x not in columns for x in subset): - msg = f"Column(s) {subset} not found in {columns}" + if subset is not None and (missing := set(subset).difference(columns)): + msg = f"Column(s) {sorted(missing)} not found in {columns}" raise ColumnNotFoundError(msg) diff --git a/tests/utils_test.py b/tests/utils_test.py index 153cb8207..9748a954b 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -291,9 +291,9 @@ def test_parse_version(version: str, expected: tuple[int, ...]) -> None: def test_check_column_exists() -> None: columns = ["a", "b", "c"] - subset = ["a", "d"] + subset = ["d", "f"] with pytest.raises( ColumnNotFoundError, - match=re.escape("Column(s) ['a', 'd'] not found in ['a', 'b', 'c']"), + match=re.escape("Column(s) ['d', 'f'] not found in ['a', 'b', 'c']"), ): check_column_exists(columns, subset)