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)