Skip to content

Commit 96faacb

Browse files
committed
PYTHON-4747 Sync change_stream.py to master
1 parent 42e48ea commit 96faacb

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

pymongo/change_stream.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2024-present MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Re-import of synchronous ChangeStream API for compatibility."""
16+
from __future__ import annotations
17+
18+
from pymongo.synchronous.change_stream import * # noqa: F403
19+
from pymongo.synchronous.change_stream import __doc__ as original_doc
20+
21+
__doc__ = original_doc
22+
__all__ = ["ChangeStream", "ClusterChangeStream", "CollectionChangeStream", "DatabaseChangeStream"] # noqa: F405

pymongo/synchronous/change_stream.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,7 @@
2222
from bson.raw_bson import RawBSONDocument
2323
from bson.timestamp import Timestamp
2424
from pymongo import _csot, common
25-
from pymongo.aggregation import (
26-
_AggregationCommand,
27-
_CollectionAggregationCommand,
28-
_DatabaseAggregationCommand,
29-
)
3025
from pymongo.collation import validate_collation_or_none
31-
from pymongo.command_cursor import CommandCursor
3226
from pymongo.errors import (
3327
ConnectionFailure,
3428
CursorNotFound,
@@ -37,8 +31,16 @@
3731
PyMongoError,
3832
)
3933
from pymongo.operations import _Op
34+
from pymongo.synchronous.aggregation import (
35+
_AggregationCommand,
36+
_CollectionAggregationCommand,
37+
_DatabaseAggregationCommand,
38+
)
39+
from pymongo.synchronous.command_cursor import CommandCursor
4040
from pymongo.typings import _CollationIn, _DocumentType, _Pipeline
4141

42+
_IS_SYNC = True
43+
4244
# The change streams spec considers the following server errors from the
4345
# getMore command non-resumable. All other getMore errors are resumable.
4446
_RESUMABLE_GETMORE_ERRORS = frozenset(
@@ -65,11 +67,11 @@
6567

6668

6769
if TYPE_CHECKING:
68-
from pymongo.client_session import ClientSession
69-
from pymongo.collection import Collection
70-
from pymongo.database import Database
71-
from pymongo.mongo_client import MongoClient
72-
from pymongo.pool import Connection
70+
from pymongo.synchronous.client_session import ClientSession
71+
from pymongo.synchronous.collection import Collection
72+
from pymongo.synchronous.database import Database
73+
from pymongo.synchronous.mongo_client import MongoClient
74+
from pymongo.synchronous.pool import Connection
7375

7476

7577
def _resumable(exc: PyMongoError) -> bool:
@@ -100,7 +102,9 @@ class ChangeStream(Generic[_DocumentType]):
100102
def __init__(
101103
self,
102104
target: Union[
103-
MongoClient[_DocumentType], Database[_DocumentType], Collection[_DocumentType]
105+
MongoClient[_DocumentType],
106+
Database[_DocumentType],
107+
Collection[_DocumentType],
104108
],
105109
pipeline: Optional[_Pipeline],
106110
full_document: Optional[str],
@@ -149,6 +153,8 @@ def __init__(
149153
self._closed = False
150154
self._timeout = self._target._timeout
151155
self._show_expanded_events = show_expanded_events
156+
157+
def _initialize_cursor(self) -> None:
152158
# Initialize cursor.
153159
self._cursor = self._create_cursor()
154160

@@ -180,7 +186,7 @@ def _change_stream_options(self) -> dict[str, Any]:
180186
else:
181187
options["resumeAfter"] = resume_token
182188

183-
if self._start_at_operation_time is not None:
189+
elif self._start_at_operation_time is not None:
184190
options["startAtOperationTime"] = self._start_at_operation_time
185191

186192
if self._show_expanded_events:

0 commit comments

Comments
 (0)