Skip to content

Commit 1c80679

Browse files
jaracobehackett
authored andcommitted
PYTHON-946 - Allow fields to be any sequence.
1 parent e734063 commit 1c80679

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

pymongo/helpers.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def _check_write_command_response(results):
270270

271271

272272
def _fields_list_to_dict(fields, option_name):
273-
"""Takes a list of field names and returns a matching dictionary.
273+
"""Takes a sequence of field names and returns a matching dictionary.
274274
275275
["a", "b"] becomes {"a": 1, "b": 1}
276276
@@ -280,15 +280,13 @@ def _fields_list_to_dict(fields, option_name):
280280
"""
281281
if isinstance(fields, collections.Mapping):
282282
return fields
283-
elif isinstance(fields, list):
284-
as_dict = {}
285-
for field in fields:
286-
if not isinstance(field, string_type):
287-
raise TypeError("%s must be a list of key names, each an "
288-
"instance of %s" % (option_name,
289-
string_type.__name__))
290-
as_dict[field] = 1
291-
return as_dict
292-
else:
293-
raise TypeError("%s must be a mapping or "
294-
"list of key names" % (option_name,))
283+
284+
if isinstance(fields, collections.Sequence):
285+
if not all(isinstance(field, string_type) for field in fields):
286+
raise TypeError("%s must be a list of key names, each an "
287+
"instance of %s" % (option_name,
288+
string_type.__name__))
289+
return dict.fromkeys(fields, 1)
290+
291+
raise TypeError("%s must be a mapping or "
292+
"list of key names" % (option_name,))

0 commit comments

Comments
 (0)