Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 5 additions & 7 deletions narwhals/_compliant/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
ToNarwhals,
ToNarwhalsT_co,
)
from narwhals._typing_compat import deprecated
from narwhals._typing_compat import assert_never, deprecated
from narwhals._utils import (
Version,
_StoresNative,
Expand Down Expand Up @@ -475,9 +475,8 @@ def __getitem__( # noqa: C901, PLR0912
compliant = self._select_multi_name(columns.native)
elif is_sequence_like(columns):
compliant = self._select_multi_name(columns)
else: # pragma: no cover
msg = f"Unreachable code, got unexpected type: {type(columns)}"
raise AssertionError(msg)
else:
assert_never(columns)

if not is_slice_none(rows):
if isinstance(rows, int):
Expand All @@ -488,8 +487,7 @@ def __getitem__( # noqa: C901, PLR0912
compliant = compliant._gather(rows.native)
elif is_sized_multi_index_selector(rows):
compliant = compliant._gather(rows)
else: # pragma: no cover
msg = f"Unreachable code, got unexpected type: {type(rows)}"
raise AssertionError(msg)
else:
assert_never(rows)

return compliant
6 changes: 3 additions & 3 deletions narwhals/_compliant/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
NativeSeriesT_co,
)
from narwhals._translate import FromIterable, FromNative, NumpyConvertible, ToNarwhals
from narwhals._typing_compat import assert_never
from narwhals._utils import (
_StoresCompliant,
_StoresNative,
Expand Down Expand Up @@ -330,9 +331,8 @@ def __getitem__(self, item: MultiIndexSelector[Self]) -> Self:
return self._gather(item.native)
elif is_sized_multi_index_selector(item):
return self._gather(item)
else: # pragma: no cover
msg = f"Unreachable code, got unexpected type: {type(item)}"
raise AssertionError(msg)
else:
assert_never(item)

@property
def str(self) -> EagerSeriesStringNamespace[Self, NativeSeriesT]: ...
Expand Down
5 changes: 3 additions & 2 deletions narwhals/_pandas_like/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
select_columns_by_name,
set_index,
)
from narwhals._typing_compat import assert_never
from narwhals._utils import (
Implementation,
is_list_of,
Expand Down Expand Up @@ -374,8 +375,8 @@ def is_between(
res = ser.gt(lower_bound) & ser.lt(upper_bound)
elif closed == "both":
res = ser.ge(lower_bound) & ser.le(upper_bound)
else: # pragma: no cover
raise AssertionError
else:
assert_never(closed)
return self._with_native(res).alias(ser.name)

def is_in(self, other: Any) -> Self:
Expand Down
19 changes: 18 additions & 1 deletion narwhals/_typing_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
else:
from typing_extensions import TypeVar, deprecated

if sys.version_info >= (3, 11):
from typing import Never, assert_never
else:
from typing_extensions import Never, assert_never

_Fn = TypeVar("_Fn", bound=Callable[..., Any])


Expand Down Expand Up @@ -65,9 +70,21 @@ def wrapper(func: _Fn, /) -> _Fn:

return wrapper

if sys.version_info >= (3, 11):
from typing import assert_never
else:
_ASSERT_NEVER_REPR_MAX_LENGTH = 100

def assert_never(arg: Never, /) -> Never:
value = repr(arg)
if len(value) > _ASSERT_NEVER_REPR_MAX_LENGTH:
value = value[:_ASSERT_NEVER_REPR_MAX_LENGTH] + "..."
msg = f"Expected code to be unreachable, but got: {value}"
raise AssertionError(msg)

# TODO @dangotbanned: Remove after dropping `3.8` (#2084)
# - https://github.com/narwhals-dev/narwhals/pull/2064#discussion_r1965921386
from typing import Protocol as Protocol38


__all__ = ["Protocol38", "TypeVar", "deprecated"]
__all__ = ["Protocol38", "TypeVar", "assert_never", "deprecated"]
5 changes: 2 additions & 3 deletions narwhals/stable/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import narwhals as nw
from narwhals import exceptions, functions as nw_f
from narwhals._typing_compat import TypeVar
from narwhals._typing_compat import TypeVar, assert_never
from narwhals._utils import (
Implementation,
Version,
Expand Down Expand Up @@ -462,8 +462,7 @@ def _stableify(
return Series(obj._compliant_series._with_version(Version.V1), level=obj._level)
if isinstance(obj, NwExpr):
return Expr(obj._to_compliant_expr, obj._metadata)
msg = f"Expected DataFrame, LazyFrame, Series, or Expr, got: {type(obj)}" # pragma: no cover
raise AssertionError(msg)
assert_never(obj)


@overload
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ exclude_also = [
'if "pyspark" in str\(constructor',
'if "pyspark_connect" in str\(constructor',
'pytest.skip\(',
'assert_never\(',
]

[tool.mypy]
Expand Down
Loading