Skip to content

Typed dictionaries not working with preconf.orjson #241

Closed

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!

Activity

  1. Tinche commented on Mar 30, 2022

    @Tinche
    Member

    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.

  2. Tinche commented on Mar 30, 2022

    @Tinche
    Member

    The fix is up on the main branch, want to give it a test and see if it solves your issue?

  3. orinatic commented on Mar 31, 2022

    @orinatic
    Author

    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'
    
  4. Tinche commented on Mar 31, 2022

    @Tinche
    Member

    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.

  5. orinatic commented on Apr 1, 2022

    @orinatic
    Author

    That appears to have fixed it! Thank you!

  6. Tinche commented on Apr 2, 2022

    @Tinche
    Member

    Great, since the new release is going out very soon I'll close this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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