← Back to team overview

launchpad-reviewers team mailing list archive

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

 

Colin Watson has proposed merging ~cjwatson/launchpad:six-iter-avoid-pyupgrade into launchpad:master.

Commit message:
Adjust ObjectSet.__iter__ to avoid pyupgrade confusion

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/413389

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.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:six-iter-avoid-pyupgrade into launchpad:master.
diff --git a/lib/lp/codehosting/inmemory.py b/lib/lp/codehosting/inmemory.py
index 522f9d1..5835dc1 100644
--- a/lib/lp/codehosting/inmemory.py
+++ b/lib/lp/codehosting/inmemory.py
@@ -130,7 +130,7 @@ class ObjectSet:
         del self._objects[db_object.id]
 
     def __iter__(self):
-        return six.itervalues(self._objects)
+        return iter(self._objects.values())
 
     def _find(self, **kwargs):
         [(key, value)] = kwargs.items()