Skip to content

Commit b362e46

Browse files
authored
perf: Avoid redefining lambda in *Namespace.all (#2102)
1 parent 8888e2c commit b362e46

File tree

7 files changed

+24
-5
lines changed

7 files changed

+24
-5
lines changed

narwhals/_arrow/namespace.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from narwhals._expression_parsing import combine_evaluate_output_names
2727
from narwhals.typing import CompliantNamespace
2828
from narwhals.utils import Implementation
29+
from narwhals.utils import get_column_names
2930
from narwhals.utils import import_dtypes_module
3031

3132
if TYPE_CHECKING:
@@ -161,7 +162,7 @@ def all(self: Self) -> ArrowExpr:
161162
],
162163
depth=0,
163164
function_name="all",
164-
evaluate_output_names=lambda df: df.columns,
165+
evaluate_output_names=get_column_names,
165166
alias_output_names=None,
166167
backend_version=self._backend_version,
167168
version=self._version,

narwhals/_dask/namespace.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from narwhals._expression_parsing import combine_alias_output_names
2424
from narwhals._expression_parsing import combine_evaluate_output_names
2525
from narwhals.typing import CompliantNamespace
26+
from narwhals.utils import get_column_names
2627

2728
if TYPE_CHECKING:
2829
from typing_extensions import Self
@@ -55,7 +56,7 @@ def func(df: DaskLazyFrame) -> list[dx.Series]:
5556
func,
5657
depth=0,
5758
function_name="all",
58-
evaluate_output_names=lambda df: df.columns,
59+
evaluate_output_names=get_column_names,
5960
alias_output_names=None,
6061
backend_version=self._backend_version,
6162
version=self._version,

narwhals/_duckdb/namespace.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from narwhals._expression_parsing import combine_alias_output_names
2525
from narwhals._expression_parsing import combine_evaluate_output_names
2626
from narwhals.typing import CompliantNamespace
27+
from narwhals.utils import get_column_names
2728

2829
if TYPE_CHECKING:
2930
import duckdb
@@ -52,7 +53,7 @@ def _all(df: DuckDBLazyFrame) -> list[duckdb.Expression]:
5253
return DuckDBExpr(
5354
call=_all,
5455
function_name="all",
55-
evaluate_output_names=lambda df: df.columns,
56+
evaluate_output_names=get_column_names,
5657
alias_output_names=None,
5758
backend_version=self._backend_version,
5859
version=self._version,

narwhals/_pandas_like/namespace.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from narwhals._pandas_like.utils import horizontal_concat
2323
from narwhals._pandas_like.utils import vertical_concat
2424
from narwhals.typing import CompliantNamespace
25+
from narwhals.utils import get_column_names
2526
from narwhals.utils import import_dtypes_module
2627

2728
if TYPE_CHECKING:
@@ -135,7 +136,7 @@ def all(self: Self) -> PandasLikeExpr:
135136
],
136137
depth=0,
137138
function_name="all",
138-
evaluate_output_names=lambda df: df.columns,
139+
evaluate_output_names=get_column_names,
139140
alias_output_names=None,
140141
implementation=self._implementation,
141142
backend_version=self._backend_version,

narwhals/_spark_like/namespace.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from narwhals._spark_like.utils import maybe_evaluate_expr
1818
from narwhals._spark_like.utils import narwhals_to_native_dtype
1919
from narwhals.typing import CompliantNamespace
20+
from narwhals.utils import get_column_names
2021

2122
if TYPE_CHECKING:
2223
from pyspark.sql import Column
@@ -51,7 +52,7 @@ def _all(df: SparkLikeLazyFrame) -> list[Column]:
5152
return SparkLikeExpr(
5253
call=_all,
5354
function_name="all",
54-
evaluate_output_names=lambda df: df.columns,
55+
evaluate_output_names=get_column_names,
5556
alias_output_names=None,
5657
backend_version=self._backend_version,
5758
version=self._version,

narwhals/typing.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def aggregate(self, *exprs: Any) -> Self:
6262
... # `select` where all args are aggregations or literals
6363
# (so, no broadcasting is necessary).
6464

65+
@property
66+
def columns(self) -> Sequence[str]: ...
67+
6568

6669
class CompliantLazyFrame(Protocol):
6770
def __narwhals_lazyframe__(self) -> Self: ...
@@ -73,6 +76,9 @@ def aggregate(self, *exprs: Any) -> Self:
7376
... # `select` where all args are aggregations or literals
7477
# (so, no broadcasting is necessary).
7578

79+
@property
80+
def columns(self) -> Sequence[str]: ...
81+
7682

7783
CompliantFrameT_contra = TypeVar(
7884
"CompliantFrameT_contra",

narwhals/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ class _FullContext(_StoresImplementation, _LimitedContext, Protocol): # noqa: P
103103
- `_version`
104104
"""
105105

106+
class _StoresColumns(Protocol):
107+
@property
108+
def columns(self) -> Sequence[str]: ...
109+
106110

107111
class Version(Enum):
108112
V1 = auto()
@@ -1344,6 +1348,10 @@ def dtype_matches_time_unit_and_time_zone(
13441348
)
13451349

13461350

1351+
def get_column_names(frame: _StoresColumns, /) -> Sequence[str]:
1352+
return frame.columns
1353+
1354+
13471355
def _hasattr_static(obj: Any, attr: str) -> bool:
13481356
sentinel = object()
13491357
return getattr_static(obj, attr, sentinel) is not sentinel

0 commit comments

Comments
 (0)