Repository navigation
Closed as duplicate
Bug
Copy link
Milestone
Description
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
ThingTypeis a type alias, eg.type ThingType = …or if inline likethings: dict[Literal[…], …]
Thanks!
Activity
[-]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[/+][-]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[/+]Tinche commentedon Feb 24, 2025
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:
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.
Tinche commentedon Feb 24, 2025
(Oh, another solution would be to use cattrs directly from git until the next release - I'm careful to ensure main is very stable.)
jrpat commentedon Feb 24, 2025
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!