Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add extra overload for concat for when it is not known statically whether inputs are eager or lazy #1783

Merged
merged 4 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions narwhals/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Protocol
from typing import TypeVar
from typing import Union
from typing import overload

from narwhals._pandas_like.utils import broadcast_align_and_extract_native
from narwhals.dataframe import DataFrame
Expand All @@ -33,6 +34,8 @@
from narwhals.dtypes import DType
from narwhals.schema import Schema
from narwhals.series import Series
from narwhals.typing import IntoDataFrameT
from narwhals.typing import IntoFrameT
from narwhals.typing import IntoSeriesT

class ArrowStreamExportable(Protocol):
Expand All @@ -41,11 +44,35 @@ def __arrow_c_stream__(
) -> object: ...


@overload
def concat(
items: Iterable[FrameT],
items: Iterable[DataFrame[IntoDataFrameT]],
*,
how: Literal["horizontal", "vertical", "diagonal"] = "vertical",
) -> FrameT:
) -> DataFrame[IntoDataFrameT]: ...


@overload
def concat(
items: Iterable[LazyFrame[IntoFrameT]],
*,
how: Literal["horizontal", "vertical", "diagonal"] = "vertical",
) -> LazyFrame[IntoFrameT]: ...


@overload
def concat(
items: Iterable[DataFrame[IntoDataFrameT] | LazyFrame[IntoFrameT]],
*,
how: Literal["horizontal", "vertical", "diagonal"] = "vertical",
) -> DataFrame[IntoDataFrameT] | LazyFrame[IntoFrameT]: ...


def concat(
items: Iterable[DataFrame[IntoDataFrameT] | LazyFrame[IntoFrameT]],
*,
how: Literal["horizontal", "vertical", "diagonal"] = "vertical",
) -> DataFrame[IntoDataFrameT] | LazyFrame[IntoFrameT]:
"""Concatenate multiple DataFrames, LazyFrames into a single entity.

Arguments:
Expand Down Expand Up @@ -191,7 +218,7 @@ def concat(
validate_laziness(items)
first_item = items[0]
plx = first_item.__narwhals_namespace__()
return first_item._from_compliant_dataframe( # type: ignore[return-value]
return first_item._from_compliant_dataframe(
plx.concat([df._compliant_frame for df in items], how=how),
)

Expand Down
22 changes: 15 additions & 7 deletions narwhals/stable/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3292,25 +3292,33 @@ def max_horizontal(*exprs: IntoExpr | Iterable[IntoExpr]) -> Expr:

@overload
def concat(
items: Iterable[DataFrame[Any]],
items: Iterable[DataFrame[IntoDataFrameT]],
*,
how: Literal["horizontal", "vertical", "diagonal"] = "vertical",
) -> DataFrame[Any]: ...
) -> DataFrame[IntoDataFrameT]: ...


@overload
def concat(
items: Iterable[LazyFrame[IntoFrameT]],
*,
how: Literal["horizontal", "vertical", "diagonal"] = "vertical",
) -> LazyFrame[IntoFrameT]: ...


@overload
def concat(
items: Iterable[LazyFrame[Any]],
items: Iterable[DataFrame[IntoDataFrameT] | LazyFrame[IntoFrameT]],
*,
how: Literal["horizontal", "vertical", "diagonal"] = "vertical",
) -> LazyFrame[Any]: ...
) -> DataFrame[IntoDataFrameT] | LazyFrame[IntoFrameT]: ...


def concat(
items: Iterable[DataFrame[Any] | LazyFrame[Any]],
items: Iterable[DataFrame[IntoDataFrameT] | LazyFrame[IntoFrameT]],
*,
how: Literal["horizontal", "vertical", "diagonal"] = "vertical",
) -> DataFrame[Any] | LazyFrame[Any]:
) -> DataFrame[IntoDataFrameT] | LazyFrame[IntoFrameT]:
"""Concatenate multiple DataFrames, LazyFrames into a single entity.

Arguments:
Expand Down Expand Up @@ -3446,7 +3454,7 @@ def concat(
β”‚ 4 ┆ null ┆ y β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜
"""
return _stableify(nw.concat(items, how=how)) # type: ignore[no-any-return]
return _stableify(nw.concat(items, how=how))
Comment on lines -3449 to +3457
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love it when this happens



def concat_str(
Expand Down
2 changes: 1 addition & 1 deletion tests/frame/invalid_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_validate_laziness() -> None:
TypeError,
match=("The items to concatenate should either all be eager, or all lazy"),
):
nw.concat([nw.from_native(df, eager_only=True), nw.from_native(df).lazy()]) # type: ignore[list-item]
nw.concat([nw.from_native(df, eager_only=True), nw.from_native(df).lazy()])


@pytest.mark.slow
Expand Down
Loading