| 
1 | 1 | from __future__ import annotations  | 
2 | 2 | 
 
  | 
3 |  | -from typing import TYPE_CHECKING  | 
 | 3 | +from typing import TYPE_CHECKING, Any  | 
4 | 4 | 
 
  | 
5 | 5 | import pytest  | 
6 | 6 | 
 
  | 
7 | 7 | import narwhals as nw  | 
8 | 8 | 
 
  | 
9 | 9 | if TYPE_CHECKING:  | 
10 | 10 |     from narwhals.typing import Frame  | 
 | 11 | +    from tests.utils import Constructor  | 
11 | 12 | 
 
  | 
12 | 13 | 
 
  | 
13 |  | -def test_native_namespace_polars() -> None:  | 
14 |  | -    pytest.importorskip("polars")  | 
15 |  | -    import polars as pl  | 
 | 14 | +data = {"a": [1, 2, 3]}  | 
16 | 15 | 
 
  | 
17 |  | -    df: Frame = nw.from_native(pl.DataFrame({"a": [1, 2, 3]}))  | 
18 |  | -    assert nw.get_native_namespace(df) is pl  | 
19 |  | -    assert nw.get_native_namespace(df.to_native()) is pl  | 
20 |  | -    assert nw.get_native_namespace(df.lazy().to_native()) is pl  | 
21 |  | -    assert nw.get_native_namespace(df["a"].to_native()) is pl  | 
22 |  | -    assert nw.get_native_namespace(df, df["a"].to_native()) is pl  | 
23 | 16 | 
 
  | 
 | 17 | +def _get_expected_namespace(constructor_name: str) -> Any | None:  # noqa: PLR0911  | 
 | 18 | +    """Get expected namespace module for a given constructor."""  | 
 | 19 | +    if "pandas" in constructor_name:  | 
 | 20 | +        import pandas as pd  | 
 | 21 | + | 
 | 22 | +        return pd  | 
 | 23 | +    elif "polars" in constructor_name:  | 
 | 24 | +        import polars as pl  | 
 | 25 | + | 
 | 26 | +        return pl  | 
 | 27 | +    elif "pyarrow_table" in constructor_name:  | 
 | 28 | +        import pyarrow as pa  | 
 | 29 | + | 
 | 30 | +        return pa  | 
 | 31 | +    elif "duckdb" in constructor_name:  | 
 | 32 | +        import duckdb  | 
 | 33 | + | 
 | 34 | +        return duckdb  | 
 | 35 | +    elif "cudf" in constructor_name:  # pragma: no cover  | 
 | 36 | +        import cudf  | 
 | 37 | + | 
 | 38 | +        return cudf  | 
 | 39 | +    elif "modin" in constructor_name:  | 
 | 40 | +        import modin.pandas as mpd  | 
 | 41 | + | 
 | 42 | +        return mpd  | 
 | 43 | +    elif "dask" in constructor_name:  | 
 | 44 | +        import dask.dataframe as dd  | 
 | 45 | + | 
 | 46 | +        return dd  | 
 | 47 | +    elif "ibis" in constructor_name:  | 
 | 48 | +        import ibis  | 
 | 49 | + | 
 | 50 | +        return ibis  | 
 | 51 | +    elif "sqlframe" in constructor_name:  | 
 | 52 | +        import sqlframe  | 
 | 53 | + | 
 | 54 | +        return sqlframe  | 
 | 55 | +    return None  # pragma: no cover  | 
 | 56 | + | 
 | 57 | + | 
 | 58 | +def test_native_namespace_frame(constructor: Constructor) -> None:  | 
 | 59 | +    constructor_name = constructor.__name__  | 
 | 60 | +    if constructor_name == "pyspark_lazy_constructor":  | 
 | 61 | +        pytest.skip(reason="Requires special handling for spark local vs spark connect")  | 
 | 62 | + | 
 | 63 | +    expected_namespace = _get_expected_namespace(constructor_name=constructor_name)  | 
 | 64 | + | 
 | 65 | +    df: Frame = nw.from_native(constructor(data))  | 
 | 66 | +    assert nw.get_native_namespace(df) is expected_namespace  | 
 | 67 | +    assert nw.get_native_namespace(df.to_native()) is expected_namespace  | 
 | 68 | +    assert nw.get_native_namespace(df.lazy().to_native()) is expected_namespace  | 
24 | 69 | 
 
  | 
25 |  | -def test_native_namespace_pandas() -> None:  | 
26 |  | -    pytest.importorskip("pandas")  | 
27 |  | -    import pandas as pd  | 
28 | 70 | 
 
  | 
29 |  | -    df: Frame = nw.from_native(pd.DataFrame({"a": [1, 2, 3]}), eager_only=True)  | 
30 |  | -    assert nw.get_native_namespace(df) is pd  | 
31 |  | -    assert nw.get_native_namespace(df.to_native()) is pd  | 
32 |  | -    assert nw.get_native_namespace(df["a"].to_native()) is pd  | 
33 |  | -    assert nw.get_native_namespace(df, df["a"].to_native()) is pd  | 
 | 71 | +def test_native_namespace_series(constructor_eager: Constructor) -> None:  | 
 | 72 | +    constructor_name = constructor_eager.__name__  | 
34 | 73 | 
 
  | 
 | 74 | +    expected_namespace = _get_expected_namespace(constructor_name=constructor_name)  | 
35 | 75 | 
 
  | 
36 |  | -def test_native_namespace_pyarrow() -> None:  | 
37 |  | -    pytest.importorskip("pyarrow")  | 
38 |  | -    import pyarrow as pa  | 
 | 76 | +    df: Frame = nw.from_native(constructor_eager(data), eager_only=True)  | 
39 | 77 | 
 
  | 
40 |  | -    df: Frame = nw.from_native(pa.table({"a": [1, 2, 3]}), eager_only=True)  | 
41 |  | -    assert nw.get_native_namespace(df) is pa  | 
42 |  | -    assert nw.get_native_namespace(df.to_native()) is pa  | 
43 |  | -    assert nw.get_native_namespace(df, df["a"].to_native()) is pa  | 
 | 78 | +    assert nw.get_native_namespace(df["a"].to_native()) is expected_namespace  | 
 | 79 | +    assert nw.get_native_namespace(df, df["a"].to_native()) is expected_namespace  | 
44 | 80 | 
 
  | 
45 | 81 | 
 
  | 
46 | 82 | def test_get_native_namespace_invalid() -> None:  | 
 | 
0 commit comments