Skip to content

Commit

Permalink
list-comprehensionify
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Jan 26, 2025
1 parent b780de5 commit 5ee6009
Showing 1 changed file with 6 additions and 21 deletions.
27 changes: 6 additions & 21 deletions narwhals/_polars/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ def __init__(
self._version = version

def __getattr__(self: Self, attr: str) -> Any:
import polars as pl

from narwhals._polars.expr import PolarsExpr

def func(*args: Any, **kwargs: Any) -> Any:
Expand All @@ -50,8 +48,6 @@ def func(*args: Any, **kwargs: Any) -> Any:
return func

def nth(self: Self, *indices: int) -> PolarsExpr:
import polars as pl

from narwhals._polars.expr import PolarsExpr

if self._backend_version < (1, 0, 0):
Expand All @@ -62,8 +58,6 @@ def nth(self: Self, *indices: int) -> PolarsExpr:
)

def len(self: Self) -> PolarsExpr:
import polars as pl

from narwhals._polars.expr import PolarsExpr

if self._backend_version < (0, 20, 5):
Expand Down Expand Up @@ -114,8 +108,6 @@ def concat(
)

def lit(self: Self, value: Any, dtype: DType | None) -> PolarsExpr:
import polars as pl

from narwhals._polars.expr import PolarsExpr

if dtype is not None:
Expand All @@ -129,8 +121,6 @@ def lit(self: Self, value: Any, dtype: DType | None) -> PolarsExpr:
)

def mean_horizontal(self: Self, *exprs: IntoPolarsExpr) -> PolarsExpr:
import polars as pl

from narwhals._polars.expr import PolarsExpr

polars_exprs = cast("list[PolarsExpr]", parse_into_exprs(*exprs, namespace=self))
Expand All @@ -155,8 +145,6 @@ def concat_str(
separator: str,
ignore_nulls: bool,
) -> PolarsExpr:
import polars as pl

from narwhals._polars.expr import PolarsExpr

pl_exprs: list[pl.Expr] = [
Expand Down Expand Up @@ -215,17 +203,14 @@ def __init__(self: Self, version: Version, backend_version: tuple[int, ...]) ->
self._backend_version = backend_version

def by_dtype(self: Self, dtypes: Iterable[DType]) -> PolarsExpr:
import polars as pl

from narwhals._polars.expr import PolarsExpr

native_dtypes: list[pl.DataType | type[pl.DataType]] = []
for dtype in dtypes:
native_dtype_instantiated = narwhals_to_native_dtype(dtype, self._version)
if isinstance(dtype, type) and issubclass(dtype, DType):
native_dtypes.append(native_dtype_instantiated.__class__)
else:
native_dtypes.append(native_dtype_instantiated)
native_dtypes = [
narwhals_to_native_dtype(dtype, self._version).__class__
if isinstance(dtype, type) and issubclass(dtype, DType)
else narwhals_to_native_dtype(dtype, self._version)
for dtype in dtypes
]
return PolarsExpr(
pl.selectors.by_dtype(native_dtypes),
version=self._version,
Expand Down

0 comments on commit 5ee6009

Please sign in to comment.