Skip to content

Read an object with keys() and item access as a mapping, as dict() does (#373) - #374

Open
gstein wants to merge 1 commit into
cdgriffith:developfrom
gstein:keys-protocol-constructor
Open

gstein wants to merge 1 commit into
cdgriffith:developfrom
gstein:keys-protocol-constructor

Conversation

@gstein

@gstein gstein commented Nov 25, 2026

Copy link
Copy Markdown

Fixes #373.

When the bug occurs

An object that is dict-like in the sense dict() and dict.update() accept, having keys() and obj[key], but is not registered as a collections.abc.Mapping. The standard library's sqlite3.Row is the common case: it has keys() and indexing by name, but no items(), and iterating it yields its values, like a tuple.

  • Box(row) took the iterable-of-pairs branch and unpacked each value as a (key, value) pair: Box(row) for a row of ('ab', 'cd') gave Box({'a': 'b', 'c': 'd'}) with no error, and a row with a non-string value raised TypeError: cannot unpack non-iterable int object.
  • Box.update(row) and Box.merge_update(row) saw keys() and then iterated the object itself for its keys, so they indexed the row by its values: IndexError: No item with that key. ConfigBox.update() did the same.

dict(row) and dict().update(row) both work, because dict goes through keys().

The change

  • Box.__init__: an argument that is not a Mapping but has keys() is read through keys() and item access, then handled by the existing mapping branch (self-reference and default_box_none_transform included). The iterable-of-pairs branch is unchanged for everything else.
  • Box.update, Box.merge_update, ConfigBox.update: iterate obj.keys() instead of obj, as dict.update does.

Tests

test_box_from_keys_protocol_object covers a real sqlite3.Row through the constructor (alone and with keyword extras), update(), merge_update() and ConfigBox.update(); a minimal class with only keys() and __getitem__, including nested conversion; the sad path where keys() names a key that __getitem__ rejects (the KeyError propagates); and that Box | row still raises TypeError, as dict | row does. The test fails on develop without the source change (constructor: TypeError), and passes with it.

uv run pytest: 1225 passed, 1 skipped. ruff check, ruff format --check and mypy box are clean. The Cython build was not exercised; the change is plain Python in the same functions.

CHANGES.rst has an entry under a new 8.0.2 heading, which you may well want to rename.

🤖 Generated with Claude Code

dict() and dict.update() accept any object that has keys() and supports
obj[key], and read it through keys(). Box did not, in three places:

- Box(obj) checked isinstance(obj, Mapping) and otherwise read obj as
  an iterable of (key, value) pairs. For a sqlite3.Row, which has keys()
  and item access but iterates over its values, that meant each value
  was unpacked as a pair: Box(row) for ('ab', 'cd') gave
  {'a': 'b', 'c': 'd'} silently, and a non-string value raised
  TypeError.
- Box.update(obj) and Box.merge_update(obj) saw keys() but then
  iterated obj itself for its keys, so for a Row they indexed by its
  values and raised IndexError. ConfigBox.update did the same.

Now the constructor reads a non-Mapping object that has keys() through
keys() and item access, and hands the result to the mapping branch; and
update, merge_update and ConfigBox.update iterate obj.keys(), as
dict.update does. The iterable-of-pairs path is unchanged for anything
without keys().

Fixes cdgriffith#373

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@gstein

gstein commented Nov 25, 2026 •

Copy link
Copy Markdown
Author

Happy to make modifications! Let me know if anything in this PR doesn't align with your thinking.

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.

1 participant