Skip to content

Preconfigured bson converter errors when destructuring class with dictionary field that has a Literal key #629

Closed as duplicate

Description

@jrpat

Using cattrs 24.1.2 and attrs 25.1.0

Repro

from typing import Literal

import attrs
import cattrs.preconf.bson

ThingType = Literal["a", "b"]

@attrs.define
class UserDoc:
    things: dict[ThingType, bool | None]


raw_user = {"things": {"a": True}}

converter = cattrs.preconf.bson.make_converter()
demo = converter.structure(raw_user, UserDoc)

Error

Traceback (most recent call last):
  File "/Users/jrpat/.vscode/extensions/ms-python.python-2025.1.2025022102-darwin-arm64/python_files/python_server.py", line 133, in exec_user_input
    retval = callable_(user_input, user_globals)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
  File "/Users/jrpat/example/.venv/lib/python3.12/site-packages/cattrs/converters.py", line 558, in structure
    return self._structure_func.dispatch(cl)(obj, cl)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jrpat/example/.venv/lib/python3.12/site-packages/cattrs/dispatch.py", line 134, in dispatch_without_caching
    res = self._function_dispatch.dispatch(typ)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jrpat/example/.venv/lib/python3.12/site-packages/cattrs/dispatch.py", line 76, in dispatch
    return handler(typ)
           ^^^^^^^^^^^^
  File "/Users/jrpat/example/.venv/lib/python3.12/site-packages/cattrs/converters.py", line 1290, in gen_structure_attrs_fromdict
    return make_dict_structure_fn(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jrpat/example/.venv/lib/python3.12/site-packages/cattrs/gen/__init__.py", line 772, in make_dict_structure_fn
    return make_dict_structure_fn_from_attrs(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jrpat/example/.venv/lib/python3.12/site-packages/cattrs/gen/__init__.py", line 406, in make_dict_structure_fn_from_attrs
    handler = find_structure_handler(
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jrpat/example/.venv/lib/python3.12/site-packages/cattrs/gen/_shared.py", line 52, in find_structure_handler
    handler = c.get_structure_hook(type, cache_result=False)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jrpat/example/.venv/lib/python3.12/site-packages/cattrs/converters.py", line 576, in get_structure_hook
    else self._structure_func.dispatch_without_caching(type)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jrpat/example/.venv/lib/python3.12/site-packages/cattrs/dispatch.py", line 134, in dispatch_without_caching
    res = self._function_dispatch.dispatch(typ)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jrpat/example/.venv/lib/python3.12/site-packages/cattrs/dispatch.py", line 76, in dispatch
    return handler(typ)
           ^^^^^^^^^^^^
  File "/Users/jrpat/example/.venv/lib/python3.12/site-packages/cattrs/converters.py", line 697, in _find_type_alias_structure_hook
    res = self.get_structure_hook(base)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jrpat/example/.venv/lib/python3.12/site-packages/cattrs/converters.py", line 574, in get_structure_hook
    self._structure_func.dispatch(type)
  File "/Users/jrpat/example/.venv/lib/python3.12/site-packages/cattrs/dispatch.py", line 134, in dispatch_without_caching
    res = self._function_dispatch.dispatch(typ)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jrpat/example/.venv/lib/python3.12/site-packages/cattrs/dispatch.py", line 76, in dispatch
    return handler(typ)
           ^^^^^^^^^^^^
  File "/Users/jrpat/example/.venv/lib/python3.12/site-packages/cattrs/preconf/bson.py", line 74, in gen_structure_mapping
    if args and issubclass(args[0], bytes):
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 1 must be a class

Notes

  • This also happens if ThingType is a type alias, eg. type ThingType = … or if inline like things: dict[Literal[…], …]

Thanks!

Activity

  1. changed the title [-]Preconfigured bson errors when destructuring class with dictionary field that has a literal key[/-] [+]Preconfigured bson converter errors when destructuring class with dictionary field that has a literal key[/+] on Feb 24, 2025
  2. changed the title [-]Preconfigured bson converter errors when destructuring class with dictionary field that has a literal key[/-] [+]Preconfigured bson converter errors when destructuring class with dictionary field that has a Literal key[/+] on Feb 24, 2025
  3. Tinche commented on Feb 24, 2025

    @Tinche
    Member

    Howdy,

    I noticed this too and this was fixed already in #599. I haven't had the chance to release it yet since it's blocked by another thing. I'm doing to close it as a duplicate to keep the issue tracker clean.

    One advice I can give you in the meantime is to maybe model this data as a typed dict - that's what I did in the codebase I ran into this. Something like this:

    from typing import NotRequired, TypedDict
    
    import attrs
    
    import cattrs.preconf.bson
    
    
    class ThingsDict(TypedDict):
        a: NotRequired[bool | None]
        b: NotRequired[bool | None]
    
    
    @attrs.define
    class UserDoc:
        things: ThingsDict = attrs.field(factory=dict)
    
    
    raw_user = {"things": {"a": True}}
    
    converter = cattrs.preconf.bson.make_converter()
    demo = converter.structure(raw_user, UserDoc)
    print(demo)

    It's a little more typing but (IMO) more idiomatic, and will give you more flexibility in the future.

    If this doesn't work for you, I can give you a piece of code that will solve the issue until the new version comes along.

  4. added this to the 25.1 milestone on Feb 24, 2025
  5. linked a pull request that will close this issueFix preconf mapping optimization #599on Feb 24, 2025
  6. added theissue type on Feb 24, 2025
  7. Tinche commented on Feb 24, 2025

    @Tinche
    Member

    (Oh, another solution would be to use cattrs directly from git until the next release - I'm careful to ensure main is very stable.)

  8. jrpat commented on Feb 24, 2025

    @jrpat
    Author

    Awesome - thanks! My project is still very much in development, so I can happily depend on the git version for now.

    Although you're totally right about the TypedDict approach being more idiomatic. Not sure why I went the Literal route - probably it was a gradual evolution and I never rethought it.

    Thanks for all your hard work on cattrs!

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

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions