Skip to content

Commit 9f191d6

Browse files
authored
PYTHON-3283 Remove Generic Typing from the ClientSession Class (#952)
1 parent 89d3fd0 commit 9f191d6

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

pymongo/client_session.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@
140140
Any,
141141
Callable,
142142
ContextManager,
143-
Generic,
144143
Mapping,
145144
NoReturn,
146145
Optional,
@@ -164,7 +163,6 @@
164163
from pymongo.read_concern import ReadConcern
165164
from pymongo.read_preferences import ReadPreference, _ServerMode
166165
from pymongo.server_type import SERVER_TYPE
167-
from pymongo.typings import _DocumentType
168166
from pymongo.write_concern import WriteConcern
169167

170168

@@ -461,7 +459,7 @@ def _within_time_limit(start_time):
461459
from pymongo.mongo_client import MongoClient
462460

463461

464-
class ClientSession(Generic[_DocumentType]):
462+
class ClientSession:
465463
"""A session for ordering sequential operations.
466464
467465
:class:`ClientSession` instances are **not thread-safe or fork-safe**.
@@ -476,13 +474,13 @@ class ClientSession(Generic[_DocumentType]):
476474

477475
def __init__(
478476
self,
479-
client: "MongoClient[_DocumentType]",
477+
client: "MongoClient",
480478
server_session: Any,
481479
options: SessionOptions,
482480
implicit: bool,
483481
) -> None:
484482
# A MongoClient, a _ServerSession, a SessionOptions, and a set.
485-
self._client: MongoClient[_DocumentType] = client
483+
self._client: MongoClient = client
486484
self._server_session = server_session
487485
self._options = options
488486
self._cluster_time = None
@@ -515,14 +513,14 @@ def _check_ended(self):
515513
if self._server_session is None:
516514
raise InvalidOperation("Cannot use ended session")
517515

518-
def __enter__(self) -> "ClientSession[_DocumentType]":
516+
def __enter__(self) -> "ClientSession":
519517
return self
520518

521519
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
522520
self._end_session(lock=True)
523521

524522
@property
525-
def client(self) -> "MongoClient[_DocumentType]":
523+
def client(self) -> "MongoClient":
526524
"""The :class:`~pymongo.mongo_client.MongoClient` this session was
527525
created from.
528526
"""

pymongo/mongo_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,7 @@ def start_session(
16301630
causal_consistency: Optional[bool] = None,
16311631
default_transaction_options: Optional[client_session.TransactionOptions] = None,
16321632
snapshot: Optional[bool] = False,
1633-
) -> client_session.ClientSession[_DocumentType]:
1633+
) -> client_session.ClientSession:
16341634
"""Start a logical session.
16351635
16361636
This method takes the same parameters as
@@ -1681,7 +1681,7 @@ def _ensure_session(self, session=None):
16811681
@contextlib.contextmanager
16821682
def _tmp_session(
16831683
self, session: Optional[client_session.ClientSession], close: bool = True
1684-
) -> "Generator[Optional[client_session.ClientSession[Any]], None, None]":
1684+
) -> "Generator[Optional[client_session.ClientSession], None, None]":
16851685
"""If provided session is None, lend a temporary session."""
16861686
if session is not None:
16871687
if not isinstance(session, client_session.ClientSession):

test/test_mypy.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Movie(TypedDict): # type: ignore[misc]
4343
from bson import CodecOptions, decode, decode_all, decode_file_iter, decode_iter, encode
4444
from bson.raw_bson import RawBSONDocument
4545
from bson.son import SON
46+
from pymongo import ASCENDING
4647
from pymongo.collection import Collection
4748
from pymongo.mongo_client import MongoClient
4849
from pymongo.operations import InsertOne
@@ -313,6 +314,14 @@ def test_son_document_type(self) -> None:
313314
def test_son_document_type_runtime(self) -> None:
314315
client = MongoClient(document_class=SON[str, Any], connect=False)
315316

317+
@only_type_check
318+
def test_create_index(self) -> None:
319+
client: MongoClient[Dict[str, str]] = MongoClient("test")
320+
db = client.test
321+
with client.start_session() as session:
322+
index = db.test.create_index([("user_id", ASCENDING)], unique=True, session=session)
323+
assert isinstance(index, str)
324+
316325

317326
class TestCommandDocumentType(unittest.TestCase):
318327
@only_type_check

0 commit comments

Comments
 (0)