Skip to content
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

Generic dataclasses #152

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import decimal
import enum
import uuid
from typing import Annotated, Any, Dict, FrozenSet, List, Set, Tuple
from typing import Annotated, Any, Dict, FrozenSet, Generic, List, Set, Tuple, TypeVar

import pytest

Expand Down Expand Up @@ -580,3 +580,27 @@ class RootContainer:
int_container: IntContainer = dataclasses.field(default_factory=IntContainer)

assert mr.load(RootContainer, {}) == RootContainer()


def test_generic() -> None:
TItem = TypeVar("TItem")

@dataclasses.dataclass(frozen=True, kw_only=True)
class GenericContainer(Generic[TItem]):
items: list[TItem]

container = GenericContainer[int](items=[1, 2, 3])
dumped = mr.dump(container)

assert dumped == {"items": [1, 2, 3]}
assert mr.load(GenericContainer[int], dumped) == container


def test_generic_default() -> None:
TItem = TypeVar("TItem")

@dataclasses.dataclass(frozen=True, kw_only=True)
class GenericContainer(Generic[TItem]):
items: list[TItem] = dataclasses.field(default_factory=list)

assert mr.load(GenericContainer[int], {}) == GenericContainer[int](items=[])
Loading