Skip to content

Commit 972b7ff

Browse files
authored
MOTOR-1352 Update APIs to Support PyMongo 4.9 (#297)
1 parent 72e68b7 commit 972b7ff

18 files changed

+61
-60
lines changed

motor/core.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
from pymongo.client_session import ClientSession
2828
from pymongo.collection import Collection
2929
from pymongo.command_cursor import CommandCursor, RawBatchCommandCursor
30-
from pymongo.cursor import _QUERY_OPTIONS, Cursor, RawBatchCursor
30+
from pymongo.cursor import Cursor, RawBatchCursor
31+
from pymongo.cursor_shared import _QUERY_OPTIONS
3132
from pymongo.database import Database
3233
from pymongo.driver_info import DriverInfo
3334
from pymongo.encryption import ClientEncryption
@@ -1769,8 +1770,6 @@ class AgnosticCursor(AgnosticBaseCursor):
17691770
comment = MotorCursorChainingMethod()
17701771
allow_disk_use = MotorCursorChainingMethod()
17711772

1772-
_Cursor__die = AsyncRead()
1773-
17741773
def rewind(self):
17751774
"""Rewind this cursor to its unevaluated state."""
17761775
self.delegate.rewind()
@@ -1788,13 +1787,13 @@ def __deepcopy__(self, memo):
17881787
return self.__class__(self.delegate.__deepcopy__(memo), self.collection)
17891788

17901789
def _query_flags(self):
1791-
return self.delegate._Cursor__query_flags
1790+
return self.delegate._query_flags
17921791

17931792
def _data(self):
1794-
return self.delegate._Cursor__data
1793+
return self.delegate._data
17951794

17961795
def _killed(self):
1797-
return self.delegate._Cursor__killed
1796+
return self.delegate._killed
17981797

17991798

18001799
class AgnosticRawBatchCursor(AgnosticCursor):
@@ -1806,8 +1805,6 @@ class AgnosticCommandCursor(AgnosticBaseCursor):
18061805
__motor_class_name__ = "MotorCommandCursor"
18071806
__delegate_class__ = CommandCursor
18081807

1809-
_CommandCursor__die = AsyncRead()
1810-
18111808
async def try_next(self):
18121809
"""Advance the cursor without blocking indefinitely.
18131810
@@ -1834,10 +1831,10 @@ def _query_flags(self):
18341831
return 0
18351832

18361833
def _data(self):
1837-
return self.delegate._CommandCursor__data
1834+
return self.delegate._data
18381835

18391836
def _killed(self):
1840-
return self.delegate._CommandCursor__killed
1837+
return self.delegate._killed
18411838

18421839

18431840
class AgnosticRawBatchCommandCursor(AgnosticCommandCursor):
@@ -1849,25 +1846,25 @@ class _LatentCursor:
18491846
"""Take the place of a PyMongo CommandCursor until aggregate() begins."""
18501847

18511848
alive = True
1852-
_CommandCursor__data = []
1853-
_CommandCursor__id = None
1854-
_CommandCursor__killed = False
1855-
_CommandCursor__sock_mgr = None
1856-
_CommandCursor__session = None
1857-
_CommandCursor__explicit_session = None
1849+
_data = []
1850+
_id = None
1851+
_killed = False
1852+
_sock_mgr = None
1853+
_session = None
1854+
_explicit_session = None
18581855
cursor_id = None
18591856

18601857
def __init__(self, collection):
1861-
self._CommandCursor__collection = collection.delegate
1858+
self._collection = collection.delegate
18621859

1863-
def _CommandCursor__end_session(self, *args, **kwargs):
1860+
def _end_session(self, *args, **kwargs):
18641861
pass
18651862

1866-
def _CommandCursor__die(self, *args, **kwargs):
1863+
def _die_lock(self, *args, **kwargs):
18671864
pass
18681865

18691866
def clone(self):
1870-
return _LatentCursor(self._CommandCursor__collection)
1867+
return _LatentCursor(self._collection)
18711868

18721869
def rewind(self):
18731870
pass
@@ -1924,9 +1921,9 @@ def _on_started(self, original_future, future):
19241921
# Return early if the task was cancelled.
19251922
if original_future.done():
19261923
return
1927-
if self.delegate._CommandCursor__data or not self.delegate.alive:
1924+
if self.delegate._data or not self.delegate.alive:
19281925
# _get_more is complete.
1929-
original_future.set_result(len(self.delegate._CommandCursor__data))
1926+
original_future.set_result(len(self.delegate._data))
19301927
else:
19311928
# Send a getMore.
19321929
future = super()._get_more()

motor/core.pyi

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ from bson.raw_bson import RawBSONDocument
4040
from pymongo import IndexModel, ReadPreference, WriteConcern
4141
from pymongo.change_stream import ChangeStream
4242
from pymongo.client_options import ClientOptions
43-
from pymongo.client_session import _T, ClientSession, SessionOptions, TransactionOptions
44-
from pymongo.collection import Collection, ReturnDocument, _WriteOp # noqa: F401
43+
from pymongo.client_session import ClientSession, SessionOptions, TransactionOptions
44+
from pymongo.collection import Collection, ReturnDocument # noqa: F401
4545
from pymongo.command_cursor import CommandCursor, RawBatchCommandCursor
46-
from pymongo.cursor import Cursor, RawBatchCursor, _Hint, _Sort
46+
from pymongo.cursor import Cursor, RawBatchCursor
47+
from pymongo.cursor_shared import _Hint, _Sort
4748
from pymongo.database import Database
4849
from pymongo.encryption import ClientEncryption, RewrapManyDataKeyResult
4950
from pymongo.encryption_options import RangeOpts
@@ -57,6 +58,8 @@ from pymongo.results import (
5758
InsertOneResult,
5859
UpdateResult,
5960
)
61+
from pymongo.synchronous.client_session import _T
62+
from pymongo.synchronous.collection import _WriteOp
6063
from pymongo.topology_description import TopologyDescription
6164
from pymongo.typings import (
6265
_Address,
@@ -756,8 +759,7 @@ class AgnosticRawBatchCommandCursor(AgnosticCommandCursor[_DocumentType]):
756759

757760
class _LatentCursor(Generic[_DocumentType]):
758761
def __init__(self, collection: AgnosticCollection[_DocumentType]): ...
759-
def _CommandCursor__end_session(self, *args: Any, **kwargs: Any) -> None: ...
760-
def _CommandCursor__die(self, *args: Any, **kwargs: Any) -> None: ...
762+
def _end_session(self, *args: Any, **kwargs: Any) -> None: ...
761763
def clone(self) -> _LatentCursor[_DocumentType]: ...
762764
def rewind(self) -> _LatentCursor[_DocumentType]: ...
763765

motor/motor_asyncio.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from typing import Any, Mapping, MutableMapping, Optional, Union
33
from bson import Code, CodecOptions, Timestamp
44
from bson.raw_bson import RawBSONDocument
55
from pymongo.client_session import TransactionOptions
6-
from pymongo.cursor import _Hint, _Sort
6+
from pymongo.cursor_shared import _Hint, _Sort
77
from pymongo.read_concern import ReadConcern
88
from pymongo.read_preferences import ReadPreference, _ServerMode
99
from pymongo.typings import _CollationIn, _DocumentType, _DocumentTypeArg, _Pipeline

motor/motor_gridfs.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ class AgnosticGridOutCursor(AgnosticCursor):
3737
__motor_class_name__ = "MotorGridOutCursor"
3838
__delegate_class__ = gridfs.GridOutCursor
3939

40-
# PyMongo's GridOutCursor inherits __die from Cursor.
41-
_Cursor__die = AsyncCommand()
42-
4340
def next_object(self):
4441
"""**DEPRECATED** - Get next GridOut object from cursor."""
4542
# Note: the super() call will raise a warning for the deprecation.
@@ -88,7 +85,6 @@ class AgnosticGridOut:
8885
__motor_class_name__ = "MotorGridOut"
8986
__delegate_class__ = gridfs.GridOut
9087

91-
_ensure_file = AsyncCommand()
9288
_id = MotorGridOutProperty()
9389
aliases = MotorGridOutProperty()
9490
chunk_size = MotorGridOutProperty()
@@ -98,6 +94,7 @@ class AgnosticGridOut:
9894
length = MotorGridOutProperty()
9995
metadata = MotorGridOutProperty()
10096
name = MotorGridOutProperty()
97+
_open = AsyncCommand(attr_name="open")
10198
read = AsyncRead()
10299
readable = DelegateMethod()
103100
readchunk = AsyncRead()
@@ -160,7 +157,7 @@ def open(self):
160157
:class:`~motor.MotorGridOut` now opens itself on demand, calling
161158
``open`` explicitly is rarely needed.
162159
"""
163-
return self._framework.chain_return_value(self._ensure_file(), self.get_io_loop(), self)
160+
return self._framework.chain_return_value(self._open(), self.get_io_loop(), self)
164161

165162
def get_io_loop(self):
166163
return self.io_loop

motor/motor_gridfs.pyi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ _SEEK_END = os.SEEK_END
3535
class AgnosticGridOutCursor(AgnosticCursor):
3636
__motor_class_name__: str
3737
__delegate_class__: type[GridOutCursor]
38-
async def _Cursor__die(self, synchronous: bool = False) -> None: ...
3938
def next_object(self) -> AgnosticGridOutCursor: ...
4039

4140
class AgnosticGridOut:
@@ -50,7 +49,7 @@ class AgnosticGridOut:
5049
length: int
5150
upload_date: datetime.datetime
5251
metadata: Optional[Mapping[str, Any]]
53-
async def _ensure_file(self) -> None: ...
52+
async def _open(self) -> None: ...
5453
def close(self) -> None: ...
5554
async def read(self, size: int = -1) -> NoReturn: ...
5655
def readable(self) -> bool: ...

motor/motor_tornado.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from typing import Any, Mapping, MutableMapping, Optional, Union
33
from bson import Code, CodecOptions, Timestamp
44
from bson.raw_bson import RawBSONDocument
55
from pymongo.client_session import TransactionOptions
6-
from pymongo.cursor import _Hint, _Sort
6+
from pymongo.cursor_shared import _Hint, _Sort
77
from pymongo.read_concern import ReadConcern
88
from pymongo.read_preferences import ReadPreference, _ServerMode
99
from pymongo.typings import _CollationIn, _DocumentType, _DocumentTypeArg, _Pipeline

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ Tracker = "https://jira.mongodb.org/projects/MOTOR/issues"
5252
path = "motor/_version.py"
5353
validate-bump = false
5454

55+
[tool.hatch.metadata]
56+
# TODO: MOTOR-1351 Remove before releasing Motor 3.6.
57+
allow-direct-references = true
58+
5559
[tool.hatch.metadata.hooks.requirements_txt]
5660
files = ["requirements.txt"]
5761

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pymongo>=4.5,<5
1+
pymongo @ git+https://github.com/mongodb/mongo-python-driver.git

test/asyncio_tests/test_asyncio_basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ async def test_read_preference(self):
9898
motor_cursor = collection.find()
9999
cursor = motor_cursor.delegate
100100

101-
self.assertEqual(Nearest(tag_sets=[{"yay": "jesse"}]), cursor._read_preference())
101+
self.assertEqual(Nearest(tag_sets=[{"yay": "jesse"}]), cursor._get_read_preference())
102102

103103
cx.close()
104104

test/asyncio_tests/test_asyncio_collection.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ async def test_async_create_encrypted_collection(self):
287287
self.assertEqual(exc.exception.code, 121)
288288
await self.db.drop_collection("testing1", encrypted_fields=ef)
289289

290+
@env.require_version_min(8, 0, -1, -1)
290291
@asyncio_test
291292
async def test_async_encrypt_expression(self):
292293
c = self.collection
@@ -299,12 +300,12 @@ async def test_async_encrypt_expression(self):
299300
"local", key_alt_names=["pymongo_encryption_example_1"]
300301
)
301302
name = "DoubleNoPrecision"
302-
range_opts = RangeOpts(sparsity=1)
303+
range_opts = RangeOpts(sparsity=1, trim_factor=1)
303304
for i in [6.0, 30.0, 200.0]:
304305
insert_payload = await client_encryption.encrypt(
305306
float(i),
306307
key_id=data_key,
307-
algorithm=Algorithm.RANGEPREVIEW,
308+
algorithm=Algorithm.RANGE,
308309
contention_factor=0,
309310
range_opts=range_opts,
310311
)
@@ -323,8 +324,8 @@ async def test_async_encrypt_expression(self):
323324
]
324325
},
325326
key_id=data_key,
326-
algorithm=Algorithm.RANGEPREVIEW,
327-
query_type=QueryType.RANGEPREVIEW,
327+
algorithm=Algorithm.RANGE,
328+
query_type=QueryType.RANGE,
328329
contention_factor=0,
329330
range_opts=range_opts,
330331
)

0 commit comments

Comments
 (0)