fix(types): mapping protocol on Structs (dict + **obj + iter) — v0.4.4#10
Merged
Conversation
Continuing the litellm-fixture-compat work. dynamiq's embedder code
emits ``meta = {"model": response.model, "usage": dict(response.usage)}``,
which fails on a bare ``msgspec.Struct`` because Python falls back to
integer indexing when neither the mapping protocol (``keys() +
__getitem__``) nor an iterable-of-pairs is recognised.
Extends ``_DictLike`` with the mapping protocol:
- ``keys()`` returns the Struct's declared ``__struct_fields__``
- ``__iter__`` yields field names (matches ``dict.__iter__``)
- ``__len__`` returns the field count
- ``values()`` and ``items()`` derive from keys
- ``__getitem__`` rejects non-str keys with ``KeyError`` instead of
the confusing ``TypeError: attribute name must be string``
Now ``dict(usage)``, ``{**usage}``, and ``for k in usage`` all work
identically to litellm's Pydantic Usage record.
Bumps to 0.4.4.
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.
Summary
Patch release. dynamiq's embedder code emits `meta = {"model": response.model, "usage": dict(response.usage)}`, which fails on a bare `msgspec.Struct` because Python falls back to integer indexing when neither the mapping protocol nor an iterable-of-pairs is recognised. arcllm 0.4.3 had `getitem/setitem` but not the full mapping protocol.
Extends `_DictLike` with:
Now `dict(usage)`, `{**usage}`, `for k in usage` all behave like litellm's Pydantic Usage record.
Validation
Bumps to 0.4.4.
Note
Low Risk
Low risk patch: adds mapping-protocol methods to
_DictLiketo improve compatibility withdict(obj)/**objand adjusts error behavior for non-string keys. Changes are localized to type ergonomics and covered by new unit tests.Overview
Improves LiteLLM-compat dict-style behavior for
msgspec.Structresponse types by extending_DictLiketo implement the mapping protocol (keys,__iter__,__len__, plusitems/values), enablingdict(usage),**usage, andfor k in usage.Tightens
__getitem__to raiseKeyErrorfor non-string keys (avoids confusingdict()integer-index fallbacks), adds regression tests for the new behaviors, and bumps the package version to0.4.4.Reviewed by Cursor Bugbot for commit 76e1fc6. Bugbot is set up for automated code reviews on this repo. Configure here.