Skip to content

Commit

Permalink
chore: update InvalidIntoExprError to mention nw.lit (#1393)
Browse files Browse the repository at this point in the history
* update intoexpr error to mention literal

* shorter lines

* update test

---------

Co-authored-by: Marco Gorelli <[email protected]>
  • Loading branch information
EdAbati and MarcoGorelli authored Nov 17, 2024
1 parent 1fb7b85 commit 39cfb07
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions narwhals/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ def __init__(self, message: str) -> None:
def from_invalid_type(cls, invalid_type: type) -> InvalidIntoExprError:
message = (
f"Expected an object which can be converted into an expression, got {invalid_type}\n\n"
"Hint: if you were trying to select a column which does not have a string column name, then "
"you should explicitly use `nw.col`.\nFor example, `df.select(nw.col(0))` if you have a column "
"named `0`."
"Hint:\n"
"- if you were trying to select a column which does not have a string\n"
" column name, then you should explicitly use `nw.col`.\n"
" For example, `df.select(nw.col(0))` if you have a column named `0`.\n"
"- if you were trying to create a new literal column, then you \n"
" should explicitly use `nw.lit`.\n"
" For example, `df.select(nw.lit(0))` if you want to create a new\n"
" column with literal value `0`."
)
return InvalidIntoExprError(message)
2 changes: 1 addition & 1 deletion tests/frame/select_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_non_string_select() -> None:

def test_int_select_pandas() -> None:
df = nw.from_native(pd.DataFrame({0: [1, 2], "b": [3, 4]}))
with pytest.raises(InvalidIntoExprError, match="\n\nHint: if you were trying"):
with pytest.raises(InvalidIntoExprError, match="\n\nHint:\n- if you were trying"):
nw.to_native(df.select(0)) # type: ignore[arg-type]


Expand Down

0 comments on commit 39cfb07

Please sign in to comment.