Skip to content

Typed dictionaries not working with preconf.orjson #241

@orinatic

Description

@orinatic
  • cattrs version: 1.10.0
  • Python version: Python 3.7.8
  • Operating System: Ubuntu 16.04

Description

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

What I Did

from typing import Dict
import attr
from cattr.preconf.orjson import make_converter

@attr.s(auto_attribs=True)
class Test:
    d: Dict[bytes, bytes]

t = Test({b'hello': b'world'})
converter = make_converter()
data = converter.unstructure(t)
print(data)
converter.structure(data, Test)

If you run this, you will get

{'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

Thanks for looking into this!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions