Merge ~cjwatson/launchpad:six-iter-avoid-pyupgrade into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 33fb8d2bd53cdd6a6bfa8182cf5147beb487341e
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:six-iter-avoid-pyupgrade
Merge into: launchpad:master
Diff against target: 13 lines (+1/-1)
1 file modified
lib/lp/codehosting/inmemory.py (+1/-1)
Reviewer Review Type Date Requested Status
Ioana Lasc (community) Approve
Review via email: mp+413389@code.launchpad.net

Commit message

Adjust ObjectSet.__iter__ to avoid pyupgrade confusion

Description of the change

pyupgrade rewrites `six.itervalues(dct)` as `dct.values()`, which is usually correct, but not when returned from an `__iter__` method. That must return an actual iterator, and returning a view object instead raises an exception:

  TypeError: iter() returned non-iterator of type 'dict_values'

Fixing this in pyupgrade seems to be hard as the AST walk doesn't have enough information about ancestor nodes beyond the immediate parent, so just manually fix the one place in Launchpad where this is a problem so that pyupgrade can handle other instances of this pattern mechanically.

To post a comment you must log in.
Revision history for this message
Ioana Lasc (ilasc) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/codehosting/inmemory.py b/lib/lp/codehosting/inmemory.py
2index 522f9d1..5835dc1 100644
3--- a/lib/lp/codehosting/inmemory.py
4+++ b/lib/lp/codehosting/inmemory.py
5@@ -130,7 +130,7 @@ class ObjectSet:
6 del self._objects[db_object.id]
7
8 def __iter__(self):
9- return six.itervalues(self._objects)
10+ return iter(self._objects.values())
11
12 def _find(self, **kwargs):
13 [(key, value)] = kwargs.items()

Subscribers

People subscribed via source and target branches

to status/vote changes: