Skip to content

Commit 368feee

Browse files
rozzabehackett
authored andcommitted
Clean ups of deprecated kwargs
1 parent 44cf142 commit 368feee

File tree

2 files changed

+35
-91
lines changed

2 files changed

+35
-91
lines changed

pymongo/collection.py

Lines changed: 28 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Collection(common.BaseObject):
3838
"""A Mongo collection.
3939
"""
4040

41-
def __init__(self, database, name, options=None, create=False, **kwargs):
41+
def __init__(self, database, name, create=False, **kwargs):
4242
"""Get / create a Mongo collection.
4343
4444
Raises :class:`TypeError` if `name` is not an instance of
@@ -57,16 +57,20 @@ def __init__(self, database, name, options=None, create=False, **kwargs):
5757
:Parameters:
5858
- `database`: the database to get a collection from
5959
- `name`: the name of the collection to get
60-
- `options`: DEPRECATED dictionary of collection options
6160
- `create` (optional): if ``True``, force collection
6261
creation even without options being set
6362
- `**kwargs` (optional): additional keyword arguments will
6463
be passed as options for the create collection command
6564
65+
.. versionchanged:: 2.2
66+
Removed deprecated argument: options
67+
6668
.. versionadded:: 2.1
6769
uuid_subtype attribute
70+
6871
.. versionchanged:: 1.5
6972
deprecating `options` in favor of kwargs
73+
7074
.. versionadded:: 1.5
7175
the `create` parameter
7276
@@ -81,16 +85,6 @@ def __init__(self, database, name, options=None, create=False, **kwargs):
8185
if not isinstance(name, basestring):
8286
raise TypeError("name must be an instance of basestring")
8387

84-
if options is not None:
85-
warnings.warn("the options argument to Collection is deprecated "
86-
"and will be removed. please use kwargs instead.",
87-
DeprecationWarning)
88-
if not isinstance(options, dict):
89-
raise TypeError("options must be an instance of dict")
90-
options.update(kwargs)
91-
elif kwargs:
92-
options = kwargs
93-
9488
if not name or ".." in name:
9589
raise InvalidName("collection names cannot be empty")
9690
if "$" in name and not (name.startswith("oplog.$main") or
@@ -108,8 +102,8 @@ def __init__(self, database, name, options=None, create=False, **kwargs):
108102
self.__name = unicode(name)
109103
self.__uuid_subtype = OLD_UUID_SUBTYPE
110104
self.__full_name = u"%s.%s" % (self.__database.name, self.__name)
111-
if create or options is not None:
112-
self.__create(options)
105+
if create or kwargs:
106+
self.__create(kwargs)
113107

114108
def __create(self, options):
115109
"""Sends a create command with the given options.
@@ -631,8 +625,7 @@ def count(self):
631625
"""
632626
return self.find().count()
633627

634-
def create_index(self, key_or_list, deprecated_unique=None,
635-
ttl=300, **kwargs):
628+
def create_index(self, key_or_list, ttl=300, **kwargs):
636629
"""Creates an index on this collection.
637630
638631
Takes either a single key or a list of (key, direction) pairs.
@@ -669,7 +662,6 @@ def create_index(self, key_or_list, deprecated_unique=None,
669662
:Parameters:
670663
- `key_or_list`: a single key or a list of (key, direction)
671664
pairs specifying the index to create
672-
- `deprecated_unique`: DEPRECATED - use `unique` as a kwarg
673665
- `ttl` (optional): time window (in seconds) during which
674666
this index will be recognized by subsequent calls to
675667
:meth:`ensure_index` - see documentation for
@@ -678,6 +670,9 @@ def create_index(self, key_or_list, deprecated_unique=None,
678670
options (see the above list) should be passed as keyword
679671
arguments
680672
673+
.. versionchanged:: 2.2
674+
Removed deprecated argument: deprecated_unique
675+
681676
.. versionchanged:: 1.5.1
682677
Accept kwargs to support all index creation options.
683678
@@ -693,12 +688,6 @@ def create_index(self, key_or_list, deprecated_unique=None,
693688

694689
index = {"key": index_doc, "ns": self.__full_name}
695690

696-
if deprecated_unique is not None:
697-
warnings.warn("using a positional arg to specify unique is "
698-
"deprecated, please use kwargs",
699-
DeprecationWarning)
700-
index["unique"] = deprecated_unique
701-
702691
name = "name" in kwargs and kwargs["name"] or _gen_index_name(keys)
703692
index["name"] = name
704693

@@ -719,8 +708,7 @@ def create_index(self, key_or_list, deprecated_unique=None,
719708

720709
return name
721710

722-
def ensure_index(self, key_or_list, deprecated_unique=None,
723-
ttl=300, **kwargs):
711+
def ensure_index(self, key_or_list, ttl=300, **kwargs):
724712
"""Ensures that an index exists on this collection.
725713
726714
Takes either a single key or a list of (key, direction) pairs.
@@ -765,14 +753,16 @@ def ensure_index(self, key_or_list, deprecated_unique=None,
765753
:Parameters:
766754
- `key_or_list`: a single key or a list of (key, direction)
767755
pairs specifying the index to create
768-
- `deprecated_unique`: DEPRECATED - use `unique` as a kwarg
769756
- `ttl` (optional): time window (in seconds) during which
770757
this index will be recognized by subsequent calls to
771758
:meth:`ensure_index`
772759
- `**kwargs` (optional): any additional index creation
773760
options (see the above list) should be passed as keyword
774761
arguments
775762
763+
.. versionchanged:: 2.2
764+
Removed deprecated argument: deprecated_unique
765+
776766
.. versionchanged:: 1.5.1
777767
Accept kwargs to support all index creation options.
778768
@@ -789,8 +779,7 @@ def ensure_index(self, key_or_list, deprecated_unique=None,
789779

790780
if not self.__database.connection._cached(self.__database.name,
791781
self.__name, name):
792-
return self.create_index(key_or_list, deprecated_unique,
793-
ttl, **kwargs)
782+
return self.create_index(key_or_list, ttl, **kwargs)
794783
return None
795784

796785
def drop_indexes(self):
@@ -901,8 +890,7 @@ def options(self):
901890

902891
# TODO key and condition ought to be optional, but deprecation
903892
# could be painful as argument order would have to change.
904-
def group(self, key, condition, initial, reduce, finalize=None,
905-
command=True):
893+
def group(self, key, condition, initial, reduce, finalize=None):
906894
"""Perform a query similar to an SQL *group by* operation.
907895
908896
Returns an array of grouped items.
@@ -929,20 +917,17 @@ def group(self, key, condition, initial, reduce, finalize=None,
929917
- `initial`: initial value of the aggregation counter object
930918
- `reduce`: aggregation function as a JavaScript string
931919
- `finalize`: function to be called on each object in output list.
932-
- `command` (optional): DEPRECATED if ``True``, run the group as a
933-
command instead of in an eval - this option is deprecated and
934-
will be removed in favor of running all groups as commands
920+
921+
.. versionchanged:: 2.2
922+
Removed deprecated argument: command
935923
936924
.. versionchanged:: 1.4
937925
The `key` argument can now be ``None`` or a JavaScript function,
938926
in addition to a :class:`list` of keys.
927+
939928
.. versionchanged:: 1.3
940929
The `command` argument now defaults to ``True`` and is deprecated.
941930
"""
942-
if not command:
943-
warnings.warn("eval-based groups are deprecated, and the "
944-
"command option will be removed.",
945-
DeprecationWarning)
946931

947932
group = {}
948933
if isinstance(key, basestring):
@@ -1016,8 +1001,7 @@ def distinct(self, key):
10161001
"""
10171002
return self.find().distinct(key)
10181003

1019-
def map_reduce(self, map, reduce, out, merge_output=False,
1020-
reduce_output=False, full_response=False, **kwargs):
1004+
def map_reduce(self, map, reduce, out, full_response=False, **kwargs):
10211005
"""Perform a map/reduce operation on this collection.
10221006
10231007
If `full_response` is ``False`` (default) returns a
@@ -1033,16 +1017,6 @@ def map_reduce(self, map, reduce, out, merge_output=False,
10331017
Note: `out` options are order sensitive. :class:`~bson.son.SON`
10341018
can be used to specify multiple options.
10351019
e.g. SON([('replace', <collection name>), ('db', <database name>)])
1036-
- `merge_output` (optional) DEPRECATED: Merge output into `out`.
1037-
If the same key exists in both the result set and the existing
1038-
output collection, the new key will overwrite the existing key.
1039-
Ignored if `out` is not an instance of `basestring`.
1040-
- `reduce_output` (optional) DEPRECATED: If documents exist for
1041-
a given key in the result set and in the existing output
1042-
collection, then a reduce operation (using the specified reduce
1043-
function) will be performed on the two values and the result will
1044-
be written to the output collection.
1045-
Ignored if `out` is not an instance of `basestring`.
10461020
- `full_response` (optional): if ``True``, return full response to
10471021
this command - otherwise just return the result collection
10481022
- `**kwargs` (optional): additional arguments to the
@@ -1055,6 +1029,9 @@ def map_reduce(self, map, reduce, out, merge_output=False,
10551029
10561030
.. seealso:: :doc:`/examples/map_reduce`
10571031
1032+
.. versionchanged:: 2.2
1033+
Removed deprecated arguments: merge_output and reduce_output
1034+
10581035
.. versionchanged:: 1.11+
10591036
DEPRECATED The merge_output and reduce_output parameters.
10601037
@@ -1064,31 +1041,13 @@ def map_reduce(self, map, reduce, out, merge_output=False,
10641041
10651042
.. mongodoc:: mapreduce
10661043
"""
1067-
if merge_output or reduce_output:
1068-
warnings.warn("merge_output and reduce_output are deprecated, "
1069-
"please pass {<merge|reduce>: <collection name>} "
1070-
"as the 'out' parameter instead.",
1071-
DeprecationWarning)
1072-
if merge_output and reduce_output:
1073-
raise InvalidOperation("Can't do both merge "
1074-
"and re-reduce of output.")
1075-
1076-
if isinstance(out, basestring):
1077-
if merge_output:
1078-
out_conf = {"merge": out}
1079-
elif reduce_output:
1080-
out_conf = {"reduce": out}
1081-
else:
1082-
out_conf = out
1083-
elif isinstance(out, dict):
1084-
out_conf = out
1085-
else:
1044+
if not isinstance(out, (basestring, dict)):
10861045
raise TypeError("'out' must be an instance of basestring or dict")
10871046

10881047
response = self.__database.command("mapreduce", self.__name,
10891048
uuid_subtype=self.__uuid_subtype,
10901049
map=map, reduce=reduce,
1091-
out=out_conf, **kwargs)
1050+
out=out, **kwargs)
10921051

10931052
if full_response or not response.get('result'):
10941053
return response

test/test_collection.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,13 +1081,6 @@ def group_checker(args, expected):
10811081
"function (obj, prev) { prev.count++; }"
10821082
))
10831083

1084-
warnings.simplefilter("error")
1085-
self.assertRaises(DeprecationWarning,
1086-
db.test.group, [], {}, {"count": 0},
1087-
"function (obj, prev) { prev.count++; }",
1088-
command=False)
1089-
warnings.simplefilter("default")
1090-
10911084
self.assertRaises(OperationFailure, db.test.group,
10921085
[], {}, {}, "5 ++ 5")
10931086

@@ -1115,17 +1108,17 @@ def test_group_with_scope(self):
11151108
self.assertEqual(2, db.test.group([], {}, {"count": 0},
11161109
Code(reduce_function,
11171110
{"inc_value": 1}),
1118-
command=True)[0]['count'])
1111+
)[0]['count'])
11191112

