Skip to content

Commit

Permalink
correct annotation in infer_kind
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Feb 18, 2025
1 parent 8aa9da7 commit e92bfd2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
15 changes: 9 additions & 6 deletions narwhals/_expression_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from narwhals.typing import CompliantSeries
from narwhals.typing import CompliantSeriesT_co
from narwhals.typing import IntoExpr
from narwhals.typing import _1DArray

ArrowOrPandasLikeExpr = TypeVar(
"ArrowOrPandasLikeExpr", bound=Union[ArrowExpr, PandasLikeExpr]
Expand Down Expand Up @@ -424,15 +425,17 @@ def all_exprs_are_aggs_or_literals(*args: IntoExpr, **kwargs: IntoExpr) -> bool:
)


def infer_expr_kind(into_expr: IntoExpr, *, strings_are_column_names: bool) -> ExprKind:
def infer_kind(
obj: IntoExpr | _1DArray | object, *, strings_are_column_names: bool
) -> ExprKind:
from narwhals.expr import Expr
from narwhals.series import Series

if isinstance(into_expr, Expr):
return into_expr._metadata["kind"]
if isinstance(into_expr, Series) or is_numpy_array(into_expr):
if isinstance(obj, Expr):
return obj._metadata["kind"]
if isinstance(obj, Series) or is_numpy_array(obj):
return ExprKind.TRANSFORM
if isinstance(into_expr, str) and strings_are_column_names:
if isinstance(obj, str) and strings_are_column_names:
return ExprKind.TRANSFORM
return ExprKind.LITERAL

Expand All @@ -450,7 +453,7 @@ def apply_n_ary_operation(
for comparand in comparands
)
kinds = [
infer_expr_kind(comparand, strings_are_column_names=strings_are_column_names)
infer_kind(comparand, strings_are_column_names=strings_are_column_names)
for comparand in comparands
]

Expand Down
6 changes: 3 additions & 3 deletions narwhals/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from narwhals._expression_parsing import ExprKind
from narwhals._expression_parsing import all_exprs_are_aggs_or_literals
from narwhals._expression_parsing import check_expressions_transform
from narwhals._expression_parsing import infer_expr_kind
from narwhals._expression_parsing import infer_kind
from narwhals.dependencies import get_polars
from narwhals.dependencies import is_numpy_array
from narwhals.dependencies import is_numpy_array_1d
Expand Down Expand Up @@ -89,11 +89,11 @@ def _flatten_and_extract(
for expr in flatten(exprs):
compliant_expr = self._extract_compliant(expr)
out_exprs.append(compliant_expr)
out_kinds.append(infer_expr_kind(expr, strings_are_column_names=True))
out_kinds.append(infer_kind(expr, strings_are_column_names=True))
for alias, expr in named_exprs.items():
compliant_expr = self._extract_compliant(expr).alias(alias)
out_exprs.append(compliant_expr)
out_kinds.append(infer_expr_kind(expr, strings_are_column_names=True))
out_kinds.append(infer_kind(expr, strings_are_column_names=True))
return out_exprs, out_kinds

@abstractmethod
Expand Down
4 changes: 2 additions & 2 deletions narwhals/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from narwhals._expression_parsing import check_expressions_transform
from narwhals._expression_parsing import combine_metadata
from narwhals._expression_parsing import extract_compliant
from narwhals._expression_parsing import infer_expr_kind
from narwhals._expression_parsing import infer_kind
from narwhals.dataframe import DataFrame
from narwhals.dataframe import LazyFrame
from narwhals.dependencies import is_numpy_array
Expand Down Expand Up @@ -1438,7 +1438,7 @@ def then(self: Self, value: IntoExpr | Any) -> Then:

class Then(Expr):
def otherwise(self: Self, value: IntoExpr | Any) -> Expr:
kind = infer_expr_kind(value, strings_are_column_names=True)
kind = infer_kind(value, strings_are_column_names=True)

def func(plx: CompliantNamespace[Any]) -> CompliantExpr[Any]:
compliant_expr = self._to_compliant_expr(plx)
Expand Down

0 comments on commit e92bfd2

Please sign in to comment.