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
{{ message }}
Repository navigation
Typed dictionaries not working with preconf.orjson #241
Dictionaries with type annotations don't appear to respect those type annotations. Worse, this only happens on unstructure, so structure fails. This isn't obvious when using the default converter, but when using the orjson converter, it becomes an issue
{'d': {"b'hello'": 'cW-iRWB'}}
Traceback (most recent call last):
File "attrs_tests.py", line 15, in <module>
converter.structure(data, Test)
File "<python-path>/python3.7/site-packages/cattr/converters.py", line 300, in structure
return self._structure_func.dispatch(cl)(obj, cl)
File "<cattrs generated structure __main__.Test>", line 3, in structure_Test
File "", line 2, in structure_mapping
File "", line 2, in <dictcomp>
File "<python-path>/python3.7/site-packages/cattr/preconf/orjson.py", line 24, in <lambda>
converter.register_structure_hook(bytes, lambda v, _: b85decode(v))
File "/usr/lib/python3.7/base64.py", line 464, in b85decode
% (i + j)) from None
ValueError: bad base85 character at position 1
The ValueError is because the keys of the dictionary are not base85 encoded the same way the values are -- the unstructure command seems to lose the types of the keys, while the structure command does not
I may have done something wrong when installing from git? I did pip install --upgrade git+https://github.com/python-attrs/cattrs.git#egg=cattrs, and now when I try to run it, I get
<python-path>/python3.7/site-packages/cattr/preconf/orjson.py in <module>
5 from typing import Any, Type, TypeVar
6
----> 7 from orjson import dumps, loads
8
9 from cattrs._compat import Set, is_mapping
ModuleNotFoundError: No module named 'orjson'
No, you're good. In the new versions the preconf converters depend on their serialization libraries, and you can do converter.dumps(myClass) instead of orjson.dumps(converter.unstructure(myClass)). You just need to pip install orjson.
Activity
Tinche commentedon Mar 30, 2022
Hm, good catch. I've added a test for this, and it would appear the bson and tomlkit converters are also affected.
I'll have a fix in the next couple of days.
Tinche commentedon Mar 30, 2022
The fix is up on the
mainbranch, want to give it a test and see if it solves your issue?orinatic commentedon Mar 31, 2022
I may have done something wrong when installing from git? I did
pip install --upgrade git+https://github.com/python-attrs/cattrs.git#egg=cattrs, and now when I try to run it, I getTinche commentedon Mar 31, 2022
No, you're good. In the new versions the preconf converters depend on their serialization libraries, and you can do
converter.dumps(myClass)instead oforjson.dumps(converter.unstructure(myClass)). You just need to pip install orjson.orinatic commentedon Apr 1, 2022
That appears to have fixed it! Thank you!
Tinche commentedon Apr 2, 2022
Great, since the new release is going out very soon I'll close this!
chore(deps): Update cattrs requirement from <1.11,>=1.8 to >=1.8,<22.…