Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Our backwards-compatibility policy can be found [here](https://github.com/python

## NEXT

- Fix the bson preconfigured converter structuring `TypedDict`s, `Counter`s and `defaultdict`s as plain dictionaries, skipping their specific hooks.
([#836](https://github.com/python-attrs/cattrs/issues/836))

- Fix the msgspec converter passing typed deques directly to the encoder, which cannot serialize them. Deques are now unstructured into lists, including when nested in attrs classes or dataclasses.
([#818](https://github.com/python-attrs/cattrs/issues/818) [#819](https://github.com/python-attrs/cattrs/pull/819))

Expand Down
16 changes: 13 additions & 3 deletions src/cattrs/preconf/bson.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from bson import DEFAULT_CODEC_OPTIONS, CodecOptions, Int64, ObjectId, decode, encode
from typing_extensions import TypeForm

from .._compat import is_mapping, is_subclass
from ..cols import mapping_structure_factory
from .._compat import is_counter, is_mapping, is_subclass, is_typeddict
from ..cols import is_defaultdict, mapping_structure_factory
from ..converters import BaseConverter, Converter
from ..dispatch import StructureHook
from ..fns import identity
Expand Down Expand Up @@ -95,9 +95,19 @@ def gen_structure_mapping(cl: Any) -> StructureHook:
h = mapping_structure_factory(cl, converter)
return h

def is_bson_mapping(cl: Any) -> bool:
# TypedDicts, Counters and defaultdicts have more specific structure
# hook factories; let them win over the bson mapping override.
return (
is_mapping(cl)
and not is_typeddict(cl)
and not is_counter(cl)
and not is_defaultdict(cl)
)

converter.register_structure_hook(Base85Bytes, lambda v, _: b85decode(v))
converter.register_unstructure_hook_factory(is_mapping, gen_unstructure_mapping)
converter.register_structure_hook_factory(is_mapping, gen_structure_mapping)
converter.register_structure_hook_factory(is_bson_mapping, gen_structure_mapping)

converter.register_structure_hook(ObjectId, lambda v, _: ObjectId(v))
configure_union_passthrough(
Expand Down
58 changes: 57 additions & 1 deletion tests/test_preconf.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
# ruff: noqa: PLC0415
import sys
import time
from collections import defaultdict
from collections.abc import Callable, Iterator, Set
from datetime import date, datetime, timezone
from enum import Enum, IntEnum, unique
from json import dumps as json_dumps
from json import loads as json_loads
from platform import python_implementation
from typing import Any, Dict, Final, List, Literal, NamedTuple, NewType, Union
from typing import (
Any,
Dict,
Final,
List,
Literal,
NamedTuple,
NewType,
TypedDict,
Union,
)

import pytest
from attrs import define, field, fields
Expand Down Expand Up @@ -45,6 +56,7 @@
Sequence,
TupleSubscriptable,
)
from cattrs.errors import ClassValidationError
from cattrs.fns import identity
from cattrs.preconf.bson import make_converter as bson_make_converter
from cattrs.preconf.json import make_converter as json_make_converter
Expand Down Expand Up @@ -764,6 +776,50 @@ def test_bson_objectid():
assert o == converter.structure(o, ObjectId)


def test_bson_typeddict():
"""BSON TypedDicts are structured using their field hooks."""

@define
class User:
name: str

class Event(TypedDict):
day: date
count: int
owner: User

converter = bson_make_converter()
doc = {"day": date(2026, 11, 10), "count": 3, "owner": User("ann")}

assert converter.loads(converter.dumps(doc, unstructure_as=Event), Event) == doc

with pytest.raises(ClassValidationError):
converter.structure({"count": 3}, Event)
with pytest.raises(ClassValidationError):
converter.structure({"day": "x", "count": "three", "owner": 1}, Event)


def test_bson_counter():
"""BSON Counters are structured into Counters."""
converter = bson_make_converter()

res = converter.structure({"a": "2"}, Counter[str])

assert isinstance(res, Counter)
assert res == Counter({"a": 2})


def test_bson_defaultdict():
"""BSON defaultdicts are structured into defaultdicts."""
converter = bson_make_converter()

res = converter.structure({"a": "2"}, defaultdict[str, int])

assert isinstance(res, defaultdict)
assert res == defaultdict(int, {"a": 2})
assert res["missing"] == 0


def test_bson_native_enums():
"""Bare, string and int enums are handled correctly."""

Expand Down