|
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,7 +95,9 @@ 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:
|
97 |
| - if "polars" in str(type(dtype)): |
| 98 | + if (pl := get_polars()) is not None and isinstance( |
| 99 | + dtype, (pl.DataType, pl.DataType.__class__) |
| 100 | + ): |
98 | 101 | msg = (
|
99 | 102 | f"Expected Narwhals object, got: {type(dtype)}.\n\n"
|
100 | 103 | "Perhaps you:\n"
|
@@ -141,7 +144,7 @@ def narwhals_to_native_dtype(dtype: DType | type[DType], dtypes: DTypes) -> Any:
|
141 | 144 | if dtype == dtypes.Datetime or isinstance(dtype, dtypes.Datetime):
|
142 | 145 | dt_time_unit = getattr(dtype, "time_unit", "us")
|
143 | 146 | dt_time_zone = getattr(dtype, "time_zone", None)
|
144 |
| - return pl.Datetime(dt_time_unit, dt_time_zone) # type: ignore[arg-type] |
| 147 | + return pl.Datetime(dt_time_unit, dt_time_zone) |
145 | 148 | if dtype == dtypes.Duration or isinstance(dtype, dtypes.Duration):
|
146 | 149 | du_time_unit: Literal["us", "ns", "ms"] = getattr(dtype, "time_unit", "us")
|
147 | 150 | return pl.Duration(time_unit=du_time_unit)
|
|
0 commit comments