Skip to content

Commit e090035

Browse files
committed
Make session var private
1 parent ebb1d62 commit e090035

File tree

8 files changed

+32
-28
lines changed

8 files changed

+32
-28
lines changed

pymongo/asynchronous/client_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ def __copy__(self) -> NoReturn:
10681068
raise TypeError("A AsyncClientSession cannot be copied, create a new session instead")
10691069

10701070

1071-
SESSION: ContextVar[Optional[AsyncClientSession]] = ContextVar("SESSION", default=None)
1071+
_SESSION: ContextVar[Optional[AsyncClientSession]] = ContextVar("SESSION", default=None)
10721072

10731073

10741074
class _EmptyServerSession:

pymongo/asynchronous/cursor.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
from _typeshed import SupportsItems
6464

6565
from bson.codec_options import CodecOptions
66-
from pymongo.asynchronous.client_session import SESSION, AsyncClientSession
66+
from pymongo.asynchronous.client_session import AsyncClientSession
6767
from pymongo.asynchronous.collection import AsyncCollection
6868
from pymongo.asynchronous.pool import AsyncConnection
6969
from pymongo.read_preferences import _ServerMode
@@ -136,13 +136,15 @@ def __init__(
136136
self._killed = False
137137
self._session: Optional[AsyncClientSession]
138138

139-
_SESSION = SESSION.get()
139+
from .client_session import _SESSION
140+
141+
bound_session = _SESSION.get()
140142

141143
if session:
142144
self._session = session
143145
self._explicit_session = True
144-
elif _SESSION:
145-
self._session = _SESSION
146+
elif bound_session:
147+
self._session = bound_session
146148
self._explicit_session = True
147149
else:
148150
self._session = None

pymongo/asynchronous/mongo_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
from pymongo.asynchronous import client_session, database, uri_parser
6666
from pymongo.asynchronous.change_stream import AsyncChangeStream, AsyncClusterChangeStream
6767
from pymongo.asynchronous.client_bulk import _AsyncClientBulk
68-
from pymongo.asynchronous.client_session import SESSION, _EmptyServerSession
68+
from pymongo.asynchronous.client_session import _SESSION, _EmptyServerSession
6969
from pymongo.asynchronous.command_cursor import AsyncCommandCursor
7070
from pymongo.asynchronous.settings import TopologySettings
7171
from pymongo.asynchronous.topology import Topology, _ErrorContext
@@ -1358,7 +1358,7 @@ def _start_session(self, implicit: bool, **kwargs: Any) -> AsyncClientSession:
13581358
bind = opts._bind
13591359
session = client_session.AsyncClientSession(self, server_session, opts, implicit)
13601360
if bind:
1361-
SESSION.set(session)
1361+
_SESSION.set(session)
13621362
return session
13631363

13641364
def start_session(

pymongo/synchronous/client_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ def __copy__(self) -> NoReturn:
10631063
raise TypeError("A ClientSession cannot be copied, create a new session instead")
10641064

10651065

1066-
SESSION: ContextVar[Optional[ClientSession]] = ContextVar("SESSION", default=None)
1066+
_SESSION: ContextVar[Optional[ClientSession]] = ContextVar("SESSION", default=None)
10671067

10681068

10691069
class _EmptyServerSession:

pymongo/synchronous/cursor.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464

6565
from bson.codec_options import CodecOptions
6666
from pymongo.read_preferences import _ServerMode
67-
from pymongo.synchronous.client_session import SESSION, ClientSession
67+
from pymongo.synchronous.client_session import ClientSession
6868
from pymongo.synchronous.collection import Collection
6969
from pymongo.synchronous.pool import Connection
7070

@@ -136,13 +136,15 @@ def __init__(
136136
self._killed = False
137137
self._session: Optional[ClientSession]
138138

139-
_SESSION = SESSION.get()
139+
from .client_session import _SESSION
140+
141+
bound_session = _SESSION.get()
140142

141143
if session:
142144
self._session = session
143145
self._explicit_session = True
144-
elif _SESSION:
145-
self._session = _SESSION
146+
elif bound_session:
147+
self._session = bound_session
146148
self._explicit_session = True
147149
else:
148150
self._session = None

pymongo/synchronous/mongo_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
from pymongo.synchronous import client_session, database, uri_parser
108108
from pymongo.synchronous.change_stream import ChangeStream, ClusterChangeStream
109109
from pymongo.synchronous.client_bulk import _ClientBulk
110-
from pymongo.synchronous.client_session import SESSION, _EmptyServerSession
110+
from pymongo.synchronous.client_session import _SESSION, _EmptyServerSession
111111
from pymongo.synchronous.command_cursor import CommandCursor
112112
from pymongo.synchronous.settings import TopologySettings
113113
from pymongo.synchronous.topology import Topology, _ErrorContext
@@ -1356,7 +1356,7 @@ def _start_session(self, implicit: bool, **kwargs: Any) -> ClientSession:
13561356
bind = opts._bind
13571357
session = client_session.ClientSession(self, server_session, opts, implicit)
13581358
if bind:
1359-
SESSION.set(session)
1359+
_SESSION.set(session)
13601360
return session
13611361

13621362
def start_session(

test/asynchronous/test_session.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,13 +380,6 @@ async def test_cursor_clone(self):
380380
clone = cursor.clone()
381381
self.assertTrue(clone.session is s)
382382

383-
# Explicit session via context variable.
384-
async with self.client.start_session(bind=True) as s:
385-
cursor = coll.find()
386-
self.assertTrue(cursor.session is s)
387-
clone = cursor.clone()
388-
self.assertTrue(clone.session is s)
389-
390383
# No explicit session.
391384
cursor = coll.find(batch_size=2)
392385
await anext(cursor)
@@ -401,6 +394,13 @@ async def test_cursor_clone(self):
401394
await cursor.close()
402395
await clone.close()
403396

397+
# Explicit session via context variable.
398+
async with self.client.start_session(bind=True) as s:
399+
cursor = coll.find()
400+
self.assertTrue(cursor.session is s)
401+
clone = cursor.clone()
402+
self.assertTrue(clone.session is s)
403+
404404
async def test_cursor(self):
405405
listener = self.listener
406406
client = self.client

test/test_session.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,13 +380,6 @@ def test_cursor_clone(self):
380380
clone = cursor.clone()
381381
self.assertTrue(clone.session is s)
382382

383-
# Explicit session via context variable.
384-
with self.client.start_session(bind=True) as s:
385-
cursor = coll.find()
386-
self.assertTrue(cursor.session is s)
387-
clone = cursor.clone()
388-
self.assertTrue(clone.session is s)
389-
390383
# No explicit session.
391384
cursor = coll.find(batch_size=2)
392385
next(cursor)
@@ -401,6 +394,13 @@ def test_cursor_clone(self):
401394
cursor.close()
402395
clone.close()
403396

397+
# Explicit session via context variable.
398+
with self.client.start_session(bind=True) as s:
399+
cursor = coll.find()
400+
self.assertTrue(cursor.session is s)
401+
clone = cursor.clone()
402+
self.assertTrue(clone.session is s)
403+
404404
def test_cursor(self):
405405
listener = self.listener
406406
client = self.client

0 commit comments

Comments
 (0)