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 281 282 283 284 285 286 287 288 289 290 291 292
|
from __future__ import annotations
from dataclasses import dataclass
from typing import Dict
import msgpack
import orjson
import pytest
from mashumaro import DataClassDictMixin
from mashumaro.config import ADD_DIALECT_SUPPORT, BaseConfig
from mashumaro.dialect import Dialect
from mashumaro.exceptions import UnresolvedTypeReferenceError
from mashumaro.mixins.msgpack import DataClassMessagePackMixin
from mashumaro.mixins.orjson import DataClassORJSONMixin
from .conftest import add_unpack_method
@dataclass
class A(DataClassDictMixin):
x: B
@dataclass
class B(DataClassDictMixin):
x: int
@dataclass
class Base(DataClassDictMixin):
pass
@dataclass
class A1(Base):
a: B1
@dataclass
class A2(Base):
a: B2
@dataclass
class A3(Base):
a: B1
x: int
class Config(BaseConfig):
code_generation_options = [ADD_DIALECT_SUPPORT]
@dataclass
class B1(Base):
b: int
@dataclass
class AMessagePack(DataClassMessagePackMixin):
x: BMessagePack
@dataclass
class BMessagePack(DataClassMessagePackMixin):
x: int
@dataclass
class BaseMessagePack(DataClassMessagePackMixin):
pass
@dataclass
class A1MessagePack(BaseMessagePack):
a: B1MessagePack
@dataclass
class A2MessagePack(BaseMessagePack):
a: B2
@dataclass
class A3MessagePack(BaseMessagePack):
a: B1MessagePack
x: int
class Config(BaseConfig):
code_generation_options = [ADD_DIALECT_SUPPORT]
@dataclass
class B1MessagePack(BaseMessagePack):
b: int
@dataclass
class A3ORJSON(DataClassORJSONMixin):
a: B1ORJSON
x: int
class Config(BaseConfig):
code_generation_options = [ADD_DIALECT_SUPPORT]
@dataclass
class B1ORJSON(DataClassORJSONMixin):
b: int
def test_postponed_annotation_evaluation():
obj = A(x=B(x=1))
assert obj.to_dict() == {"x": {"x": 1}}
assert A.from_dict({"x": {"x": 1}}) == obj
def test_unresolved_type_with_allowed_postponed_annotation_evaluation():
@dataclass
class DataClass(DataClassDictMixin):
x: X
with pytest.raises(UnresolvedTypeReferenceError):
DataClass.from_dict({})
with pytest.raises(UnresolvedTypeReferenceError):
DataClass(x=1).to_dict()
def test_unresolved_type_with_disallowed_postponed_annotation_evaluation():
with pytest.raises(UnresolvedTypeReferenceError):
@dataclass
class DataClass(DataClassDictMixin):
x: X
class Config(BaseConfig):
allow_postponed_evaluation = False
with add_unpack_method:
with pytest.raises(UnresolvedTypeReferenceError):
@dataclass
class DataClass(DataClassDictMixin):
x: X
class Config(BaseConfig):
allow_postponed_evaluation = False
def test_postponed_annotation_evaluation_with_parent():
obj = A1(B1(1))
assert A1.from_dict({"a": {"b": 1}}) == obj
assert obj.to_dict() == {"a": {"b": 1}}
def test_postponed_annotation_evaluation_with_parent_and_no_reference():
with pytest.raises(UnresolvedTypeReferenceError):
A2.from_dict({"a": {"b": 1}})
with pytest.raises(UnresolvedTypeReferenceError):
A2(None).to_dict()
def test_postponed_annotation_evaluation_with_parent_and_dialect():
class MyDialect(Dialect):
serialization_strategy = {
int: {
"serialize": lambda i: str(int(i * 1000)),
"deserialize": lambda s: int(int(s) / 1000),
}
}
obj = A3(B1(1), 2)
assert A3.from_dict({"a": {"b": 1}, "x": 2}) == obj
assert A3.from_dict({"a": {"b": 1}, "x": "2000"}, dialect=MyDialect) == obj
assert obj.to_dict() == {"a": {"b": 1}, "x": 2}
assert obj.to_dict(dialect=MyDialect) == {"a": {"b": 1}, "x": "2000"}
def test_postponed_annotation_evaluation_msgpack():
obj = AMessagePack(x=BMessagePack(x=1))
assert obj.to_dict() == {"x": {"x": 1}}
assert AMessagePack.from_dict({"x": {"x": 1}}) == obj
dump = msgpack.dumps({"x": {"x": 1}})
assert obj.to_msgpack() == dump
assert AMessagePack.from_msgpack(dump) == obj
def test_unresolved_type_with_allowed_postponed_annotation_evaluation_msgpack():
@dataclass
class DataClass(DataClassMessagePackMixin):
x: X
with pytest.raises(UnresolvedTypeReferenceError):
DataClass.from_msgpack(b"")
with pytest.raises(UnresolvedTypeReferenceError):
DataClass(x=1).to_msgpack()
def test_postponed_annotation_evaluation_with_parent_msgpack():
obj = A1MessagePack(B1MessagePack(1))
dump = msgpack.dumps({"a": {"b": 1}})
assert A1MessagePack.from_msgpack(dump) == obj
assert obj.to_msgpack() == dump
def test_postponed_annotation_evaluation_with_parent_and_no_reference_msgpack():
with pytest.raises(UnresolvedTypeReferenceError):
A2MessagePack.from_msgpack(b"")
with pytest.raises(UnresolvedTypeReferenceError):
A2MessagePack(None).to_msgpack()
def test_postponed_annotation_evaluation_with_parent_and_dialect_msgpack():
class MyDialect(Dialect):
serialization_strategy = {
int: {
"serialize": lambda i: str(int(i * 1000)),
"deserialize": lambda s: int(int(s) / 1000),
}
}
obj = A3MessagePack(B1MessagePack(1), 2)
dump = msgpack.dumps({"a": {"b": 1}, "x": 2})
dump_dialect = msgpack.dumps({"a": {"b": 1}, "x": "2000"})
assert A3MessagePack.from_msgpack(dump) == obj
assert A3MessagePack.from_msgpack(dump_dialect, dialect=MyDialect) == obj
assert obj.to_msgpack() == dump
assert obj.to_msgpack(dialect=MyDialect) == dump_dialect
def test_postponed_msgpack_with_custom_encoder_and_decoder():
def decoder(data) -> Dict[str, bytes]:
def modify(d):
result = {}
for k, v in d.items():
if isinstance(v, dict):
result[k] = modify(v)
else:
result[k] = v // 1000
return result
return modify(msgpack.loads(data))
def encoder(data: Dict[str, bytes]) -> bytes:
def modify(d):
result = {}
for k, v in d.items():
if isinstance(v, dict):
result[k] = modify(v)
else:
result[k] = v * 1000
return result
return msgpack.dumps(modify(data))
instance = A3MessagePack(B1MessagePack(123), 456)
dumped = msgpack.packb({"a": {"b": 123000}, "x": 456000})
assert instance.to_msgpack(encoder=encoder) == dumped
assert A3MessagePack.from_msgpack(dumped, decoder=decoder) == instance
def test_postponed_orjson_with_custom_encoder_and_decoder():
def decoder(data) -> Dict[str, bytes]:
def modify(d):
result = {}
for k, v in d.items():
if isinstance(v, dict):
result[k] = modify(v)
else:
result[k] = v // 1000
return result
return modify(orjson.loads(data))
def encoder(data: Dict[str, bytes], **_) -> bytes:
def modify(d):
result = {}
for k, v in d.items():
if isinstance(v, dict):
result[k] = modify(v)
else:
result[k] = v * 1000
return result
return orjson.dumps(modify(data))
instance = A3ORJSON(B1ORJSON(123), 456)
dumped = orjson.dumps({"a": {"b": 123000}, "x": 456000})
assert instance.to_jsonb(encoder=encoder) == dumped
assert A3ORJSON.from_json(dumped, decoder=decoder) == instance
|