Add attrs support
See original GitHub issueIt would be very nice to have attrs support, alongside support for dataclasses.
Attrs works quite similarly to dataclasses (dataclasses drew a lot of inspiration from attrs).
The major difference for serialization with orjson is, that the list of fields can be found in the __attrs_attrs__
attribute. As POC, I wrote a small patch:
--- a/src/typeref.rs
+++ b/src/typeref.rs
@@ -84,7 +84,7 @@
DST_STR = PyUnicode_InternFromString("dst\0".as_ptr() as *const c_char);
DICT_STR = PyUnicode_InternFromString("__dict__\0".as_ptr() as *const c_char);
DATACLASS_FIELDS_STR =
- PyUnicode_InternFromString("__dataclass_fields__\0".as_ptr() as *const c_char);
+ PyUnicode_InternFromString("__attrs_attrs__\0".as_ptr() as *const c_char);
ARRAY_STRUCT_STR =
pyo3::ffi::PyUnicode_InternFromString("__array_struct__\0".as_ptr() as *const c_char);
VALUE_STR = pyo3::ffi::PyUnicode_InternFromString("value\0".as_ptr() as *const c_char);
I compiled orjson and tested it:
>>> from attr import dataclass
>>> import orjson
>>>
>>> @dataclass
... class Data:
... x: int
... y: int
...
>>> orjson.dumps(Data(1, 2))
b'{"x":1,"y":2}'
Issue Analytics
- State:
- Created 3 years ago
- Reactions:12
- Comments:5 (2 by maintainers)
Top Results From Across the Web
attrs by Example - attrs 22.2.0 documentation
If you create an attribute with init=False , the kw_only argument is ignored. Keyword-only attributes allow subclasses to add attributes without default values, ......
Read more >attrs - PyPI
attrs is the Python package that will bring back the joy of writing classes by relieving you from the drudgery of implementing object...
Read more >attr() - CSS: Cascading Style Sheets - MDN Web Docs
The attr() CSS function is used to retrieve the value of an attribute of the selected element and use it in the stylesheet....
Read more >Pycharm plugin for attrs? - python - Stack Overflow
3 Answers 3 · an attrs PR to add PEP 484 stubs, which are recognized by PyCharm (but does not add support for...
Read more >3. Add attributes | Jira Service Management Data Center and ...
You'll see the default attributes, and will be able to add new ones by adding them to the list. Each attribute needs the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hello! Any update on this effort? Having native support for
attrs
classes inorjson
would be very useful for me.The test for
dataclasses._FIELD
uses a pointer so that’d be the potential reason for two cases. If you or someone wants to look into this more, great.