question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add attrs support

See original GitHub issue

It 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:closed
  • Created 3 years ago
  • Reactions:12
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
blthayercommented, Dec 2, 2021

Hello! Any update on this effort? Having native support for attrs classes in orjson would be very useful for me.

0reactions
ijlcommented, Jun 16, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found