|
8 | 8 | from narwhals.dtypes import DType
|
9 | 9 | from narwhals.typing import DTypes
|
10 | 10 |
|
| 11 | +from narwhals.dependencies import get_polars |
11 | 12 | from narwhals.utils import parse_version
|
12 | 13 |
|
13 | 14 |
|
@@ -94,6 +95,17 @@ def native_to_narwhals_dtype(dtype: Any, dtypes: DTypes) -> DType:
|
94 | 95 |
|
95 | 96 |
|
96 | 97 | def narwhals_to_native_dtype(dtype: DType | type[DType], dtypes: DTypes) -> Any:
|
| 98 | + if (pl := get_polars()) is not None and isinstance( |
| 99 | + dtype, (pl.DataType, pl.DataType.__class__) |
| 100 | + ): |
| 101 | + msg = ( |
| 102 | + f"Expected Narwhals object, got: {type(dtype)}.\n\n" |
| 103 | + "Perhaps you:\n" |
| 104 | + "- Forgot a `nw.from_native` somewhere?\n" |
| 105 | + "- Used `pl.Int64` instead of `nw.Int64`?" |
| 106 | + ) |
| 107 | + raise TypeError(msg) |
| 108 | + |
97 | 109 | import polars as pl # ignore-banned-import()
|
98 | 110 |
|
99 | 111 | if dtype == dtypes.Float64:
|
@@ -132,7 +144,7 @@ def narwhals_to_native_dtype(dtype: DType | type[DType], dtypes: DTypes) -> Any:
|
132 | 144 | if dtype == dtypes.Datetime or isinstance(dtype, dtypes.Datetime):
|
133 | 145 | dt_time_unit = getattr(dtype, "time_unit", "us")
|
134 | 146 | dt_time_zone = getattr(dtype, "time_zone", None)
|
135 |
| - return pl.Datetime(dt_time_unit, dt_time_zone) # type: ignore[arg-type] |
| 147 | + return pl.Datetime(dt_time_unit, dt_time_zone) |
136 | 148 | if dtype == dtypes.Duration or isinstance(dtype, dtypes.Duration):
|
137 | 149 | du_time_unit: Literal["us", "ns", "ms"] = getattr(dtype, "time_unit", "us")
|
138 | 150 | return pl.Duration(time_unit=du_time_unit)
|
|
0 commit comments