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

chore: IntoImplementation type alias #1917

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion narwhals/_duckdb/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def __getitem__(self: Self, item: str) -> DuckDBInterchangeSeries:

def collect(
self: Self,
backend: ModuleType | Implementation | str | None,
backend: Implementation | None,
**kwargs: Any,
) -> CompliantDataFrame:
if backend is None or backend is Implementation.PYARROW:
Expand Down
2 changes: 1 addition & 1 deletion narwhals/_spark_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def columns(self: Self) -> list[str]:

def collect(
self: Self,
backend: ModuleType | Implementation | str | None,
backend: Implementation | None,
**kwargs: Any,
) -> CompliantDataFrame:
if backend is Implementation.PANDAS:
Expand Down
5 changes: 3 additions & 2 deletions narwhals/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from narwhals.typing import IntoDataFrame
from narwhals.typing import IntoExpr
from narwhals.typing import IntoFrame
from narwhals.typing import IntoImplementation
from narwhals.typing import SizeUnit

PS = ParamSpec("PS")
Expand Down Expand Up @@ -504,7 +505,7 @@ def __arrow_c_stream__(self: Self, requested_schema: object | None = None) -> ob
def lazy(
self: Self,
*,
backend: ModuleType | Implementation | str | None = None,
backend: IntoImplementation | None = None,
) -> LazyFrame[Any]:
"""Restrict available API methods to lazy-only ones.

Expand Down Expand Up @@ -3828,7 +3829,7 @@ def __getitem__(self: Self, item: str | slice) -> NoReturn:

def collect(
self: Self,
backend: ModuleType | Implementation | str | None = None,
backend: IntoImplementation | None = None,
**kwargs: Any,
) -> DataFrame[Any]:
r"""Materialize this LazyFrame into a DataFrame.
Expand Down
5 changes: 3 additions & 2 deletions narwhals/stable/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
from narwhals.dtypes import DType
from narwhals.functions import ArrowStreamExportable
from narwhals.typing import IntoExpr
from narwhals.typing import IntoImplementation
from narwhals.typing import IntoSeries

T = TypeVar("T")
Expand Down Expand Up @@ -170,7 +171,7 @@ def __getitem__(self: Self, item: Any) -> Any:
def lazy(
self: Self,
*,
backend: ModuleType | Implementation | str | None = None,
backend: IntoImplementation | None = None,
) -> LazyFrame[Any]:
"""Restrict available API methods to lazy-only ones.

Expand Down Expand Up @@ -294,7 +295,7 @@ def _extract_compliant(self: Self, arg: Any) -> Any:

def collect(
self: Self,
backend: ModuleType | Implementation | str | None = None,
backend: IntoImplementation | None = None,
**kwargs: Any,
) -> DataFrame[Any]:
r"""Materialize this LazyFrame into a DataFrame.
Expand Down
4 changes: 4 additions & 0 deletions narwhals/typing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from types import ModuleType
from typing import TYPE_CHECKING
from typing import Any
from typing import Callable
Expand Down Expand Up @@ -244,6 +245,9 @@ def lit(
... return s.abs().to_native()
"""

IntoImplementation: TypeAlias = Union["Implementation", ModuleType, str]
"""Anything which can be converted to a Narwhals Implementation via Implementation.from_backend."""

Comment on lines +248 to +250
Copy link
Contributor

@dangotbanned dangotbanned Feb 4, 2025

Choose a reason for hiding this comment

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

#1917 (comment)

Oh, is that because there isn't a page for Implementation?

right, thanks 🀦

maybe we should make a type alias for Backend, rather than IntoImplementation?

@MarcoGorelli responding here as it has the context.

I did originally propose either IntoBackend or IntoImplementation in (#1914 (comment))

Although (IntoImplementation) is a mouthful, I do think its the best description of what is acceptable.

IntoBackend would make more sense if the enum were renamed.

I think having Implementation and Backend could be a point of confusion - since they're semantically similar but mean very different things to narwhals

Note

I'd personally leave the description as:
Anything which can be converted to a Narwhals Implementation.

SizeUnit: TypeAlias = Literal[
"b",
"kb",
Expand Down
5 changes: 2 additions & 3 deletions narwhals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from narwhals.dataframe import LazyFrame
from narwhals.series import Series
from narwhals.typing import DTypes
from narwhals.typing import IntoImplementation
from narwhals.typing import IntoSeriesT
from narwhals.typing import SizeUnit

Expand Down Expand Up @@ -136,9 +137,7 @@ def from_string(
return mapping.get(backend_name, Implementation.UNKNOWN)

@classmethod
def from_backend(
cls: type[Self], backend: str | Implementation | ModuleType
) -> Implementation:
def from_backend(cls: type[Self], backend: IntoImplementation) -> Implementation:
"""Instantiate from native namespace module, string, or Implementation.

Arguments:
Expand Down
Loading