-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for enhanced builtin instantiation (#375)
- Loading branch information
Showing
5 changed files
with
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from dataclasses import dataclass | ||
from decimal import Decimal | ||
from typing import Optional | ||
|
||
from pytest import mark, param | ||
|
||
|
||
from dataclasses_json import DataClassJsonMixin | ||
|
||
|
||
@dataclass(frozen=True) | ||
class DataClassWithBuiltins(DataClassJsonMixin): | ||
actually_a_str: str | ||
actually_an_int: int | ||
actually_a_float: float | ||
|
||
|
||
@mark.parametrize( | ||
"model_dict, expected_model", | ||
[ | ||
param( | ||
{"actually_a_str": "str", "actually_an_int": 42, "actually_a_float": 42.1}, | ||
DataClassWithBuiltins(actually_a_str="str", actually_an_int=42, actually_a_float=42.1), | ||
id="Happy case" | ||
), | ||
param( | ||
{"actually_a_str": "str", "actually_an_int": Decimal("42.1"), "actually_a_float": Decimal("42.1")}, | ||
DataClassWithBuiltins(actually_a_str="str", actually_an_int=42, actually_a_float=42.1), | ||
id="Decimal as int and float" | ||
), | ||
] | ||
) | ||
def test__DataClassWithBuiltins__from_dict(model_dict, expected_model): | ||
assert DataClassWithBuiltins.from_dict(model_dict) == expected_model |
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,32 @@ | ||
from dataclasses import dataclass | ||
|
||
from dataclasses_json import DataClassJsonMixin | ||
|
||
|
||
class MyStr(str): | ||
|
||
def is_even_length(self) -> bool: | ||
return len(self) % 2 == 0 | ||
|
||
|
||
@dataclass(frozen=True) | ||
class DataClassWithStrSubclass(DataClassJsonMixin): | ||
any_str: str | ||
my_str: MyStr | ||
|
||
|
||
class TestDataClassWithStrSubclass: | ||
|
||
def test_encode__no_instantiation_required(self): | ||
model_dict = {"any_str": "str", "my_str": MyStr("str")} | ||
expected = DataClassWithStrSubclass(any_str="str", my_str=MyStr("str")) | ||
actual = DataClassWithStrSubclass.from_dict(model_dict) | ||
assert expected == actual | ||
assert model_dict["my_str"] is actual.my_str | ||
|
||
def test_encode__subclass_str_instantiated(self): | ||
model_dict = {"any_str": "str", "my_str": "str"} | ||
expected = DataClassWithStrSubclass(any_str="str", my_str=MyStr("str")) | ||
actual = DataClassWithStrSubclass.from_dict(model_dict) | ||
assert expected == actual | ||
assert model_dict["my_str"] is not actual.my_str |