Skip to content

Commit

Permalink
feat: improve error message on check_columns_exist (#1799)
Browse files Browse the repository at this point in the history
* improve check column exists message

* remove join

* order missing
  • Loading branch information
DeaMariaLeon authored Jan 11, 2025
1 parent 5981743 commit fbe29a6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions narwhals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions tests/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit fbe29a6

Please sign in to comment.