Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions pymongo/client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@
Any,
Callable,
ContextManager,
Generic,
Mapping,
NoReturn,
Optional,
Expand All @@ -164,7 +163,6 @@
from pymongo.read_concern import ReadConcern
from pymongo.read_preferences import ReadPreference, _ServerMode
from pymongo.server_type import SERVER_TYPE
from pymongo.typings import _DocumentType
from pymongo.write_concern import WriteConcern


Expand Down Expand Up @@ -461,7 +459,7 @@ def _within_time_limit(start_time):
from pymongo.mongo_client import MongoClient


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

def __init__(
self,
client: "MongoClient[_DocumentType]",
client: "MongoClient",
server_session: Any,
options: SessionOptions,
implicit: bool,
) -> None:
# A MongoClient, a _ServerSession, a SessionOptions, and a set.
self._client: MongoClient[_DocumentType] = client
self._client: MongoClient = client
self._server_session = server_session
self._options = options
self._cluster_time = None
Expand Down Expand Up @@ -515,14 +513,14 @@ def _check_ended(self):
if self._server_session is None:
raise InvalidOperation("Cannot use ended session")

def __enter__(self) -> "ClientSession[_DocumentType]":
def __enter__(self) -> "ClientSession":
return self

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

@property
def client(self) -> "MongoClient[_DocumentType]":
def client(self) -> "MongoClient":
"""The :class:`~pymongo.mongo_client.MongoClient` this session was
created from.
"""
Expand Down
4 changes: 2 additions & 2 deletions pymongo/mongo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,7 @@ def start_session(
causal_consistency: Optional[bool] = None,
default_transaction_options: Optional[client_session.TransactionOptions] = None,
snapshot: Optional[bool] = False,
) -> client_session.ClientSession[_DocumentType]:
) -> client_session.ClientSession:
"""Start a logical session.
This method takes the same parameters as
Expand Down Expand Up @@ -1681,7 +1681,7 @@ def _ensure_session(self, session=None):
@contextlib.contextmanager
def _tmp_session(
self, session: Optional[client_session.ClientSession], close: bool = True
) -> "Generator[Optional[client_session.ClientSession[Any]], None, None]":
) -> "Generator[Optional[client_session.ClientSession], None, None]":
"""If provided session is None, lend a temporary session."""
if session is not None:
if not isinstance(session, client_session.ClientSession):
Expand Down
9 changes: 9 additions & 0 deletions test/test_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Movie(TypedDict): # type: ignore[misc]
from bson import CodecOptions, decode, decode_all, decode_file_iter, decode_iter, encode
from bson.raw_bson import RawBSONDocument
from bson.son import SON
from pymongo import ASCENDING
from pymongo.collection import Collection
from pymongo.mongo_client import MongoClient
from pymongo.operations import InsertOne
Expand Down Expand Up @@ -313,6 +314,14 @@ def test_son_document_type(self) -> None:
def test_son_document_type_runtime(self) -> None:
client = MongoClient(document_class=SON[str, Any], connect=False)

@only_type_check
def test_create_index(self) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this test run create_index instead of a transaction?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

client: MongoClient[Dict[str, str]] = MongoClient("test")
db = client.test
with client.start_session() as session:
index = db.test.create_index([("user_id", ASCENDING)], unique=True, session=session)
assert isinstance(index, str)


class TestCommandDocumentType(unittest.TestCase):
@only_type_check
Expand Down