-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support generic dataclass (#525)
- Loading branch information
Showing
3 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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,83 @@ | ||
from dataclasses import dataclass | ||
from datetime import datetime | ||
from typing import Generic, TypeVar | ||
|
||
import pytest | ||
|
||
from dataclasses_json import dataclass_json | ||
|
||
S = TypeVar("S") | ||
T = TypeVar("T") | ||
|
||
|
||
@dataclass_json | ||
@dataclass | ||
class Bar: | ||
value: int | ||
|
||
|
||
@dataclass_json | ||
@dataclass | ||
class Foo(Generic[T]): | ||
bar: T | ||
|
||
|
||
@dataclass_json | ||
@dataclass | ||
class Baz(Generic[T]): | ||
foo: Foo[T] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"instance_of_t, decodes_successfully", | ||
[ | ||
pytest.param(1, True, id="literal"), | ||
pytest.param([1], True, id="literal_list"), | ||
pytest.param({"a": 1}, True, id="map_of_literal"), | ||
pytest.param(datetime(2021, 1, 1), False, id="extended_type"), | ||
pytest.param(Bar(1), False, id="object"), | ||
] | ||
) | ||
def test_dataclass_with_generic_dataclass_field(instance_of_t, decodes_successfully): | ||
foo = Foo(bar=instance_of_t) | ||
baz = Baz(foo=foo) | ||
decoded = Baz[type(instance_of_t)].from_json(baz.to_json()) | ||
assert decoded.foo == Foo.from_json(foo.to_json()) | ||
if decodes_successfully: | ||
assert decoded == baz | ||
else: | ||
assert decoded != baz | ||
|
||
|
||
@dataclass_json | ||
@dataclass | ||
class Foo2(Generic[T, S]): | ||
bar1: T | ||
bar2: S | ||
|
||
|
||
@dataclass_json | ||
@dataclass | ||
class Baz2(Generic[T, S]): | ||
foo2: Foo2[T, S] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"instance_of_t, decodes_successfully", | ||
[ | ||
pytest.param(1, True, id="literal"), | ||
pytest.param([1], True, id="literal_list"), | ||
pytest.param({"a": 1}, True, id="map_of_literal"), | ||
pytest.param(datetime(2021, 1, 1), False, id="extended_type"), | ||
pytest.param(Bar(1), False, id="object"), | ||
] | ||
) | ||
def test_dataclass_with_multiple_generic_dataclass_fields(instance_of_t, decodes_successfully): | ||
foo2 = Foo2(bar1=instance_of_t, bar2=instance_of_t) | ||
baz = Baz2(foo2=foo2) | ||
decoded = Baz2[type(instance_of_t), type(instance_of_t)].from_json(baz.to_json()) | ||
assert decoded.foo2 == Foo2.from_json(foo2.to_json()) | ||
if decodes_successfully: | ||
assert decoded == baz | ||
else: | ||
assert decoded != baz |
de0d230
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.
Coverage Report
de0d230
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.
Coverage Report
de0d230
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.
Coverage Report
de0d230
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.
Coverage Report
de0d230
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.
Coverage Report
de0d230
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.
Coverage Report