🤖 Fix MultiDict.values()/items() to return proper dict views (#1113) - #1527
Open
apoorvdarshan wants to merge 2 commits into
Open
🤖 Fix MultiDict.values()/items() to return proper dict views (#1113)#1527apoorvdarshan wants to merge 2 commits into
apoorvdarshan wants to merge 2 commits into
Conversation
MultiDict.values() and items() returned one-shot generator expressions, so they had no len() and were exhausted after a single iteration. This is inconsistent with keys() (a real dict view) and with the class docstring's promise that MultiDict "behaves exactly like a normal dict". Return collections.abc.ValuesView(self) / ItemsView(self) instead. Because MultiDict is a MutableMapping whose __getitem__ already yields the newest value per key, these standard views deliver the correct values while adding len() support and re-iterability, matching real dict views. Fixes bottlepy#1113. This contribution is dedicated to the public domain. ai-assisted-by: Claude Code (claude-opus-4-8, Anthropic)
Member
|
|
This contribution is dedicated to the public domain. ai-assisted-by: Codex (GPT-5, OpenAI)
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 #1113.
Root cause
MultiDict.values()andMultiDict.items()overrode the implementations inherited fromMutableMappingwith one-shot generator expressions. Those generators had nolen()and were exhausted after one iteration, unlike normal dictionary views.Fix
Remove the legacy
values()anditems()overrides and use the standard implementations already provided byMutableMapping.MultiDict.__getitem__()returns the newest value for each key, so the inherited views preserve the existing value semantics while adding sizing, re-iteration, and normal view containment behavior.The legacy
itervaluesanditeritemsnames remain available as aliases to the inherited implementations.Tests
Added
test_views_are_reiterable_and_sizedto verify thatkeys(),values(), anditems():len();The full suite passes on the minimum supported Python 3.9: 360 passed.
This contribution is dedicated to the public domain.
Disclosure: prepared with AI assistance; manually reviewed and verified locally.