11201113
self.assertEqual(4, db.test.group([], {}, {"count": 0},
11211114
Code(reduce_function,
11221115
{"inc_value": 2}),
1123-
command=True)[0]['count'])
1116+
)[0]['count'])
11241117

11251118
self.assertEqual(1, db.test.group([], {}, {"count": 0},
11261119
Code(reduce_function,
11271120
{"inc_value": 0.5}),
1128-
command=True)[0]['count'])
1121+
)[0]['count'])
11291122

11301123
def test_large_limit(self):
11311124
db = self.db
@@ -1408,28 +1401,20 @@ def test_map_reduce(self):
14081401
result = db.test.map_reduce(map, reduce, out='mrunittests')
14091402
self.assertEqual(1, result.find_one({"_id": "hampster"})["value"])
14101403
db.test.remove({"id": 5})
1411-
warnings.simplefilter("ignore")
1404+
14121405
result = db.test.map_reduce(map, reduce,
1413-
out='mrunittests', merge_output=True)
1406+
out={'merge': 'mrunittests'})
14141407
self.assertEqual(3, result.find_one({"_id": "cat"})["value"])
14151408
self.assertEqual(1, result.find_one({"_id": "hampster"})["value"])
14161409

14171410
result = db.test.map_reduce(map, reduce,
1418-
out='mrunittests', reduce_output=True)
1419-
warnings.simplefilter("default")
1411+
out={'reduce': 'mrunittests'})
1412+
14201413
self.assertEqual(6, result.find_one({"_id": "cat"})["value"])
14211414
self.assertEqual(4, result.find_one({"_id": "dog"})["value"])
14221415
self.assertEqual(2, result.find_one({"_id": "mouse"})["value"])
14231416
self.assertEqual(1, result.find_one({"_id": "hampster"})["value"])
14241417

1425-
self.assertRaises(InvalidOperation,
1426-
db.test.map_reduce,
1427-
map,
1428-
reduce,
1429-
out='mrunittests',
1430-
merge_output=True,
1431-
reduce_output=True)
1432-
14331418
result = db.test.map_reduce(
14341419
map,
14351420
reduce,

0 commit comments

Comments
 (0)