From 5a55ddc8922213905578bcc170fce231966bf9c0 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Wed, 8 May 2024 15:22:12 +0100 Subject: [PATCH] better error message --- narwhals/dtypes.py | 8 ++++++++ tests/test_common.py | 2 ++ 2 files changed, 10 insertions(+) diff --git a/narwhals/dtypes.py b/narwhals/dtypes.py index db373e261..4d0cb15c2 100644 --- a/narwhals/dtypes.py +++ b/narwhals/dtypes.py @@ -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: diff --git a/tests/test_common.py b/tests/test_common.py index c589f3a4c..4ccf7a348 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -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])