Skip to content

Commit

Permalink
refactor: fix TRY checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ELC committed Jul 9, 2024
1 parent 964d80d commit 93ce621
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 30 deletions.
3 changes: 2 additions & 1 deletion narwhals/_arrow/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,5 @@ def validate_dataframe_comparand(other: Any) -> Any:
msg = "not implemented yet" # pragma: no cover
raise NotImplementedError(msg)
return other._native_series
raise AssertionError("Please report a bug")
error_message = "Please report a bug"
raise AssertionError(error_message)
3 changes: 2 additions & 1 deletion narwhals/_pandas_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def _validate_columns(self, columns: Sequence[str]) -> None:
raise ValueError(
msg,
)
raise AssertionError("Pls report bug")
error_message = "Please report a bug"
raise AssertionError(error_message)

def _from_native_dataframe(self, df: Any) -> Self:
return self.__class__(
Expand Down
5 changes: 3 additions & 2 deletions narwhals/_pandas_like/group_by.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ def agg_pandas( # noqa: PLR0913
try:
result_simple = grouped.agg(aggs)
except AttributeError as exc:
raise RuntimeError(
error_message = (
"Failed to aggregated - does your aggregation function return a scalar?"
) from exc
)
raise RuntimeError(error_message) from exc
result_simple.columns = [f"{a}_{b}" for a, b in result_simple.columns]
result_simple = result_simple.rename(columns=name_mapping).reset_index()
return from_dataframe(result_simple.loc[:, output_names])
Expand Down
3 changes: 2 additions & 1 deletion narwhals/_pandas_like/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ def validate_dataframe_comparand(index: Any, other: Any) -> Any:
backend_version=other._backend_version,
)
return other._native_series
raise AssertionError("Please report a bug")
error_message = "Please report a bug"
raise AssertionError(error_message)


def create_native_series(
Expand Down
3 changes: 2 additions & 1 deletion narwhals/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1853,7 +1853,8 @@ def __repr__(self) -> str: # pragma: no cover
)

def __getitem__(self, item: str | slice) -> Series | Self:
raise TypeError("Slicing is not supported on LazyFrame")
error_message = "Slicing is not supported on LazyFrame"
raise TypeError(error_message)

def collect(self) -> DataFrame[Any]:
r"""
Expand Down
3 changes: 2 additions & 1 deletion narwhals/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -3271,10 +3271,11 @@ def lit(value: Any, dtype: DType | None = None) -> Expr:
"""
if (np := get_numpy()) is not None and isinstance(value, np.ndarray):
raise ValueError(
error_message = (
"numpy arrays are not supported as literal values. "
"Consider using `with_columns` to create a new column from the array."
)
raise ValueError(error_message)

if isinstance(value, (list, tuple)):
msg = f"Nested datatypes are not supported yet. Got {value}"
Expand Down
3 changes: 2 additions & 1 deletion narwhals/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def concat(
"Only horizontal and vertical concatenations are supported"
)
if not items:
raise ValueError("No items to concatenate")
error_message = "No items to concatenate"
raise ValueError(error_message)
items = list(items)
validate_same_library(items)
validate_laziness(items)
Expand Down
48 changes: 32 additions & 16 deletions narwhals/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,25 +255,29 @@ def from_native( # noqa: PLR0915

if (pl := get_polars()) is not None and isinstance(native_dataframe, pl.DataFrame):
if series_only: # pragma: no cover (todo)
raise TypeError("Cannot only use `series_only` with polars.DataFrame")
error_message = "Cannot only use `series_only` with polars.DataFrame"
raise TypeError(error_message)
return DataFrame(
native_dataframe,
is_polars=True,
backend_version=parse_version(pl.__version__),
)
elif (pl := get_polars()) is not None and isinstance(native_dataframe, pl.LazyFrame):
if series_only: # pragma: no cover (todo)
raise TypeError("Cannot only use `series_only` with polars.LazyFrame")
error_message = "Cannot only use `series_only` with polars.LazyFrame"
raise TypeError(error_message)
if eager_only: # pragma: no cover (todo)
raise TypeError("Cannot only use `eager_only` with polars.LazyFrame")
error_message = "Cannot only use `eager_only` with polars.LazyFrame"
raise TypeError(error_message)
return LazyFrame(
native_dataframe,
is_polars=True,
backend_version=parse_version(pl.__version__),
)
elif (pd := get_pandas()) is not None and isinstance(native_dataframe, pd.DataFrame):
if series_only: # pragma: no cover (todo)
raise TypeError("Cannot only use `series_only` with dataframe")
error_message = "Cannot only use `series_only` with dataframe"
raise TypeError(error_message)
return DataFrame(
PandasDataFrame(
native_dataframe,
Expand All @@ -287,7 +291,8 @@ def from_native( # noqa: PLR0915
native_dataframe, mpd.DataFrame
): # pragma: no cover
if series_only:
raise TypeError("Cannot only use `series_only` with modin.DataFrame")
error_message = "Cannot only use `series_only` with modin.DataFrame"
raise TypeError(error_message)
return DataFrame(
PandasDataFrame(
native_dataframe,
Expand All @@ -301,7 +306,8 @@ def from_native( # noqa: PLR0915
native_dataframe, cudf.DataFrame
):
if series_only:
raise TypeError("Cannot only use `series_only` with modin.DataFrame")
error_message = "Cannot only use `series_only` with modin.DataFrame"
raise TypeError(error_message)
return DataFrame(
PandasDataFrame(
native_dataframe,
Expand All @@ -313,7 +319,8 @@ def from_native( # noqa: PLR0915
)
elif (pa := get_pyarrow()) is not None and isinstance(native_dataframe, pa.Table):
if series_only: # pragma: no cover (todo)
raise TypeError("Cannot only use `series_only` with arrow table")
error_message = "Cannot only use `series_only` with arrow table"
raise TypeError(error_message)
return DataFrame(
ArrowDataFrame(
native_dataframe, backend_version=parse_version(pa.__version__)
Expand All @@ -323,7 +330,8 @@ def from_native( # noqa: PLR0915
)
elif hasattr(native_dataframe, "__narwhals_dataframe__"): # pragma: no cover
if series_only: # pragma: no cover (todo)
raise TypeError("Cannot only use `series_only` with dataframe")
error_message = "Cannot only use `series_only` with dataframe"
raise TypeError(error_message)
# placeholder (0,) version here, as we wouldn't use it in this case anyway.
return DataFrame(
native_dataframe.__narwhals_dataframe__(),
Expand All @@ -332,9 +340,11 @@ def from_native( # noqa: PLR0915
)
elif hasattr(native_dataframe, "__narwhals_lazyframe__"): # pragma: no cover
if series_only: # pragma: no cover (todo)
raise TypeError("Cannot only use `series_only` with lazyframe")
error_message = "Cannot only use `series_only` with lazyframe"
raise TypeError(error_message)
if eager_only: # pragma: no cover (todo)
raise TypeError("Cannot only use `eager_only` with lazyframe")
error_message = "Cannot only use `eager_only` with lazyframe"
raise TypeError(error_message)
# placeholder (0,) version here, as we wouldn't use it in this case anyway.
return LazyFrame(
native_dataframe.__narwhals_lazyframe__(),
Expand All @@ -343,15 +353,17 @@ def from_native( # noqa: PLR0915
)
elif (pl := get_polars()) is not None and isinstance(native_dataframe, pl.Series):
if not allow_series: # pragma: no cover (todo)
raise TypeError("Please set `allow_series=True`")
error_message = "Please set `allow_series=True`"
raise TypeError(error_message)
return Series(
native_dataframe,
is_polars=True,
backend_version=parse_version(pl.__version__),
)
elif (pd := get_pandas()) is not None and isinstance(native_dataframe, pd.Series):
if not allow_series: # pragma: no cover (todo)
raise TypeError("Please set `allow_series=True`")
error_message = "Please set `allow_series=True`"
raise TypeError(error_message)
return Series(
PandasSeries(
native_dataframe,
Expand All @@ -365,7 +377,8 @@ def from_native( # noqa: PLR0915
native_dataframe, mpd.Series
): # pragma: no cover
if not allow_series: # pragma: no cover (todo)
raise TypeError("Please set `allow_series=True`")
error_message = "Please set `allow_series=True`"
raise TypeError(error_message)
return Series(
PandasSeries(
native_dataframe,
Expand All @@ -379,7 +392,8 @@ def from_native( # noqa: PLR0915
native_dataframe, cudf.Series
): # pragma: no cover
if not allow_series: # pragma: no cover (todo)
raise TypeError("Please set `allow_series=True`")
error_message = "Please set `allow_series=True`"
raise TypeError(error_message)
return Series(
PandasSeries(
native_dataframe,
Expand All @@ -393,7 +407,8 @@ def from_native( # noqa: PLR0915
native_dataframe, pa.ChunkedArray
):
if not allow_series: # pragma: no cover (todo)
raise TypeError("Please set `allow_series=True`")
error_message = "Please set `allow_series=True`"
raise TypeError(error_message)
return Series(
ArrowSeries(
native_dataframe, backend_version=parse_version(pa.__version__), name=""
Expand All @@ -403,7 +418,8 @@ def from_native( # noqa: PLR0915
)
elif hasattr(native_dataframe, "__narwhals_series__"): # pragma: no cover
if not allow_series: # pragma: no cover (todo)
raise TypeError("Please set `allow_series=True`")
error_message = "Please set `allow_series=True`"
raise TypeError(error_message)
# placeholder (0,) version here, as we wouldn't use it in this case anyway.
return Series(
native_dataframe.__narwhals_series__(), backend_version=(0,), is_polars=False
Expand Down
3 changes: 2 additions & 1 deletion narwhals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ def maybe_align_index(lhs: T, rhs: Series | BaseFrame[Any]) -> T:

def _validate_index(index: Any) -> None:
if not index.is_unique:
raise ValueError("given index doesn't have a unique index")
error_message = "given index doesn't have a unique index"
raise ValueError(error_message)

lhs_any = cast(Any, lhs)
rhs_any = cast(Any, rhs)
Expand Down
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ lint.ignore = [
'S',
'SLF001',
'TD',
'TRY003', # todo: enable
'TRY004'
]

[tool.ruff.lint.isort]
Expand Down
5 changes: 2 additions & 3 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@

def zip_strict(left: Sequence[Any], right: Sequence[Any]) -> Iterator[Any]:
if len(left) != len(right):
raise ValueError(
"left len != right len", len(left), len(right)
) # pragma: no cover
error_message = f"left {len(left)=} != right {len(right)=}"
raise ValueError(error_message) # pragma: no cover
return zip(left, right)


Expand Down

0 comments on commit 93ce621

Please sign in to comment.