From fcedc510e1039ab47b1c2d52f1392f298feaf81b Mon Sep 17 00:00:00 2001 From: Julius Park Date: Wed, 22 Sep 2021 12:18:19 -0700 Subject: [PATCH] PYTHON-2501 Remove iteritems from son.SON (#731) --- bson/son.py | 23 ++++++++--------------- doc/changelog.rst | 3 +++ doc/migrate-to-pymongo4.rst | 15 +++++++++++++++ pymongo/collection.py | 2 +- pymongo/helpers.py | 2 ++ 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/bson/son.py b/bson/son.py index a72696129..5a3210fcd 100644 --- a/bson/son.py +++ b/bson/son.py @@ -77,24 +77,16 @@ class SON(dict): def has_key(self, key): return key in self.__keys - # third level takes advantage of second level definitions - def iteritems(self): - for k in self: - yield (k, self[k]) - def iterkeys(self): return self.__iter__() # fourth level uses definitions from lower levels def itervalues(self): - for _, v in self.iteritems(): + for _, v in self.items(): yield v def values(self): - return [v for _, v in self.iteritems()] - - def items(self): - return [(key, self[key]) for key in self] + return [v for _, v in self.items()] def clear(self): self.__keys = [] @@ -122,7 +114,7 @@ class SON(dict): def popitem(self): try: - k, v = next(self.iteritems()) + k, v = next(iter(self.items())) except StopIteration: raise KeyError('container is empty') del self[k] @@ -132,8 +124,8 @@ class SON(dict): # Make progressively weaker assumptions about "other" if other is None: pass - elif hasattr(other, 'iteritems'): # iteritems saves memory and lookups - for k, v in other.iteritems(): + elif hasattr(other, 'items'): + for k, v in other.items(): self[k] = v elif hasattr(other, 'keys'): for k in other.keys(): @@ -155,7 +147,8 @@ class SON(dict): regular dictionary is order-insensitive. """ if isinstance(other, SON): - return len(self) == len(other) and self.items() == other.items() + return len(self) == len(other) and list(self.items()) == \ + list(other.items()) return self.to_dict() == other def __ne__(self, other): @@ -189,7 +182,7 @@ class SON(dict): if val_id in memo: return memo.get(val_id) memo[val_id] = out - for k, v in self.iteritems(): + for k, v in self.items(): if not isinstance(v, RE_TYPE): v = copy.deepcopy(v, memo) out[k] = v diff --git a/doc/changelog.rst b/doc/changelog.rst index 6364985bb..dcc4c881c 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -134,6 +134,9 @@ Breaking Changes in 4.0 - The ``hint`` option is now required when using ``min`` or ``max`` queries with :meth:`~pymongo.collection.Collection.find`. - ``name`` is now a required argument for the :class:`pymongo.driver_info.DriverInfo` class. +- :meth:`~bson.son.SON.items` now returns a ``dict_items`` object rather + than a list. +- Removed :meth:`bson.son.SON.iteritems`. - :class:`~pymongo.collection.Collection` and :class:`~pymongo.database.Database` now raises an error upon evaluating as a Boolean, please use the syntax ``if collection is not None:`` or ``if database is not None:`` as diff --git a/doc/migrate-to-pymongo4.rst b/doc/migrate-to-pymongo4.rst index 7cebec9d8..2906919b2 100644 --- a/doc/migrate-to-pymongo4.rst +++ b/doc/migrate-to-pymongo4.rst @@ -669,6 +669,21 @@ custom types to BSON, the :class:`~bson.codec_options.TypeCodec` and For more information, see the :doc:`custom type example `. +``SON().items()`` now returns ``dict_items`` object. +---------------------------------------------------- +:meth:`~bson.son.SON.items` now returns a ``dict_items`` object rather than +a list. + +``SON().iteritems()`` removed. +------------------------------ +``SON.iteritems()`` now removed. Code that looks like this:: + + for k, v in son.iteritems(): + +Can now be replaced by code that looks like:: + + for k, v in son.items(): + IsMaster is removed ------------------- diff --git a/pymongo/collection.py b/pymongo/collection.py index b30bcf22f..7e8e29908 100644 --- a/pymongo/collection.py +++ b/pymongo/collection.py @@ -1834,7 +1834,7 @@ class Collection(common.BaseObject): cursor = self.list_indexes(session=session) info = {} for index in cursor: - index["key"] = index["key"].items() + index["key"] = list(index["key"].items()) index = dict(index) info[index.pop("name")] = index return info diff --git a/pymongo/helpers.py b/pymongo/helpers.py index 37d6f59b7..55d53d836 100644 --- a/pymongo/helpers.py +++ b/pymongo/helpers.py @@ -72,6 +72,8 @@ def _index_list(key_or_list, direction=None): else: if isinstance(key_or_list, str): return [(key_or_list, ASCENDING)] + if isinstance(key_or_list, abc.ItemsView): + return list(key_or_list) elif not isinstance(key_or_list, (list, tuple)): raise TypeError("if no direction is specified, " "key_or_list must be an instance of list")