File tree Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change
1
+ [[entries ]]
2
+ id = " 34f4cbf8-4bd4-4b6d-837f-6bf4b2006803"
3
+ type = " feature"
4
+ description = " Add `DeserializeAs` setting"
5
+ author = " @NiklasRosenstein"
Original file line number Diff line number Diff line change @@ -326,7 +326,38 @@ class SerializeDefaults(BooleanSetting):
326
326
to how the name of the setting appears assertive of the fact that the instance indicates the setting is enabled."""
327
327
328
328
329
- # NOTE(NiklasRosenstein): For Python 3.6, metadata passed into Annotated[...] must be hashable.
329
+ @dataclasses .dataclass
330
+ class DeserializeAs (Setting ):
331
+ """Indicates that a field should be deserialized as the given type instead of the type of the field. This is
332
+ typically used when a field should be typed as an abstract class or interface, but during deserialization of the
333
+ field, a concrete type should be used instead.
334
+
335
+ Example:
336
+
337
+ ```py
338
+ import typing
339
+ from dataclasses import dataclass
340
+ from databind.core.settings import DeserializeAs
341
+
342
+ @dataclass
343
+ class A:
344
+ pass
345
+
346
+ @dataclass
347
+ class B(A):
348
+ pass
349
+
350
+ @dataclass
351
+ class MyClass:
352
+ my_field: typing.Annotated[A, DeserializeAs(B)]
353
+ ```
354
+
355
+ Here, although `MyClass.my_field` is annotated as `A`, when a payload is deserialized into an instance of
356
+ `MyClass`, the value for `my_field` will be deserialized as an instance of `B` instead of `A`.
357
+ """
358
+
359
+ type : t .Type [t .Any ]
360
+ priority : Priority = Priority .NORMAL
330
361
331
362
332
363
@dataclasses .dataclass
You can’t perform that action at this time.
0 commit comments