PYTHON-2501 Remove iteritems from son.SON (#731)

This commit is contained in:
Julius Park 2021-09-22 12:18:19 -07:00 committed by GitHub
parent f1d3f9ca2f
commit fcedc510e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 16 deletions

View File

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

View File

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

View File

@ -669,6 +669,21 @@ custom types to BSON, the :class:`~bson.codec_options.TypeCodec` and
For more information, see the
:doc:`custom type example <examples/custom_type>`.
``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
-------------------

View File

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

View File

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