-
Notifications
You must be signed in to change notification settings - Fork 170
test: Add serde tests for DTypes
#3205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
219fbe4
test: Add serde tests for `DType`s
dangotbanned 4cc022b
test: Move dtype fixtures to `conftest`
dangotbanned c4e4d23
test: Reuse fixtures for coverage
dangotbanned b2ba749
test: Indirectly test `_DeferredIterable`
dangotbanned 29e580d
Update tests/serde_test.py
dangotbanned 78f2f22
more dtype eq
dangotbanned cafb17d
Merge branch 'dtype-serde' of https://github.com/narwhals-dev/narwhalβ¦
dangotbanned 1cf172e
Merge branch 'main' into dtype-serde
dangotbanned File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| """Serialization tests, based on [py-polars/tests/unit/test_serde.py]. | ||
|
|
||
| See also [Pickling Class Instances](https://docs.python.org/3/library/pickle.html#pickling-class-instances). | ||
|
|
||
| [py-polars/tests/unit/test_serde.py]: https://github.com/pola-rs/polars/blob/a143eb0d7077ee9da2ce209a19c21d7f82228081/py-polars/tests/unit/test_serde.py | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pickle | ||
| import string | ||
|
|
||
| # ruff: noqa: S301 | ||
| from typing import TYPE_CHECKING, Protocol, TypeVar | ||
|
|
||
| import pytest | ||
|
|
||
| import narwhals as nw | ||
| import narwhals.stable.v1 as nw_v1 | ||
| from narwhals.dtypes import DType | ||
| from narwhals.typing import IntoDType, NonNestedDType, TimeUnit | ||
|
|
||
| if TYPE_CHECKING: | ||
| from narwhals.typing import DTypes | ||
| from tests.utils import NestedOrEnumDType | ||
|
|
||
|
|
||
| IntoDTypeT = TypeVar("IntoDTypeT", bound=IntoDType) | ||
|
|
||
|
|
||
| namespaces = pytest.mark.parametrize("namespace", [nw, nw_v1]) | ||
| time_units = pytest.mark.parametrize("time_unit", ["ns", "us", "ms", "s"]) | ||
|
|
||
|
|
||
| class Identity(Protocol): | ||
| def __call__(self, obj: IntoDTypeT, /) -> IntoDTypeT: ... | ||
|
|
||
|
|
||
| def _roundtrip_pickle(protocol: int | None = None) -> Identity: | ||
| def fn(obj: IntoDTypeT, /) -> IntoDTypeT: | ||
| result: IntoDTypeT = pickle.loads(pickle.dumps(obj, protocol)) | ||
| return result | ||
|
|
||
| return fn | ||
|
|
||
|
|
||
| @pytest.fixture( | ||
| params=[_roundtrip_pickle(), _roundtrip_pickle(4), _roundtrip_pickle(5)], | ||
| ids=["pickle-None", "pickle-4", "pickle-5"], | ||
| ) | ||
| def roundtrip(request: pytest.FixtureRequest) -> Identity: | ||
| fn: Identity = request.param | ||
| return fn | ||
|
|
||
|
|
||
| @namespaces | ||
| @time_units | ||
| def test_serde_datetime_dtype( | ||
| namespace: DTypes, time_unit: TimeUnit, roundtrip: Identity | ||
| ) -> None: | ||
| dtype = namespace.Datetime(time_unit) | ||
| result = roundtrip(dtype) | ||
| assert result == dtype | ||
|
|
||
|
|
||
| @namespaces | ||
| @time_units | ||
| def test_serde_duration_dtype( | ||
| namespace: DTypes, time_unit: TimeUnit, roundtrip: Identity | ||
| ) -> None: | ||
| dtype = namespace.Duration(time_unit) | ||
| result = roundtrip(dtype) | ||
| assert result == dtype | ||
|
|
||
|
|
||
| def test_serde_doubly_nested_struct_dtype(roundtrip: Identity) -> None: | ||
| dtype = nw.Struct([nw.Field("a", nw.List(nw.String))]) | ||
| result = roundtrip(dtype) | ||
| assert result == dtype | ||
|
|
||
|
|
||
| def test_serde_doubly_nested_array_dtype(roundtrip: Identity) -> None: | ||
| dtype = nw.Array(nw.Array(nw.Int32(), 2), 3) | ||
| result = roundtrip(dtype) | ||
| assert result == dtype | ||
|
|
||
|
|
||
| def test_serde_dtype_class(roundtrip: Identity) -> None: | ||
| dtype_class = nw.Datetime | ||
| result = roundtrip(dtype_class) | ||
| assert result == dtype_class | ||
| assert isinstance(result, type) | ||
|
|
||
|
|
||
| def test_serde_enum_dtype(roundtrip: Identity) -> None: | ||
| dtype = nw.Enum(["a", "b"]) | ||
| result = roundtrip(dtype) | ||
| assert result == dtype | ||
| assert isinstance(result, DType) | ||
|
|
||
|
|
||
| def test_serde_enum_v1_dtype(roundtrip: Identity) -> None: | ||
| dtype = nw_v1.Enum() | ||
| result = roundtrip(dtype) | ||
| assert result == dtype | ||
| assert isinstance(result, nw_v1.Enum) | ||
| tp = type(result) | ||
| with pytest.raises(TypeError): | ||
| tp(["a", "b"]) # type: ignore[call-arg] | ||
|
|
||
|
|
||
| def test_serde_enum_deferred(roundtrip: Identity) -> None: | ||
| pytest.importorskip("polars") | ||
| import polars as pl | ||
|
|
||
| categories = tuple(string.printable) | ||
| dtype_pl = pl.Enum(categories) | ||
| series_pl = pl.Series(categories).cast(dtype_pl).extend_constant(categories[5], 1000) | ||
| series_nw = nw.from_native(series_pl, series_only=True) | ||
| dtype_nw = series_nw.dtype | ||
| assert isinstance(dtype_nw, nw.Enum) | ||
| result = roundtrip(dtype_nw) | ||
| assert isinstance(result, nw.Enum) | ||
| assert result == dtype_nw | ||
| assert result == series_nw.dtype | ||
| assert dtype_nw == roundtrip(result) | ||
| assert ( | ||
| type(result)(dtype_pl.categories).categories | ||
| == roundtrip(result).categories | ||
| == categories | ||
| == result.categories | ||
| == roundtrip(dtype_nw).categories | ||
| ) | ||
|
|
||
|
|
||
| def test_serde_non_nested_dtypes( | ||
| non_nested_type: type[NonNestedDType], roundtrip: Identity | ||
| ) -> None: | ||
| dtype = non_nested_type() | ||
| result = roundtrip(dtype) | ||
| assert isinstance(result, DType) | ||
| assert isinstance(result, non_nested_type) | ||
| assert result == non_nested_type() | ||
| assert result == non_nested_type | ||
|
|
||
|
|
||
| def test_serde_nested_dtypes( | ||
| nested_dtype: NestedOrEnumDType, roundtrip: Identity | ||
| ) -> None: | ||
| result = roundtrip(nested_dtype) | ||
| assert isinstance(result, DType) | ||
| assert isinstance(result, nested_dtype.__class__) | ||
| assert result == nested_dtype | ||
| assert result == nested_dtype.base_type() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea I had here is we can pretty easily extend this by adding more functions that can roundtrip.
pickleis all that I've used, but the protocols which are being called can be used elsewhereUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Barely on topic, but noticable from the example I used
@FBruzzesi nested
DTypes are where the metaclass repr from polars would be nice