Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 4 additions & 29 deletions narwhals/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from typing import Literal
from typing import Mapping
from typing import Sequence
from typing import TypeVar
from typing import cast
from typing import overload

from narwhals._expression_parsing import ExpansionKind
from narwhals._expression_parsing import ExprKind
Expand Down Expand Up @@ -57,46 +57,21 @@
from narwhals.dtypes import DType
from narwhals.schema import Schema
from narwhals.series import Series
from narwhals.typing import IntoDataFrameT
from narwhals.typing import IntoExpr
from narwhals.typing import IntoFrameT
from narwhals.typing import IntoSeriesT
from narwhals.typing import NativeFrame
from narwhals.typing import NativeLazyFrame
from narwhals.typing import _2DArray

_IntoSchema: TypeAlias = "Mapping[str, DType] | Schema | Sequence[str] | None"
FrameT = TypeVar("FrameT", "DataFrame[Any]", "LazyFrame[Any]")


@overload
def concat(
items: Iterable[DataFrame[IntoDataFrameT]],
items: Iterable[FrameT],
*,
how: Literal["horizontal", "vertical", "diagonal"] = "vertical",
) -> 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]:
) -> FrameT:
"""Concatenate multiple DataFrames, LazyFrames into a single entity.

Arguments:
Expand Down
31 changes: 4 additions & 27 deletions narwhals/stable/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
from narwhals.typing import _1DArray
from narwhals.typing import _2DArray

FrameT = TypeVar("FrameT", "DataFrame[Any]", "LazyFrame[Any]")
IntoSeriesT = TypeVar("IntoSeriesT", bound="IntoSeries", default=Any)
T = TypeVar("T", default=Any)
else:
Expand Down Expand Up @@ -2049,35 +2050,11 @@ def max_horizontal(*exprs: IntoExpr | Iterable[IntoExpr]) -> Expr:
return _stableify(nw.max_horizontal(*exprs))


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

Arguments:
Expand All @@ -2096,7 +2073,7 @@ def concat(
Raises:
TypeError: The items to concatenate should either all be eager, or all lazy
"""
return _stableify(nw.concat(items, how=how))
return cast("FrameT", _stableify(nw.concat(items, how=how)))
Copy link
Member Author

Choose a reason for hiding this comment

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

Not super happy about using cast - but it'll do for now



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 @@ -83,7 +83,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()])
nw.concat([nw.from_native(df, eager_only=True), nw.from_native(df).lazy()]) # type: ignore[type-var]
Comment on lines 83 to +86
Copy link
Member Author

Choose a reason for hiding this comment

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

#2339 (comment)

@MarcoGorelli I have no idea if that made sense πŸ˜…

I'm trying to say - if we merge (#2341) then I still want to base the new signatures on the new TypeVar.

We currently raise here, but the @overloads pass as valid



@pytest.mark.slow
Expand Down
Loading