1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
from __future__ import annotations
import datetime
import logging
from dataclasses import dataclass
from decimal import Decimal
from typing import Optional
import pytest
from dataclass_wizard import JSONWizard, DumpMeta
from dataclass_wizard.errors import ParseError
from ..conftest import *
log = logging.getLogger(__name__)
@dataclass
class B:
date_field: datetime.datetime | None
@dataclass
class C:
...
@dataclass
class D:
...
@dataclass
class DummyClass:
...
@pytest.mark.parametrize(
'input,expectation',
[
# Wrong type: `my_field1` is passed in a float (not in valid Union types)
({'my_field1': 3.1, 'my_field2': [], 'my_field3': (3,)}, pytest.raises(ParseError)),
# Wrong type: `my_field3` is passed a float type
({'my_field1': 3, 'my_field2': [], 'my_field3': 2.1}, pytest.raises(ParseError)),
# Wrong type: `my_field3` is passed a list type
({'my_field1': 3, 'my_field2': [], 'my_field3': [1]}, pytest.raises(ParseError)),
# Wrong type: `my_field3` is passed in a tuple of float (invalid Union type)
({'my_field1': 3, 'my_field2': [], 'my_field3': (1.0,)}, pytest.raises(ParseError)),
# OK: `my_field3` is passed in a tuple of int (one of the valid Union types)
({'my_field1': 3, 'my_field2': [], 'my_field3': (1,)}, does_not_raise()),
# Wrong number of elements for `my_field3`: expected only one
({'my_field1': 3, 'my_field2': [], 'my_field3': (1, 2)}, pytest.raises(ParseError)),
# Type checks for all fields
({'my_field1': 'string',
'my_field2': [{'date_field': None}],
'my_field3': ('hello world',)}, does_not_raise()),
]
)
def test_load_with_future_annotation_v1(input, expectation):
"""
Test case using the latest Python 3.10 features, such as PEP 604- style
annotations.
Ref: https://www.python.org/dev/peps/pep-0604/
"""
@dataclass
class A(JSONWizard):
my_field1: bool | str | int
my_field2: list[B]
my_field3: int | tuple[str | int] | bool
with expectation:
result = A.from_dict(input)
log.debug('Parsed object: %r', result)
@pytest.mark.parametrize(
'input,expectation',
[
# Wrong type: `my_field2` is passed in a float (expected str, int, or None)
({'my_field1': datetime.date.min, 'my_field2': 1.23, 'my_field3': {'key': [None]}},
pytest.raises(ParseError)),
# Type checks
({'my_field1': datetime.date.max, 'my_field2': None, 'my_field3': {'key': []}}, does_not_raise()),
# ParseError: expected list of B, C, D, or None; passed in a list of string instead.
({'my_field1': Decimal('3.1'), 'my_field2': 7, 'my_field3': {'key': ['hello']}},
pytest.raises(ParseError)),
# ParseError: expected list of B, C, D, or None; passed in a list of DummyClass instead.
({'my_field1': Decimal('3.1'), 'my_field2': 7, 'my_field3': {'key': [DummyClass()]}},
pytest.raises(ParseError)),
# Type checks
({'my_field1': Decimal('3.1'), 'my_field2': 7, 'my_field3': {'key': [None]}},
does_not_raise()),
# TODO enable once dataclasses are fully supported in Union types
pytest.param({'my_field1': Decimal('3.1'), 'my_field2': 7, 'my_field3': {'key': [C()]}},
does_not_raise(),
marks=pytest.mark.skip('Dataclasses in Union types are '
'not fully supported currently.')),
]
)
def test_load_with_future_annotation_v2(input, expectation):
"""
Test case using the latest Python 3.10 features, such as PEP 604- style
annotations.
Ref: https://www.python.org/dev/peps/pep-0604/
"""
@dataclass
class A(JSONWizard):
my_field1: Decimal | datetime.date | str
my_field2: str | Optional[int]
my_field3: dict[str | int, list[B | C | Optional[D]]]
with expectation:
result = A.from_dict(input)
log.debug('Parsed object: %r', result)
def test_dataclasses_in_union_types():
"""Dataclasses in Union types when manually specifying `tag` value."""
@dataclass
class Container(JSONWizard):
class _(JSONWizard.Meta):
key_transform_with_dump = 'SNAKE'
my_data: Data
my_dict: dict[str, A | B]
@dataclass
class Data:
my_str: str
my_list: list[C | D]
@dataclass
class A(JSONWizard):
class _(JSONWizard.Meta):
tag = 'AA'
val: str
@dataclass
class B(JSONWizard):
class _(JSONWizard.Meta):
tag = 'BB'
val: int
@dataclass
class C(JSONWizard):
class _(JSONWizard.Meta):
tag = '_C_'
my_field: int
@dataclass
class D(JSONWizard):
class _(JSONWizard.Meta):
tag = '_D_'
my_field: float
# Fix so the forward reference works
globals().update(locals())
c = Container.from_dict({
'my_data': {
'myStr': 'string',
'MyList': [{'__tag__': '_D_', 'my_field': 1.23},
{'__tag__': '_C_', 'my_field': 3.21}]
},
'my_dict': {
'key': {'__tag__': 'AA',
'val': '123'}
}
})
expected_obj = Container(
my_data=Data(my_str='string',
my_list=[D(my_field=1.23),
C(my_field=3)]),
my_dict={'key': A(val='123')}
)
expected_dict = {
"my_data": {"my_str": "string",
"my_list": [{"my_field": 1.23, "__tag__": "_D_"},
{"my_field": 3, "__tag__": "_C_"}]},
"my_dict": {"key": {"val": "123", "__tag__": "AA"}}
}
assert c == expected_obj
assert c.to_dict() == expected_dict
def test_dataclasses_in_union_types_with_auto_assign_tags():
"""
Dataclasses in Union types with auto-assign tags, and a custom tag field.
"""
@dataclass
class Container(JSONWizard):
class _(JSONWizard.Meta):
key_transform_with_dump = 'SNAKE'
tag_key = 'type'
auto_assign_tags = True
my_data: Data
my_dict: dict[str, A | B]
@dataclass
class Data:
my_str: str
my_list: list[C | D | E]
@dataclass
class A:
val: str
@dataclass
class B:
val: int
@dataclass
class C:
my_field: int
@dataclass
class D:
my_field: float
@dataclass
class E:
...
# This is to coverage a case where we have a Meta config for a class,
# but we do not define a tag in the Meta config.
DumpMeta(key_transform='SNAKE').bind_to(D)
# Bind a custom tag to class E, so we can cover a case when
# `auto_assign_tags` is true, but we are still able to specify a
# custom tag for a class.
DumpMeta(tag='!E').bind_to(E)
# Fix so the forward reference works
globals().update(locals())
c = Container.from_dict({
'my_data': {
'myStr': 'string',
'MyList': [{'type': 'D', 'my_field': 1.23},
{'type': 'C', 'my_field': 3.21},
{'type': '!E'}]
},
'my_dict': {
'key': {'type': 'A',
'val': '123'}
}
})
expected_obj = Container(
my_data=Data(my_str='string',
my_list=[D(my_field=1.23),
C(my_field=3),
E()]),
my_dict={'key': A(val='123')}
)
expected_dict = {
"my_data": {"my_str": "string",
"my_list": [{"my_field": 1.23, "type": "D"},
{"my_field": 3, "type": "C"},
{'type': '!E'}]},
"my_dict": {"key": {"val": "123", "type": "A"}}
}
assert c == expected_obj
assert c.to_dict() == expected_dict
|