PYTHON-2440 Workaround namedtuple._asdict() bug on Python 3.4 (#525)

This commit is contained in:
Prashant Mital 2020-11-24 12:11:22 -08:00 committed by GitHub
parent 807ab5ac9c
commit 4119d35d04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 2 deletions

View File

@ -295,6 +295,17 @@ class CodecOptions(_options_base):
self.unicode_decode_error_handler, self.tzinfo,
self.type_registry))
def _options_dict(self):
"""Dictionary of the arguments used to create this object."""
# TODO: PYTHON-2442 use _asdict() instead
return {
'document_class': self.document_class,
'tz_aware': self.tz_aware,
'uuid_representation': self.uuid_representation,
'unicode_decode_error_handler': self.unicode_decode_error_handler,
'tzinfo': self.tzinfo,
'type_registry': self.type_registry}
def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, self._arguments_repr())
@ -310,7 +321,7 @@ class CodecOptions(_options_base):
.. versionadded:: 3.5
"""
opts = self._asdict()
opts = self._options_dict()
opts.update(kwargs)
return CodecOptions(**opts)

View File

@ -311,6 +311,16 @@ class JSONOptions(CodecOptions):
self.json_mode,
super(JSONOptions, self)._arguments_repr()))
def _options_dict(self):
# TODO: PYTHON-2442 use _asdict() instead
options_dict = super(JSONOptions, self)._options_dict()
options_dict.update({
'strict_number_long': self.strict_number_long,
'datetime_representation': self.datetime_representation,
'strict_uuid': self.strict_uuid,
'json_mode': self.json_mode})
return options_dict
def with_options(self, **kwargs):
"""
Make a copy of this JSONOptions, overriding some options::
@ -324,7 +334,7 @@ class JSONOptions(CodecOptions):
.. versionadded:: 3.12
"""
opts = self._asdict()
opts = self._options_dict()
for opt in ('strict_number_long', 'datetime_representation',
'strict_uuid', 'json_mode'):
opts[opt] = kwargs.get(opt, getattr(self, opt))

View File

@ -280,6 +280,9 @@ class ClientUnitTest(unittest.TestCase):
readpreference=ReadPreference.NEAREST.mongos_mode)
self.assertEqual(c.read_preference, ReadPreference.NEAREST)
@unittest.skipIf(
sys.version_info[0] == 3 and sys.version_info[1] == 4,
"PYTHON-2442: workaround namedtuple._asdict() bug on Python 3.4")
def test_metadata(self):
metadata = copy.deepcopy(_METADATA)
metadata['application'] = {'name': 'foobar'}