66from itertools import chain
77from typing import TYPE_CHECKING
88
9+ from narwhals ._native import is_native_pandas
910from narwhals ._plan ._guards import (
1011 is_column_name_or_selector ,
1112 is_expr ,
1213 is_into_expr_column ,
1314 is_iterable_reject ,
1415 is_selector ,
1516)
16- from narwhals ._plan .exceptions import (
17- invalid_into_expr_error ,
18- is_iterable_pandas_error ,
19- is_iterable_polars_error ,
20- )
17+ from narwhals ._plan .exceptions import invalid_into_expr_error , is_iterable_error
2118from narwhals ._utils import qualified_type_name
22- from narwhals .dependencies import get_polars , is_pandas_dataframe , is_pandas_series
19+ from narwhals .dependencies import get_polars
2320from narwhals .exceptions import InvalidOperationError
2421
2522if TYPE_CHECKING :
2623 from collections .abc import Iterator
2724 from typing import Any , TypeVar
2825
29- import polars as pl
3026 from typing_extensions import TypeAlias , TypeIs
3127
3228 from narwhals ._plan .expr import Expr
@@ -126,7 +122,7 @@ def parse_into_expr_ir(
126122 expr = col (input )
127123 elif isinstance (input , list ):
128124 if list_as_series is None :
129- raise TypeError (input )
125+ raise TypeError (input ) # pragma: no cover
130126 expr = lit (list_as_series (input ))
131127 else :
132128 expr = lit (input , dtype = dtype )
@@ -140,9 +136,9 @@ def parse_into_selector_ir(input: ColumnNameOrSelector | Expr, /) -> SelectorIR:
140136 from narwhals ._plan import selectors as cs
141137
142138 selector = cs .by_name (input )
143- elif is_expr (input ):
139+ elif is_expr (input ): # pragma: no cover
144140 selector = input .meta .as_selector ()
145- else :
141+ else : # pragma: no cover
146142 msg = f"cannot turn { qualified_type_name (input )!r} into selector"
147143 raise TypeError (msg )
148144 return selector ._ir
@@ -194,8 +190,8 @@ def _parse_sort_by_into_iter_expr_ir(
194190) -> Iterator [ExprIR ]:
195191 for e in _parse_into_iter_expr_ir (by , * more_by ):
196192 if e .is_scalar :
197- msg = f"All expressions sort keys must preserve length, but got:\n { e !r} "
198- raise InvalidOperationError (msg )
193+ msg = f"All expressions sort keys must preserve length, but got:\n { e !r} " # pragma: no cover
194+ raise InvalidOperationError (msg ) # pragma: no cover
199195 yield e
200196
201197
@@ -216,14 +212,14 @@ def _parse_into_iter_selector_ir(
216212
217213 if not _is_empty_sequence (first_input ):
218214 if _is_iterable (first_input ) and not isinstance (first_input , str ):
219- if more_inputs :
215+ if more_inputs : # pragma: no cover
220216 raise invalid_into_expr_error (first_input , more_inputs , {})
221217 else :
222218 for into in first_input : # type: ignore[var-annotated]
223219 yield parse_into_selector_ir (into )
224220 else :
225221 yield parse_into_selector_ir (first_input )
226- for into in more_inputs :
222+ for into in more_inputs : # pragma: no cover
227223 yield parse_into_selector_ir (into )
228224
229225
@@ -298,18 +294,13 @@ def _combine_predicates(predicates: Iterator[ExprIR], /) -> ExprIR:
298294
299295
300296def _is_iterable (obj : Iterable [T ] | Any ) -> TypeIs [Iterable [T ]]:
301- if is_pandas_dataframe (obj ) or is_pandas_series (obj ):
302- raise is_iterable_pandas_error (obj )
303- if _is_polars (obj ):
304- raise is_iterable_polars_error (obj )
297+ if is_native_pandas (obj ) or (
298+ (pl := get_polars ())
299+ and isinstance (obj , (pl .Series , pl .Expr , pl .DataFrame , pl .LazyFrame ))
300+ ):
301+ raise is_iterable_error (obj )
305302 return isinstance (obj , Iterable )
306303
307304
308305def _is_empty_sequence (obj : Any ) -> bool :
309306 return isinstance (obj , Sequence ) and not obj
310-
311-
312- def _is_polars (obj : Any ) -> TypeIs [pl .Series | pl .Expr | pl .DataFrame | pl .LazyFrame ]:
313- return (pl := get_polars ()) is not None and isinstance (
314- obj , (pl .Series , pl .Expr , pl .DataFrame , pl .LazyFrame )
315- )
0 commit comments