Skip to content

🤖 Fix MultiDict.values()/items() to return proper dict views (#1113) - #1527

Open
apoorvdarshan wants to merge 2 commits into
bottlepy:masterfrom
apoorvdarshan:fix/issue-1113-multidict-views
Open

🤖 Fix MultiDict.values()/items() to return proper dict views (#1113)#1527
apoorvdarshan wants to merge 2 commits into
bottlepy:masterfrom
apoorvdarshan:fix/issue-1113-multidict-views

Conversation

@apoorvdarshan

@apoorvdarshan apoorvdarshan commented Jul 9, 2026

Copy link
Copy Markdown

Fixes #1113.

Root cause

MultiDict.values() and MultiDict.items() overrode the implementations inherited from MutableMapping with one-shot generator expressions. Those generators had no len() and were exhausted after one iteration, unlike normal dictionary views.

Fix

Remove the legacy values() and items() overrides and use the standard implementations already provided by MutableMapping. 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 itervalues and iteritems names remain available as aliases to the inherited implementations.

Tests

Added test_views_are_reiterable_and_sized to verify that keys(), values(), and items():

  • support len();
  • can be iterated more than once; and
  • reflect the newest value for each key.

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.

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)
@defnull

defnull commented Jul 10, 2026

Copy link
Copy Markdown
Member

MutableMapping already implements items() and values(). If MutableMapping has a compatible implementation in all supported python versions,w e can just remove the overrides and get the same effect. Those are probably old Python 2.x compatibility workarounds that never got removed.

This contribution is dedicated to the public domain.

ai-assisted-by: Codex (GPT-5, OpenAI)
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.

MultiDict.values - incorrect implementation in python 3

2 participants