| 
 | 1 | +"""Serialization tests, based on [py-polars/tests/unit/test_serde.py].  | 
 | 2 | +
  | 
 | 3 | +See also [Pickling Class Instances](https://docs.python.org/3/library/pickle.html#pickling-class-instances).  | 
 | 4 | +
  | 
 | 5 | +[py-polars/tests/unit/test_serde.py]: https://github.com/pola-rs/polars/blob/a143eb0d7077ee9da2ce209a19c21d7f82228081/py-polars/tests/unit/test_serde.py  | 
 | 6 | +"""  | 
 | 7 | + | 
 | 8 | +from __future__ import annotations  | 
 | 9 | + | 
 | 10 | +import pickle  | 
 | 11 | +import string  | 
 | 12 | + | 
 | 13 | +# ruff: noqa: S301  | 
 | 14 | +from typing import TYPE_CHECKING, Protocol, TypeVar  | 
 | 15 | + | 
 | 16 | +import pytest  | 
 | 17 | + | 
 | 18 | +import narwhals as nw  | 
 | 19 | +import narwhals.stable.v1 as nw_v1  | 
 | 20 | +from narwhals.dtypes import DType  | 
 | 21 | +from narwhals.typing import IntoDType, NonNestedDType, TimeUnit  | 
 | 22 | + | 
 | 23 | +if TYPE_CHECKING:  | 
 | 24 | +    from narwhals.typing import DTypes  | 
 | 25 | +    from tests.utils import NestedOrEnumDType  | 
 | 26 | + | 
 | 27 | + | 
 | 28 | +IntoDTypeT = TypeVar("IntoDTypeT", bound=IntoDType)  | 
 | 29 | + | 
 | 30 | + | 
 | 31 | +namespaces = pytest.mark.parametrize("namespace", [nw, nw_v1])  | 
 | 32 | +time_units = pytest.mark.parametrize("time_unit", ["ns", "us", "ms", "s"])  | 
 | 33 | + | 
 | 34 | + | 
 | 35 | +class Identity(Protocol):  | 
 | 36 | +    def __call__(self, obj: IntoDTypeT, /) -> IntoDTypeT: ...  | 
 | 37 | + | 
 | 38 | + | 
 | 39 | +def _roundtrip_pickle(protocol: int | None = None) -> Identity:  | 
 | 40 | +    def fn(obj: IntoDTypeT, /) -> IntoDTypeT:  | 
 | 41 | +        result: IntoDTypeT = pickle.loads(pickle.dumps(obj, protocol))  | 
 | 42 | +        return result  | 
 | 43 | + | 
 | 44 | +    return fn  | 
 | 45 | + | 
 | 46 | + | 
 | 47 | +@pytest.fixture(  | 
 | 48 | +    params=[_roundtrip_pickle(), _roundtrip_pickle(4), _roundtrip_pickle(5)],  | 
 | 49 | +    ids=["pickle-None", "pickle-4", "pickle-5"],  | 
 | 50 | +)  | 
 | 51 | +def roundtrip(request: pytest.FixtureRequest) -> Identity:  | 
 | 52 | +    fn: Identity = request.param  | 
 | 53 | +    return fn  | 
 | 54 | + | 
 | 55 | + | 
 | 56 | +@namespaces  | 
 | 57 | +@time_units  | 
 | 58 | +def test_serde_datetime_dtype(  | 
 | 59 | +    namespace: DTypes, time_unit: TimeUnit, roundtrip: Identity  | 
 | 60 | +) -> None:  | 
 | 61 | +    dtype = namespace.Datetime(time_unit)  | 
 | 62 | +    result = roundtrip(dtype)  | 
 | 63 | +    assert result == dtype  | 
 | 64 | + | 
 | 65 | + | 
 | 66 | +@namespaces  | 
 | 67 | +@time_units  | 
 | 68 | +def test_serde_duration_dtype(  | 
 | 69 | +    namespace: DTypes, time_unit: TimeUnit, roundtrip: Identity  | 
 | 70 | +) -> None:  | 
 | 71 | +    dtype = namespace.Duration(time_unit)  | 
 | 72 | +    result = roundtrip(dtype)  | 
 | 73 | +    assert result == dtype  | 
 | 74 | + | 
 | 75 | + | 
 | 76 | +def test_serde_doubly_nested_struct_dtype(roundtrip: Identity) -> None:  | 
 | 77 | +    dtype = nw.Struct([nw.Field("a", nw.List(nw.String))])  | 
 | 78 | +    result = roundtrip(dtype)  | 
 | 79 | +    assert result == dtype  | 
 | 80 | + | 
 | 81 | + | 
 | 82 | +def test_serde_doubly_nested_array_dtype(roundtrip: Identity) -> None:  | 
 | 83 | +    dtype = nw.Array(nw.Array(nw.Int32(), 2), 3)  | 
 | 84 | +    result = roundtrip(dtype)  | 
 | 85 | +    assert result == dtype  | 
 | 86 | + | 
 | 87 | + | 
 | 88 | +def test_serde_dtype_class(roundtrip: Identity) -> None:  | 
 | 89 | +    dtype_class = nw.Datetime  | 
 | 90 | +    result = roundtrip(dtype_class)  | 
 | 91 | +    assert result == dtype_class  | 
 | 92 | +    assert isinstance(result, type)  | 
 | 93 | + | 
 | 94 | + | 
 | 95 | +def test_serde_enum_dtype(roundtrip: Identity) -> None:  | 
 | 96 | +    dtype = nw.Enum(["a", "b"])  | 
 | 97 | +    result = roundtrip(dtype)  | 
 | 98 | +    assert result == dtype  | 
 | 99 | +    assert isinstance(result, DType)  | 
 | 100 | + | 
 | 101 | + | 
 | 102 | +def test_serde_enum_v1_dtype(roundtrip: Identity) -> None:  | 
 | 103 | +    dtype = nw_v1.Enum()  | 
 | 104 | +    result = roundtrip(dtype)  | 
 | 105 | +    assert result == dtype  | 
 | 106 | +    assert isinstance(result, nw_v1.Enum)  | 
 | 107 | +    tp = type(result)  | 
 | 108 | +    with pytest.raises(TypeError):  | 
 | 109 | +        tp(["a", "b"])  # type: ignore[call-arg]  | 
 | 110 | + | 
 | 111 | + | 
 | 112 | +def test_serde_enum_deferred(roundtrip: Identity) -> None:  | 
 | 113 | +    pytest.importorskip("polars")  | 
 | 114 | +    import polars as pl  | 
 | 115 | + | 
 | 116 | +    categories = tuple(string.printable)  | 
 | 117 | +    dtype_pl = pl.Enum(categories)  | 
 | 118 | +    series_pl = pl.Series(categories).cast(dtype_pl).extend_constant(categories[5], 1000)  | 
 | 119 | +    series_nw = nw.from_native(series_pl, series_only=True)  | 
 | 120 | +    dtype_nw = series_nw.dtype  | 
 | 121 | +    assert isinstance(dtype_nw, nw.Enum)  | 
 | 122 | +    result = roundtrip(dtype_nw)  | 
 | 123 | +    assert isinstance(result, nw.Enum)  | 
 | 124 | +    assert result == dtype_nw  | 
 | 125 | +    assert result == series_nw.dtype  | 
 | 126 | +    assert dtype_nw == roundtrip(result)  | 
 | 127 | +    assert (  | 
 | 128 | +        type(result)(dtype_pl.categories).categories  | 
 | 129 | +        == roundtrip(result).categories  | 
 | 130 | +        == categories  | 
 | 131 | +        == result.categories  | 
 | 132 | +        == roundtrip(dtype_nw).categories  | 
 | 133 | +    )  | 
 | 134 | + | 
 | 135 | + | 
 | 136 | +def test_serde_non_nested_dtypes(  | 
 | 137 | +    non_nested_type: type[NonNestedDType], roundtrip: Identity  | 
 | 138 | +) -> None:  | 
 | 139 | +    dtype = non_nested_type()  | 
 | 140 | +    result = roundtrip(dtype)  | 
 | 141 | +    assert isinstance(result, DType)  | 
 | 142 | +    assert isinstance(result, non_nested_type)  | 
 | 143 | +    assert result == non_nested_type()  | 
 | 144 | +    assert result == non_nested_type  | 
 | 145 | + | 
 | 146 | + | 
 | 147 | +def test_serde_nested_dtypes(  | 
 | 148 | +    nested_dtype: NestedOrEnumDType, roundtrip: Identity  | 
 | 149 | +) -> None:  | 
 | 150 | +    result = roundtrip(nested_dtype)  | 
 | 151 | +    assert isinstance(result, DType)  | 
 | 152 | +    assert isinstance(result, nested_dtype.__class__)  | 
 | 153 | +    assert result == nested_dtype  | 
 | 154 | +    assert result == nested_dtype.base_type()  | 
0 commit comments