You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+109-30
Original file line number
Diff line number
Diff line change
@@ -54,12 +54,14 @@ assert user == User(name='John', age=30, is_active=True)
54
54
Dacite supports following features:
55
55
56
56
- nested structures
57
-
- (basic) types checking
57
+
- (basic) type checking
58
58
- optional fields (i.e. `typing.Optional`)
59
59
- unions
60
+
- generics
60
61
- forward references
61
62
- collections
62
63
- custom type hooks
64
+
- case conversion
63
65
64
66
## Motivation
65
67
@@ -109,6 +111,7 @@ Configuration is a (data) class with following fields:
109
111
-`check_types`
110
112
-`strict`
111
113
-`strict_unions_match`
114
+
-`convert_key`
112
115
113
116
The examples below show all features of `from_dict` function and usage
114
117
of all `Config` parameters.
@@ -233,6 +236,71 @@ result = from_dict(data_class=B, data=data)
233
236
assert result == B(a_list=[A(x='test1', y=1), A(x='test2', y=2)])
234
237
```
235
238
239
+
### Generics
240
+
241
+
Dacite supports generics: (multi-)generic dataclasses, but also dataclasses that inherit from a generic dataclass, or dataclasses that have a generic dataclass field.
242
+
243
+
```python
244
+
T = TypeVar('T')
245
+
U = TypeVar('U')
246
+
247
+
@dataclass
248
+
classX:
249
+
a: str
250
+
251
+
252
+
@dataclass
253
+
classA(Generic[T, U]):
254
+
x: T
255
+
y: list[U]
256
+
257
+
data = {
258
+
'x': {
259
+
'a': 'foo',
260
+
},
261
+
'y': [1, 2, 3]
262
+
}
263
+
264
+
result = from_dict(data_class=A[X, int], data=data)
265
+
266
+
assert result == A(x=X(a='foo'), y=[1,2,3])
267
+
268
+
269
+
@dataclass
270
+
classB(A[X, int]):
271
+
z: str
272
+
273
+
data = {
274
+
'x': {
275
+
'a': 'foo',
276
+
},
277
+
'y': [1, 2, 3],
278
+
'z': 'bar'
279
+
}
280
+
281
+
result = from_dict(data_class=B, data=data)
282
+
283
+
assert result == B(x=X(a='foo'), y=[1,2,3], z='bar')
284
+
285
+
286
+
@dataclass
287
+
classC:
288
+
z: A[X, int]
289
+
290
+
data = {
291
+
'z': {
292
+
'x': {
293
+
'a': 'foo',
294
+
},
295
+
'y': [1, 2, 3],
296
+
}
297
+
}
298
+
299
+
result = from_dict(data_class=C, data=data)
300
+
301
+
assert result == C(z=A(x=X(a='foo'), y=[1,2,3]))
302
+
```
303
+
236
304
### Type hooks
237
305
238
306
You can use `Config.type_hooks` argument if you want to transform the input
0 commit comments