Repository navigation
Conversation
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>
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #373.
When the bug occurs
An object that is dict-like in the sense
dict()anddict.update()accept, havingkeys()andobj[key], but is not registered as acollections.abc.Mapping. The standard library'ssqlite3.Rowis the common case: it haskeys()and indexing by name, but noitems(), 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')gaveBox({'a': 'b', 'c': 'd'})with no error, and a row with a non-string value raisedTypeError: cannot unpack non-iterable int object.Box.update(row)andBox.merge_update(row)sawkeys()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)anddict().update(row)both work, becausedictgoes throughkeys().The change
Box.__init__: an argument that is not aMappingbut haskeys()is read throughkeys()and item access, then handled by the existing mapping branch (self-reference anddefault_box_none_transformincluded). The iterable-of-pairs branch is unchanged for everything else.Box.update,Box.merge_update,ConfigBox.update: iterateobj.keys()instead ofobj, asdict.updatedoes.Tests
test_box_from_keys_protocol_objectcovers a realsqlite3.Rowthrough the constructor (alone and with keyword extras),update(),merge_update()andConfigBox.update(); a minimal class with onlykeys()and__getitem__, including nested conversion; the sad path wherekeys()names a key that__getitem__rejects (theKeyErrorpropagates); and thatBox | rowstill raisesTypeError, asdict | rowdoes. The test fails ondevelopwithout the source change (constructor:TypeError), and passes with it.uv run pytest: 1225 passed, 1 skipped.ruff check,ruff format --checkandmypy boxare clean. The Cython build was not exercised; the change is plain Python in the same functions.CHANGES.rsthas an entry under a new 8.0.2 heading, which you may well want to rename.🤖 Generated with Claude Code