Skip to content

Add __reversed__ support to keys, values, and items views#1304

Open
r266-tech wants to merge 1 commit intoaio-libs:masterfrom
r266-tech:add-reversed-to-views
Open

Add __reversed__ support to keys, values, and items views#1304
r266-tech wants to merge 1 commit intoaio-libs:masterfrom
r266-tech:add-reversed-to-views

Conversation

@r266-tech
Copy link
Copy Markdown

Summary

Implements the __reversed__ method for _KeysView, _ValuesView, and _ItemsView, allowing reversed() iteration over multidict views.

Since Python 3.8, built-in dict views support reversed(). This PR brings multidict views to parity.

Closes #448.

Changes

  • Added iter_entries_reverse() to _HtKeys for reverse entry traversal
  • Added __reversed__ and _iter_reversed to _KeysView, _ValuesView, and _ItemsView
  • All view types (including CIMultiDict and proxies) support reversed()

Example

from multidict import MultiDict

md = MultiDict([('a', 1), ('b', 2), ('c', 3)])
list(reversed(md.keys()))   # ['c', 'b', 'a']
list(reversed(md.values())) # [3, 2, 1]
list(reversed(md.items()))  # [('c', 3), ('b', 2), ('a', 1)]

Implement the  method for _KeysView, _ValuesView, and
_ItemsView, allowing reversed() iteration over multidict views.

Also adds iter_entries_reverse() to _HtKeys for reverse entry traversal.

Closes aio-libs#448
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Mar 22, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 22, 2026

Merging this PR will not alter performance

✅ 245 untouched benchmarks


Comparing r266-tech:add-reversed-to-views (bd1b5d6) with master (81fc6b9)

Open in CodSpeed

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 22, 2026

Codecov Report

❌ Patch coverage is 30.43478% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.40%. Comparing base (81fc6b9) to head (bd1b5d6).
⚠️ Report is 9 commits behind head on master.

Files with missing lines Patch % Lines
multidict/_multidict_py.py 30.43% 16 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1304      +/-   ##
==========================================
- Coverage   99.85%   99.40%   -0.46%     
==========================================
  Files          26       26              
  Lines        3513     3536      +23     
  Branches      253      259       +6     
==========================================
+ Hits         3508     3515       +7     
- Misses          3       19      +16     
  Partials        2        2              
Flag Coverage Δ
CI-GHA 99.40% <30.43%> (-0.46%) ⬇️
pytest 99.40% <30.43%> (-0.46%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Member

@Vizonex Vizonex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend you add a C Implementation and not just a Python One. This is because most users will end up with compiled binaries and multidict will default to the C Module if found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make keys view reversible

3 participants