Skip to content

Commit bfce451

Browse files
mgornypre-commit-ci[bot]dangotbanned
authored
test: Fix more tests to work without polars (and pandas, pyarrow) (#2245)
* test: Fix more tests to work without polars (and pandas, pyarrow) Split and/or skip tests requiring `polars` when they are unavailable. Whenever that was a low-hanging fruit, also imports of `pandas` and `pyarrow` were made optional. Part of bug #1726 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix typing * Refactor all optional test imports * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Convert duckdb and ibis instances * Also pa_csv * Ignore type hints on more tested-invalid calls * Fix ruff * Workaround ruff in `ibis_test.py` --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Dan Redding <[email protected]>
1 parent 19c3e9c commit bfce451

38 files changed

+504
-174
lines changed

tests/dependencies/is_into_dataframe_test.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,23 @@ def __narwhals_dataframe__(self) -> Self: # pragma: no cover
2828

2929

3030
def test_is_into_dataframe_pyarrow() -> None:
31-
pa = pytest.importorskip("pyarrow")
31+
pytest.importorskip("pyarrow")
32+
import pyarrow as pa
33+
3234
assert is_into_dataframe(pa.table(DATA))
3335

3436

3537
def test_is_into_dataframe_polars() -> None:
36-
pl = pytest.importorskip("polars")
38+
pytest.importorskip("polars")
39+
import polars as pl
40+
3741
assert is_into_dataframe(pl.DataFrame(DATA))
3842

3943

4044
def test_is_into_dataframe_pandas() -> None:
41-
pd = pytest.importorskip("pandas")
45+
pytest.importorskip("pandas")
46+
import pandas as pd
47+
4248
assert is_into_dataframe(pd.DataFrame(DATA))
4349
assert is_into_dataframe(nw.from_native(pd.DataFrame(DATA)))
4450

tests/dependencies/is_into_series_test.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,23 @@ def __narwhals_series__(self) -> Self: # pragma: no cover
2626

2727

2828
def test_is_into_series_pyarrow() -> None:
29-
pa = pytest.importorskip("pyarrow")
29+
pytest.importorskip("pyarrow")
30+
import pyarrow as pa
31+
3032
assert is_into_series(pa.chunked_array([["a", "b"]]))
3133

3234

3335
def test_is_into_series_polars() -> None:
34-
pl = pytest.importorskip("polars")
36+
pytest.importorskip("polars")
37+
import polars as pl
38+
3539
assert is_into_series(pl.Series([1, 2, 3]))
3640

3741

3842
def test_is_into_series_pandas() -> None:
39-
pd = pytest.importorskip("pandas")
43+
pytest.importorskip("pandas")
44+
import pandas as pd
45+
4046
assert is_into_series(pd.Series([1, 2, 3]))
4147
assert is_into_series(nw.from_native(pd.Series([1, 2, 3]), series_only=True))
4248

tests/dependencies/is_pandas_dataframe_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ def test_is_pandas_dataframe() -> None:
1111

1212

1313
def test_not_is_pandas_dataframe() -> None:
14-
pl = pytest.importorskip("polars")
14+
pytest.importorskip("polars")
15+
import polars as pl
16+
1517
assert not is_pandas_dataframe(pl.DataFrame())

tests/dtypes_test.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,12 @@ def test_pandas_fixed_offset_1302() -> None:
220220

221221

222222
def test_huge_int() -> None:
223-
duckdb = pytest.importorskip("duckdb")
224-
pl = pytest.importorskip("polars")
223+
pytest.importorskip("duckdb")
224+
pytest.importorskip("polars")
225+
226+
import duckdb
227+
import polars as pl
228+
225229
df = pl.DataFrame({"a": [1, 2, 3]})
226230

227231
if POLARS_VERSION >= (1, 18):
@@ -251,8 +255,12 @@ def test_huge_int() -> None:
251255

252256
@pytest.mark.skipif(PANDAS_VERSION < (1, 5), reason="too old for pyarrow")
253257
def test_decimal() -> None:
254-
duckdb = pytest.importorskip("duckdb")
255-
pl = pytest.importorskip("polars")
258+
pytest.importorskip("duckdb")
259+
pytest.importorskip("polars")
260+
261+
import duckdb
262+
import polars as pl
263+
256264
df = pl.DataFrame({"a": [1]}, schema={"a": pl.Decimal})
257265
result = nw.from_native(df).schema
258266
assert result["a"] == nw.Decimal
@@ -325,8 +333,12 @@ def test_dtype_is_x() -> None:
325333

326334
@pytest.mark.skipif(POLARS_VERSION < (1, 18), reason="too old for Int128")
327335
def test_huge_int_to_native() -> None:
328-
duckdb = pytest.importorskip("duckdb")
329-
pl = pytest.importorskip("polars")
336+
pytest.importorskip("duckdb")
337+
pytest.importorskip("polars")
338+
339+
import duckdb
340+
import polars as pl
341+
330342
df = pl.DataFrame({"a": [1, 2, 3]})
331343
df_casted = (
332344
nw.from_native(df).with_columns(a_int=nw.col("a").cast(nw.Int128())).to_native()
@@ -351,8 +363,12 @@ def test_huge_int_to_native() -> None:
351363

352364

353365
def test_cast_decimal_to_native() -> None:
354-
duckdb = pytest.importorskip("duckdb")
355-
pl = pytest.importorskip("polars")
366+
pytest.importorskip("duckdb")
367+
pytest.importorskip("polars")
368+
369+
import duckdb
370+
import polars as pl
371+
356372
data = {"a": [1, 2, 3]}
357373

358374
df = pl.DataFrame(data)
@@ -372,7 +388,7 @@ def test_cast_decimal_to_native() -> None:
372388
NotImplementedError, match="Casting to Decimal is not supported yet."
373389
):
374390
(
375-
nw.from_native(obj)
391+
nw.from_native(obj) # type: ignore[call-overload]
376392
.with_columns(a=nw.col("a").cast(nw.Decimal()))
377393
.to_native()
378394
)

tests/expr_and_series/cast_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,13 @@ def test_cast_struct(request: pytest.FixtureRequest, constructor: Constructor) -
311311

312312

313313
def test_raise_if_polars_dtype(constructor: Constructor) -> None:
314-
pl = pytest.importorskip("polars")
314+
pytest.importorskip("polars")
315+
import polars as pl
315316

316317
for dtype in [pl.String, pl.String()]:
317318
df = nw.from_native(constructor({"a": [1, 2, 3], "b": [4, 5, 6]}))
318319
with pytest.raises(TypeError, match="Expected Narwhals dtype, got:"):
319-
df.select(nw.col("a").cast(dtype))
320+
df.select(nw.col("a").cast(dtype)) # type: ignore[arg-type]
320321

321322

322323
def test_cast_time(request: pytest.FixtureRequest, constructor: Constructor) -> None:

tests/expr_and_series/nth_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def test_nth(
3939
reason="1.0.0",
4040
)
4141
def test_nth_not_supported() -> None: # pragma: no cover
42-
pl = pytest.importorskip("polars")
42+
pytest.importorskip("polars")
43+
import polars as pl
44+
4345
df = nw.from_native(pl.DataFrame(data))
4446
with pytest.raises(
4547
AttributeError, match="`nth` is only supported for Polars>=1.0.0."

tests/frame/arrow_c_stream_test.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
from tests.utils import POLARS_VERSION
77
from tests.utils import PYARROW_VERSION
88

9-
pl = pytest.importorskip("polars")
10-
pa = pytest.importorskip("pyarrow")
11-
pc = pytest.importorskip("pyarrow.compute")
9+
pytest.importorskip("polars")
10+
pytest.importorskip("pyarrow")
11+
pytest.importorskip("pyarrow.compute")
12+
13+
import polars as pl
14+
import pyarrow as pa
15+
import pyarrow.compute as pc
1216

1317

1418
@pytest.mark.skipif(POLARS_VERSION < (1, 3), reason="too old for pycapsule in Polars")

tests/frame/getitem_test.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ def test_slice_rows_with_step(
4545

4646

4747
def test_slice_rows_with_step_pyarrow() -> None:
48-
pa = pytest.importorskip("pyarrow")
48+
pytest.importorskip("pyarrow")
49+
import pyarrow as pa
50+
4951
with pytest.raises(
5052
NotImplementedError,
5153
match="Slicing with step is not supported on PyArrow tables",
@@ -54,7 +56,9 @@ def test_slice_rows_with_step_pyarrow() -> None:
5456

5557

5658
def test_slice_lazy_fails() -> None:
57-
pl = pytest.importorskip("polars")
59+
pytest.importorskip("polars")
60+
import polars as pl
61+
5862
with pytest.raises(TypeError, match="Slicing is not supported on LazyFrame"):
5963
_ = nw.from_native(pl.LazyFrame(data))[1:]
6064

tests/frame/interchange_native_namespace_test.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
import narwhals.stable.v1 as nw
99

10-
pl = pytest.importorskip("polars")
10+
pytest.importorskip("polars")
11+
import polars as pl
1112

1213
data: Mapping[str, Any] = {"a": [1, 2, 3], "b": [4.5, 6.7, 8.9], "z": ["x", "y", "w"]}
1314

@@ -34,7 +35,9 @@ def test_interchange() -> None:
3435
def test_ibis(
3536
tmpdir: pytest.TempdirFactory, request: pytest.FixtureRequest
3637
) -> None: # pragma: no cover
37-
ibis = pytest.importorskip("ibis")
38+
pytest.importorskip("ibis")
39+
import ibis
40+
3841
try:
3942
ibis.set_backend("duckdb")
4043
except ImportError:
@@ -52,7 +55,9 @@ def test_ibis(
5255

5356

5457
def test_duckdb() -> None:
55-
duckdb = pytest.importorskip("duckdb")
58+
pytest.importorskip("duckdb")
59+
import duckdb
60+
5661
df_pl = pl.DataFrame(data) # noqa: F841
5762

5863
rel = duckdb.sql("select * from df_pl")

tests/frame/interchange_schema_test.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import narwhals.stable.v1 as nw
1010
from tests.utils import IBIS_VERSION
1111

12-
pl = pytest.importorskip("polars")
12+
pytest.importorskip("polars")
13+
import polars as pl
1314

1415

1516
def test_interchange_schema() -> None:
@@ -73,7 +74,9 @@ def test_interchange_schema() -> None:
7374
def test_interchange_schema_ibis(
7475
tmpdir: pytest.TempdirFactory, request: pytest.FixtureRequest
7576
) -> None: # pragma: no cover
76-
ibis = pytest.importorskip("ibis")
77+
pytest.importorskip("ibis")
78+
import ibis
79+
7780
try:
7881
ibis.set_backend("duckdb")
7982
except ImportError:
@@ -164,7 +167,9 @@ def test_interchange_schema_ibis(
164167

165168

166169
def test_interchange_schema_duckdb() -> None:
167-
duckdb = pytest.importorskip("duckdb")
170+
pytest.importorskip("duckdb")
171+
import duckdb
172+
168173
df_pl = pl.DataFrame( # noqa: F841
169174
{
170175
"a": [1, 1, 2],

0 commit comments

Comments
 (0)