Skip to content

Commit 5edd9a0

Browse files
committed
SH review
1 parent 13f1a15 commit 5edd9a0

5 files changed

Lines changed: 14 additions & 29 deletions

File tree

pymongo/asynchronous/client_session.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,25 +182,18 @@
182182

183183
_IS_SYNC = False
184184

185-
_SESSION: ContextVar[Optional[_AsyncBoundClientSession]] = ContextVar("SESSION", default=None)
185+
_SESSION: ContextVar[Optional[AsyncClientSession]] = ContextVar("SESSION", default=None)
186186

187187

188-
class _AsyncBoundClientSession:
189-
def __init__(self, session: AsyncClientSession, client_id: int):
190-
self.session = session
191-
self.client_id = client_id
192-
193-
194-
class AsyncBoundSessionContext:
188+
class _AsyncBoundSessionContext:
195189
"""Context manager returned by AsyncClientSession.bind() that manages bound state."""
196190

197191
def __init__(self, session: AsyncClientSession) -> None:
198192
self._session = session
199-
self._session_token: Optional[Token[_AsyncBoundClientSession]] = None
193+
self._session_token: Optional[Token[AsyncClientSession]] = None
200194

201195
async def __aenter__(self) -> AsyncClientSession:
202-
bound_session = _AsyncBoundClientSession(self._session, id(self._session._client))
203-
self._session_token = _SESSION.set(bound_session) # type: ignore[assignment]
196+
self._session_token = _SESSION.set(self._session) # type: ignore[assignment]
204197
return self._session
205198

206199
async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
@@ -574,12 +567,12 @@ def _check_ended(self) -> None:
574567
if self._server_session is None:
575568
raise InvalidOperation("Cannot use ended session")
576569

577-
def bind(self) -> AsyncBoundSessionContext:
570+
def bind(self) -> _AsyncBoundSessionContext:
578571
"""Bind this session so it is implicitly passed to all database operations within the returned context.
579572
580573
.. versionadded:: 4.17
581574
"""
582-
return AsyncBoundSessionContext(self)
575+
return _AsyncBoundSessionContext(self)
583576

584577
async def __aenter__(self) -> AsyncClientSession:
585578
return self

pymongo/asynchronous/mongo_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,8 +2308,8 @@ async def _process_response(
23082308
def _get_bound_session(self) -> Optional[AsyncClientSession]:
23092309
bound_session = _SESSION.get()
23102310
if bound_session:
2311-
if bound_session.client_id == id(self):
2312-
return bound_session.session
2311+
if bound_session.client is self:
2312+
return bound_session
23132313
else:
23142314
raise InvalidOperation(
23152315
"Only the client that created the bound session can perform operations within its context block. See <PLACEHOLDER> for more information."

pymongo/synchronous/client_session.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,25 +181,18 @@
181181

182182
_IS_SYNC = True
183183

184-
_SESSION: ContextVar[Optional[_BoundClientSession]] = ContextVar("SESSION", default=None)
185-
186-
187-
class _BoundClientSession:
188-
def __init__(self, session: ClientSession, client_id: int):
189-
self.session = session
190-
self.client_id = client_id
184+
_SESSION: ContextVar[Optional[ClientSession]] = ContextVar("SESSION", default=None)
191185

192186

193187
class BoundSessionContext:
194188
"""Context manager returned by ClientSession.bind() that manages bound state."""
195189

196190
def __init__(self, session: ClientSession) -> None:
197191
self._session = session
198-
self._session_token: Optional[Token[_BoundClientSession]] = None
192+
self._session_token: Optional[Token[ClientSession]] = None
199193

200194
def __enter__(self) -> ClientSession:
201-
bound_session = _BoundClientSession(self._session, id(self._session._client))
202-
self._session_token = _SESSION.set(bound_session) # type: ignore[assignment]
195+
self._session_token = _SESSION.set(self._session) # type: ignore[assignment]
203196
return self._session
204197

205198
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:

pymongo/synchronous/mongo_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,8 +2302,8 @@ def _process_response(self, reply: Mapping[str, Any], session: Optional[ClientSe
23022302
def _get_bound_session(self) -> Optional[ClientSession]:
23032303
bound_session = _SESSION.get()
23042304
if bound_session:
2305-
if bound_session.client_id == id(self):
2306-
return bound_session.session
2305+
if bound_session.client is self:
2306+
return bound_session
23072307
else:
23082308
raise InvalidOperation(
23092309
"Only the client that created the bound session can perform operations within its context block. See <PLACEHOLDER> for more information."

tools/synchro.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
"AsyncRawBatchCursor": "RawBatchCursor",
3838
"AsyncRawBatchCommandCursor": "RawBatchCommandCursor",
3939
"AsyncClientSession": "ClientSession",
40-
"_AsyncBoundClientSession": "_BoundClientSession",
41-
"AsyncBoundSessionContext": "BoundSessionContext",
40+
"_AsyncBoundSessionContext": "BoundSessionContext",
4241
"AsyncChangeStream": "ChangeStream",
4342
"AsyncCollectionChangeStream": "CollectionChangeStream",
4443
"AsyncDatabaseChangeStream": "DatabaseChangeStream",

0 commit comments

Comments
 (0)