-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attrs compatibility example + doc improvement
- Loading branch information
Showing
8 changed files
with
83 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Attrs compatibility | ||
|
||
```python | ||
{!examples/attrs_compatibility.py!} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from dataclasses import field, fields, make_dataclass | ||
from functools import lru_cache | ||
from typing import Optional | ||
|
||
import attr | ||
|
||
from apischema import deserialize, serialize, settings | ||
from apischema.conversions import Conversions, Deserialization, Serialization | ||
|
||
|
||
@lru_cache() | ||
def attrs_to_dataclass(cls: type) -> type: | ||
assert hasattr(cls, "__attrs_attrs__") | ||
fields_without_default = [ | ||
(a.name, a.type) for a in cls.__attrs_attrs__ if a.default == attr.NOTHING | ||
] | ||
fields_with_default = [ | ||
(a.name, a.type, field(default=a.default)) | ||
for a in cls.__attrs_attrs__ | ||
if a.default != attr.NOTHING | ||
] | ||
return make_dataclass(cls.__name__, fields_without_default + fields_with_default) | ||
|
||
|
||
prev_deserialization = settings.deserialization() | ||
prev_serialization = settings.serialization() | ||
|
||
|
||
@settings.deserialization | ||
def deserialization( | ||
cls: type, conversions: Optional[Conversions] | ||
) -> Optional[Deserialization]: | ||
result = prev_deserialization(cls, conversions) | ||
if result is not None: | ||
return result | ||
elif hasattr(cls, "__attrs_attrs__"): | ||
source = attrs_to_dataclass(cls) | ||
|
||
def converter(source_obj): | ||
return cls(**{f.name: getattr(source_obj, f.name) for f in fields(source)}) | ||
|
||
return {source: (converter, None)} | ||
else: | ||
return None | ||
|
||
|
||
@settings.serialization | ||
def serialization( | ||
cls: type, conversions: Optional[Conversions] | ||
) -> Optional[Serialization]: | ||
result = prev_serialization(cls, conversions) | ||
if result is not None: | ||
return result | ||
elif hasattr(cls, "__attrs_attrs__"): | ||
target = attrs_to_dataclass(cls) | ||
|
||
def converter(obj): | ||
return target(**{a.name: getattr(obj, a.name) for a in cls.__attrs_attrs__}) | ||
|
||
return target, (converter, None) | ||
else: | ||
return None | ||
|
||
|
||
@attr.s | ||
class Foo: | ||
bar: int = attr.ib() | ||
|
||
|
||
assert deserialize(Foo, {"bar": 0}) == Foo(0) | ||
assert serialize(Foo(0)) == {"bar": 0} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters