Skip to content

Commit

Permalink
better error message
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed May 8, 2024
1 parent ff70991 commit 5a55ddc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions narwhals/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ class Date(TemporalType): ...


def translate_dtype(plx: Any, dtype: DType) -> Any:
if "polars" in str(type(dtype)):
msg = (
f"Expected Narwhals object, got: {type(dtype)}.\n\n"
"Perhaps you:\n"
"- Forgot a `nw.from_native` somewhere?\n"
"- Used `pl.Int64` instead of `nw.Int64`?"
)
raise TypeError(msg)
if dtype == Float64:
return plx.Float64
if dtype == Float32:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,8 @@ def test_invalid() -> None:
df.select(nw.all() + nw.all())
with pytest.raises(TypeError, match="Perhaps you:"):
df.select([pl.col("a")]) # type: ignore[list-item]
with pytest.raises(TypeError, match="Perhaps you:"):
df.select([nw.col("a").cast(pl.Int64)])


@pytest.mark.parametrize("df_raw", [df_pandas])
Expand Down

0 comments on commit 5a55ddc

Please sign in to comment.