File: 2to3.md

package info (click to toggle)
python-mashumaro 3.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,408 kB
  • sloc: python: 19,981; sh: 16; makefile: 5
file content (217 lines) | stat: -rw-r--r-- 6,181 bytes parent folder | download | duplicates (2)
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
Migration from version 2 to version 3
--------------------------------------------------------------------------------

* [Moving serialization format mixins](#moving-serialization-format-mixins)
* [Removing `use_bytes` parameter](#removing-use_bytes-parameter)
* [Removing `use_enum` parameter](#removing-use_enum-parameter)
* [Removing `use_datetime` parameter](#removing-use_datetime-parameter)
* [Changing `from_json`, `from_msgpack`, `from_yaml` signature](#changing-from_json-from_msgpack-from_yaml-signature)
* [Changing `to_json`, `to_msgpack`, `to_yaml` signature](#changing-to_json-to_msgpack-to_yaml-signature)

### Moving serialization format mixins

You might need to alter your imports if you've used the following mixins:
* `DataClassJSONMixin`
* `DataClassMessagePackMixin`
* `DataClassYAMLMixin`

Tne new imports will look like this:

```python
from mashumaro.mixins.json import DataClassJSONMixin
from mashumaro.mixins.msgpack import DataClassMessagePackMixin
from mashumaro.mixins.yaml import DataClassYAMLMixin
```

### Removing `use_bytes` parameter

Parameter `use_bytes` was removed from `from_dict` / `to_dict` methods.
If you've used it to pass bytes or bytearray values as is, you can do the same
with [dialect](https://github.com/Fatal1ty/mashumaro#dialects) and
[pass_through](https://github.com/Fatal1ty/mashumaro/tree/master#passing-field-values-as-is)
features:

```python
from dataclasses import dataclass

from mashumaro import DataClassDictMixin, pass_through
from mashumaro.config import BaseConfig, ADD_DIALECT_SUPPORT
from mashumaro.dialect import Dialect

class BytesDialect(Dialect):
    serialization_strategy = {
        bytes: pass_through,
        bytearray: pass_through,
    }

@dataclass
class A(DataClassDictMixin):
    bytes: bytes
    bytearray: bytearray

    class Config(BaseConfig):
        code_generation_options = [ADD_DIALECT_SUPPORT]

obj = A(b"\x00", bytearray(b"\x00"))
dct = {"bytes": b"\x00", "bytearray": bytearray(b"\x00")}

assert A.from_dict(dct, dialect=BytesDialect) == obj
assert obj.to_dict(dialect=BytesDialect) == dct
```

### Removing `use_enum` parameter

Parameter `use_enum` was removed from `from_dict` / `to_dict` methods.
If you've used it to pass enum values as is, you can do the same
with [dialect](https://github.com/Fatal1ty/mashumaro#dialects) and
[pass_through](https://github.com/Fatal1ty/mashumaro/tree/master#passing-field-values-as-is)
features:

```python
from dataclasses import dataclass
from enum import Enum

from mashumaro import DataClassDictMixin, pass_through
from mashumaro.config import BaseConfig, ADD_DIALECT_SUPPORT
from mashumaro.dialect import Dialect

class MyEnum(Enum):
    a = 1
    b = 2

class EnumDialect(Dialect):
    serialization_strategy = {
        MyEnum: pass_through,
    }

@dataclass
class A(DataClassDictMixin):
    my_enum: MyEnum

    class Config(BaseConfig):
        code_generation_options = [ADD_DIALECT_SUPPORT]

obj = A(MyEnum.a)
dct = {"my_enum": MyEnum.a}

assert A.from_dict(dct, dialect=EnumDialect) == obj
assert obj.to_dict(dialect=EnumDialect) == dct
```

### Removing `use_datetime` parameter

Parameter `use_datetime` was removed from `from_dict` / `to_dict` methods.
If you've used it to pass datetime, date and time values as is, you can do
the same with [dialect](https://github.com/Fatal1ty/mashumaro#dialects) and
[pass_through](https://github.com/Fatal1ty/mashumaro/tree/master#passing-field-values-as-is)
features:

```python
from dataclasses import dataclass
from datetime import date, datetime, time

from mashumaro import DataClassDictMixin, pass_through
from mashumaro.config import BaseConfig, ADD_DIALECT_SUPPORT
from mashumaro.dialect import Dialect

class DatetimeDialect(Dialect):
    serialization_strategy = {
        date: pass_through,
        datetime: pass_through,
        time: pass_through,
    }

@dataclass
class A(DataClassDictMixin):
    datetime: datetime
    date: date
    time: time

    class Config(BaseConfig):
        code_generation_options = [ADD_DIALECT_SUPPORT]

obj = A(
    datetime=datetime(2022, 2, 9, 12, 0),
    date=date(2022, 2, 9),
    time=time(12, 0),
)
dct = {
    "datetime": datetime(2022, 2, 9, 12, 0),
    "date": date(2022, 2, 9),
    "time": time(12, 0),
}

assert A.from_dict(dct, dialect=DatetimeDialect) == obj
assert obj.to_dict(dialect=DatetimeDialect) == dct
```

### Changing `from_json`, `from_msgpack`, `from_yaml` signature

In version 2 methods `from_json`, `from_msgpack`, `from_yaml` had the following
signature:
```python
@classmethod
def from_*(  # where * is json, msgpack, yaml
    cls,
    data: EncodedData,
    decoder: Decoder = ...,
    dict_params: Mapping = ...,
    **decoder_kwargs,
)
```

In version 3 these methods have a slightly different signature:
```python
@classmethod
def from_*(  # where * is json, msgpack, yaml
    cls,
    data: EncodedData,
    decoder: Decoder = ...,
    **from_dict_kwargs,
)
```

As you can see, the `dict_params` positional argument was removed in order
to pass keyword arguments to underlying `from_dict` method. Decoder parameters
were removed because they can be easily passed to decoder using
a lambda function, a partial object or something else:

```python
A.from_json(
    data,
    decoder=lambda data: json.loads(data, parse_float=decimal.Decimal),
)
```

### Changing `to_json`, `to_msgpack`, `to_yaml` signature

In version 2 methods `to_json`, `to_msgpack`, `to_yaml` had the following
signature:
```python
def to_*(  # where * is json, msgpack, yaml
    self,
    encoder: Encoder = ...
    dict_params: Mapping = ...,
    **encoder_kwargs,
)
```

In version 3 these methods have a slightly different signature:
```python
def to_*(  # where * is json, msgpack, yaml
    self,
    encoder: Encoder = ...,
    **to_dict_kwargs,
)
```

As you can see, the `dict_params` positional argument was removed in order
to pass keyword arguments to underlying `to_dict` method. Encoder parameters
were removed because they can be easily passed to encoder using
a lambda function, a partial object or something else:

```python
dataclass_obj.to_json(
    encoder=lambda data: json.dumps(data, ensure_ascii=False),
)
```