diff --git a/narwhals/typing.py b/narwhals/typing.py index b908e67112..9b36394571 100644 --- a/narwhals/typing.py +++ b/narwhals/typing.py @@ -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 @@ -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: ... diff --git a/tests/translate/from_native_test.py b/tests/translate/from_native_test.py index 7b48c8a79f..ab607f4a19 100644 --- a/tests/translate/from_native_test.py +++ b/tests/translate/from_native_test.py @@ -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]