Skip to content

Commit 62bffaf

Browse files
Refactor _get_df_family to raise an error instead.
1 parent 4557973 commit 62bffaf

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

pins/drivers.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from .errors import PinsInsecureReadError
66

77
from typing import Literal, Sequence
8-
from typing_extensions import assert_never
98

109
# TODO: move IFileSystem out of boards, to fix circular import
1110
# from .boards import IFileSystem
@@ -24,7 +23,8 @@ def _assert_is_pandas_df(x, file_type: str) -> None:
2423
)
2524

2625

27-
def _get_df_family(df) -> Literal["unknown", "pandas", "polars"]:
26+
def _get_df_family(df) -> Literal["pandas", "polars"]:
27+
"""Return the type of DataFrame, or raise NotImplementedError if we can't decide."""
2828
try:
2929
import polars as pl
3030
except ModuleNotFoundError:
@@ -36,16 +36,15 @@ def _get_df_family(df) -> Literal["unknown", "pandas", "polars"]:
3636

3737
is_pandas_df = isinstance(df, pd.DataFrame)
3838

39-
if not is_polars_df and not is_pandas_df:
40-
return "unknown"
41-
if is_polars_df and is_pandas_df: # Hybrid DataFrame type!
42-
return "unknown"
39+
if is_polars_df and is_pandas_df:
40+
raise NotImplementedError(
41+
"Hybrid DataFrames (simultaneously pandas and polars) are not supported."
42+
)
4343
elif is_polars_df:
4444
return "polars"
4545
elif is_pandas_df:
4646
return "pandas"
47-
else:
48-
assert_never(df)
47+
raise NotImplementedError(f"Unrecognized DataFrame type: {type(df)}")
4948

5049

5150
def load_path(meta, path_to_version):
@@ -234,15 +233,13 @@ def save_data(obj, fname, type=None, apply_suffix: bool = True) -> "str | Sequen
234233

235234

236235
def default_title(obj, name):
237-
df_family = _get_df_family(obj)
238-
239-
if df_family in ("pandas", "polars"):
240-
# TODO(compat): title says CSV rather than data.frame
241-
# see https://github.com/machow/pins-python/issues/5
242-
shape_str = " x ".join(map(str, obj.shape))
243-
return f"{name}: a pinned {shape_str} DataFrame"
244-
elif df_family == "unknown":
236+
try:
237+
_get_df_family(obj)
238+
except NotImplementedError:
245239
obj_name = type(obj).__qualname__
246240
return f"{name}: a pinned {obj_name} object"
247-
else:
248-
assert_never(df_family)
241+
242+
# TODO(compat): title says CSV rather than data.frame
243+
# see https://github.com/machow/pins-python/issues/5
244+
shape_str = " x ".join(map(str, obj.shape))
245+
return f"{name}: a pinned {shape_str} DataFrame"

0 commit comments

Comments
 (0)