Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion bson/codec_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,17 @@ def _arguments_repr(self):
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())

Expand All @@ -310,7 +321,7 @@ def with_options(self, **kwargs):

.. versionadded:: 3.5
"""
opts = self._asdict()
opts = self._options_dict()
opts.update(kwargs)
return CodecOptions(**opts)

Expand Down
12 changes: 11 additions & 1 deletion bson/json_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,16 @@ def _arguments_repr(self):
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::
Expand All @@ -324,7 +334,7 @@ def with_options(self, **kwargs):

.. 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))
Expand Down
3 changes: 3 additions & 0 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ def test_read_preference(self):
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'}
Expand Down