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
6 changes: 4 additions & 2 deletions narwhals/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@

if TYPE_CHECKING:
from types import ModuleType
from typing import Iterable
from typing import Mapping
from typing import Sized

import numpy as np
from typing_extensions import Self
Expand All @@ -53,8 +55,8 @@ def columns(self) -> Any: ...

def join(self, *args: Any, **kwargs: Any) -> Any: ...

class NativeSeries(Protocol):
def __len__(self) -> int: ...
class NativeSeries(Sized, Iterable[Any], Protocol):
def filter(self, *args: Any, **kwargs: Any) -> Any: ...

class DataFrameLike(Protocol):
def __dataframe__(self, *args: Any, **kwargs: Any) -> Any: ...
Expand Down
12 changes: 8 additions & 4 deletions tests/translate/from_native_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,20 @@ def __dataframe__(self) -> None: # pragma: no cover
assert result is mockdf


def test_from_native_altair_array_like() -> None:
def test_from_native_strict_native_series() -> None:
obj: list[int] = [1, 2, 3, 4]
array_like = cast("Iterable[Any]", obj)
not_array_like: Literal[1] = 1
np_array = pl.Series(obj).to_numpy()

with pytest.raises(TypeError, match="got.+list"):
false_positive_native_series = nw.from_native(obj, series_only=True) # noqa: F841
nw.from_native(obj, series_only=True) # type: ignore[call-overload]

with pytest.raises(TypeError, match="got.+list"):
true_negative_iterable = nw.from_native(array_like, series_only=True) # type: ignore[call-overload] # noqa: F841
nw.from_native(array_like, series_only=True) # type: ignore[call-overload]

with pytest.raises(TypeError, match="got.+int"):
true_negative_not_native_series = nw.from_native(not_array_like, series_only=True) # type: ignore[call-overload] # noqa: F841
nw.from_native(not_array_like, series_only=True) # type: ignore[call-overload]

with pytest.raises(TypeError, match="got.+numpy.ndarray"):
nw.from_native(np_array, series_only=True) # type: ignore[call-overload]
Loading