Skip to content

Commit 1864f8b

Browse files
Merge pull request #293 from IlyaSkriblovsky/deprecation-warnings-2
DeprecationWarnings for insert(), update(), remove() and as_class
2 parents 006dc20 + 89f17da commit 1864f8b

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

docs/source/NEWS.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
Changelog
22
=========
33

4-
Release 24.0.0 (2024-09-16)
4+
Release 24.0.0 (2024-10-01)
55
---------------------------
66

77
API Changes
88
^^^^^^^^
99

1010
- This is the last release that supports Python <3.8 and MongoDB <4.0
11+
- Collection methods `insert()`, `update()` and `remove()` are deprecated in favor of
12+
corresponding `*_one()` and `*_many()` methods. Old methods will be removed in the next release.
13+
- Collection methods `save()`, `find_and_modify() and `group()` are deprecated and will be removed
14+
in the next release.
15+
- `as_class` argument of `find()`, `find_with_cursor()` and `find_one()` is deprecated and will
16+
be removed in the next release.
1117

1218

1319
Release UPCOMING (yyyy-mm-dd)

tests/basic/test_queries.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
from bson import BSON, ObjectId
16+
from bson import BSON, CodecOptions, ObjectId
1717
from bson.son import SON
1818
from pymongo.collection import ReturnDocument
1919
from pymongo.errors import (
@@ -265,13 +265,28 @@ def test_AsClass(self):
265265
yield self.coll.insert({"x": 42})
266266

267267
doc = yield self.coll.find_one({})
268-
self.assertTrue(type(doc) is dict)
268+
self.assertIs(type(doc), dict)
269269

270270
class CustomDict(dict):
271271
pass
272272

273273
doc = yield self.coll.find_one({}, as_class=CustomDict)
274-
self.assertTrue(type(doc) is CustomDict)
274+
self.assertIs(type(doc), CustomDict)
275+
276+
@defer.inlineCallbacks
277+
def test_AsClassCodecOption(self):
278+
yield self.coll.insert({"x": 42})
279+
280+
doc = yield self.coll.find_one()
281+
self.assertIs(type(doc), dict)
282+
283+
class CustomDict(dict):
284+
pass
285+
286+
doc = yield self.coll.with_options(
287+
codec_options=CodecOptions(document_class=CustomDict)
288+
).find_one()
289+
self.assertIs(type(doc), CustomDict)
275290

276291
@defer.inlineCallbacks
277292
def test_FindOneNone(self):

txmongo/collection.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,12 @@ def __real_find_with_cursor(
448448
filter = BSON.encode(filter, codec_options=self.codec_options)
449449

450450
as_class = kwargs.get("as_class")
451+
if as_class is not None:
452+
warnings.warn(
453+
"as_class argument of will be removed in the next version of TxMongo. Please use document_class parameter of codec_options.",
454+
DeprecationWarning,
455+
)
456+
451457
proto = self._database.connection.getprotocol()
452458

453459
def after_connection(protocol):
@@ -591,6 +597,11 @@ def count(self, filter=None, **kwargs):
591597

592598
@timeout
593599
def group(self, keys, initial, reduce, condition=None, finalize=None, **kwargs):
600+
warnings.warn(
601+
"Collection.group() method will be removed in the next version of TxMongo. Please use aggregate() or map_reduce().",
602+
DeprecationWarning,
603+
)
604+
594605
body = {
595606
"ns": self._collection_name,
596607
"initial": initial,
@@ -674,6 +685,11 @@ def insert(self, docs, safe=None, flags=0, **kwargs):
674685
:class:`Deferred` that fires with single ``_id`` field or a list of
675686
``_id`` fields of inserted documents.
676687
"""
688+
warnings.warn(
689+
"Collection.insert() method will be removed in the next version of TxMongo. Please use insert_one() or insert_many().",
690+
DeprecationWarning,
691+
)
692+
677693
if isinstance(docs, dict):
678694
ids = docs.get("_id", ObjectId())
679695
docs["_id"] = ids
@@ -895,6 +911,10 @@ def update(
895911
:class:`Deferred` that is called back when request is sent to
896912
MongoDB or confirmed by MongoDB (depending on selected Write Concern).
897913
"""
914+
warnings.warn(
915+
"Collection.update() method will be removed in the next version of TxMongo. Please use update_one(), update_many() or replace_one().",
916+
DeprecationWarning,
917+
)
898918

899919
if not isinstance(spec, dict):
900920
raise TypeError("TxMongo: spec must be an instance of dict.")
@@ -1071,6 +1091,12 @@ def on_ok(raw_response):
10711091

10721092
@timeout
10731093
def save(self, doc, safe=None, **kwargs):
1094+
warnings.warn(
1095+
"Collection.save() method will be removed in the next version of TxMongo. "
1096+
"Please use insert_one() or replace_one().",
1097+
DeprecationWarning,
1098+
)
1099+
10741100
if not isinstance(doc, dict):
10751101
raise TypeError(
10761102
"TxMongo: cannot save objects of type {0}".format(type(doc))
@@ -1083,6 +1109,11 @@ def save(self, doc, safe=None, **kwargs):
10831109

10841110
@timeout
10851111
def remove(self, spec, safe=None, single=False, flags=0, **kwargs):
1112+
warnings.warn(
1113+
"Collection.remove() method will be removed in the next version of TxMongo. Please use delete_one() or delete_many().",
1114+
DeprecationWarning,
1115+
)
1116+
10861117
if isinstance(spec, ObjectId):
10871118
spec = SON(dict(_id=spec))
10881119
if not isinstance(spec, dict):
@@ -1318,6 +1349,12 @@ def on_ok(raw):
13181349

13191350
@timeout
13201351
def find_and_modify(self, query=None, update=None, upsert=False, **kwargs):
1352+
warnings.warn(
1353+
"Collection.find_and_modify() method will be removed in the next version of TxMongo. "
1354+
"Please use find_one_and_update(), find_one_and_replace() or find_one_and_delete().",
1355+
DeprecationWarning,
1356+
)
1357+
13211358
no_obj_error = "No matching object found"
13221359

13231360
if not update and not kwargs.get("remove", None):

0 commit comments

Comments
 (0)