Skip to content

Fix bson preconf skipping TypedDict, Counter and defaultdict hooks - #837

Open
ShahabAhmed01 wants to merge 1 commit into
python-attrs:mainfrom
ShahabAhmed01:fix/bson-typeddict-counter-hooks
Open

ShahabAhmed01 wants to merge 1 commit into
python-attrs:mainfrom
ShahabAhmed01:fix/bson-typeddict-counter-hooks

Conversation

@ShahabAhmed01

Copy link
Copy Markdown

Fix the bson preconfigured converter structuring TypedDicts, Counters
and defaultdicts as plain dictionaries, skipping their specific structure
hooks.

Root cause

BsonConverter.is_bson_mapping was registered as a structure hook factory
for is_mapping. Because it was consulted before the TypedDict/Counter/
defaultdict-specific factories, those types fell through to the bson
mapping factory and were structured as plain dicts, skipping their hooks.

Fix

is_bson_mapping now excludes is_typeddict, is_counter and
is_defaultdict, so those types keep their specific hooks; generic mapping
and bytes-key handling are unchanged.

Tests

Adds regression tests test_bson_typeddict, test_bson_counter,
test_bson_defaultdict.

Red/green

All three tests fail without the src/cattrs/preconf/bson.py change and
pass with it. Before, bson output diverged from json and did not raise
ClassValidationError for invalid TypedDict/Counter/defaultdict input;
after it matches json and raises ClassValidationError.

Verification

  • tests/test_preconf.py — 64 passed, 22 skipped (CPython 3.14)
  • Full suite — 1074 passed, 22 skipped, 15 xfailed (CPython 3.13.18 +
    immutables)
  • ruff 0.15.6 — all checks passed; 118 files already formatted

Fixes #836

Copilot AI balanced review requested due to automatic review settings November 13, 2026 09:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bson converter structures TypedDicts (and Counters) as plain dicts, skipping their hooks

2 participants