PYTHON-3283 Remove Generic Typing from the ClientSession Class (#952)

This commit is contained in:
Steven Silvester 2022-05-25 05:55:36 -05:00 committed by GitHub
parent 89d3fd0355
commit 9f191d6bb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 9 deletions

View File

@ -140,7 +140,6 @@ from typing import (
Any,
Callable,
ContextManager,
Generic,
Mapping,
NoReturn,
Optional,
@ -164,7 +163,6 @@ from pymongo.helpers import _RETRYABLE_ERROR_CODES
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
@ -461,7 +459,7 @@ if TYPE_CHECKING:
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**.
@ -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
@ -515,14 +513,14 @@ class ClientSession(Generic[_DocumentType]):
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.
"""

View File

@ -1630,7 +1630,7 @@ class MongoClient(common.BaseObject, Generic[_DocumentType]):
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
@ -1681,7 +1681,7 @@ class MongoClient(common.BaseObject, Generic[_DocumentType]):
@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):

View File

@ -43,6 +43,7 @@ from test.utils import rs_or_single_client
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
@ -313,6 +314,14 @@ class TestDocumentType(unittest.TestCase):
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:
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