Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PYTHON-4476 Separate data and IO classes more effectively #1678

Merged
merged 16 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
104 changes: 55 additions & 49 deletions gridfs/asynchronous/grid_file.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gridfs/grid_file_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Any, Optional

from pymongo import ASCENDING
from pymongo.asynchronous.common import MAX_MESSAGE_SIZE
from pymongo.common import MAX_MESSAGE_SIZE
from pymongo.errors import InvalidOperation

_SEEK_SET = os.SEEK_SET
Expand Down
30 changes: 17 additions & 13 deletions gridfs/synchronous/grid_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
_grid_out_property,
)
from pymongo import ASCENDING, DESCENDING, WriteConcern, _csot
from pymongo.common import validate_string
from pymongo.errors import (
BulkWriteError,
ConfigurationError,
Expand All @@ -50,13 +51,13 @@
InvalidOperation,
OperationFailure,
)
from pymongo.helpers_shared import _check_write_command_response
from pymongo.read_preferences import ReadPreference, _ServerMode
from pymongo.synchronous.client_session import ClientSession
from pymongo.synchronous.collection import Collection
from pymongo.synchronous.common import validate_string
from pymongo.synchronous.cursor import Cursor
from pymongo.synchronous.database import Database
from pymongo.synchronous.helpers import _check_write_command_response, next
from pymongo.synchronous.read_preferences import ReadPreference, _ServerMode
from pymongo.synchronous.helpers import next

_IS_SYNC = True

Expand Down Expand Up @@ -234,7 +235,10 @@ def get_version(
raise NoFile("no version %d for filename %r" % (version, filename)) from None

def get_last_version(
self, filename: Optional[str] = None, session: Optional[ClientSession] = None, **kwargs: Any
self,
filename: Optional[str] = None,
session: Optional[ClientSession] = None,
**kwargs: Any,
) -> GridOut:
"""Get the most recent version of a file in GridFS by ``"filename"``
or metadata fields.
Expand Down Expand Up @@ -497,7 +501,7 @@ def __init__(
.. seealso:: The MongoDB documentation on `gridfs <https://dochub.mongodb.org/core/gridfs>`_.
"""
if not isinstance(db, Database):
raise TypeError("database must be an instance of AsyncDatabase")
raise TypeError("database must be an instance of Database")

db = _clear_entity_type_registry(db)

Expand Down Expand Up @@ -1028,7 +1032,7 @@ def __init__(
provided by :class:`~gridfs.GridFS`.

Raises :class:`TypeError` if `root_collection` is not an
instance of :class:`~pymongo.collection.AsyncCollection`.
instance of :class:`~pymongo.collection.Collection`.

Any of the file level options specified in the `GridFS Spec
<http://dochub.mongodb.org/core/gridfsspec>`_ may be passed as
Expand Down Expand Up @@ -1069,10 +1073,10 @@ def __init__(

.. versionchanged:: 3.0
`root_collection` must use an acknowledged
:attr:`~pymongo.collection.AsyncCollection.write_concern`
:attr:`~pymongo.collection.Collection.write_concern`
"""
if not isinstance(root_collection, Collection):
raise TypeError("root_collection must be an instance of AsyncCollection")
raise TypeError("root_collection must be an instance of Collection")

if not root_collection.write_concern.acknowledged:
raise ConfigurationError("root_collection must use acknowledged write_concern")
Expand Down Expand Up @@ -1401,7 +1405,7 @@ def __init__(
Either `file_id` or `file_document` must be specified,
`file_document` will be given priority if present. Raises
:class:`TypeError` if `root_collection` is not an instance of
:class:`~pymongo.collection.AsyncCollection`.
:class:`~pymongo.collection.Collection`.

:param root_collection: root collection to read from
:param file_id: value of ``"_id"`` for the file to read
Expand All @@ -1424,7 +1428,7 @@ def __init__(
from the server. Metadata is fetched when first needed.
"""
if not isinstance(root_collection, Collection):
raise TypeError("root_collection must be an instance of AsyncCollection")
raise TypeError("root_collection must be an instance of Collection")
_disallow_transactions(session)

root_collection = _clear_entity_type_registry(root_collection)
Expand Down Expand Up @@ -1482,7 +1486,7 @@ def __getattr__(self, name: str) -> Any:
self.open() # type: ignore[unused-coroutine]
elif not self._file:
raise InvalidOperation(
"You must call AsyncGridOut.open() before accessing the %s property" % name
"You must call GridOut.open() before accessing the %s property" % name
)
if name in self._file:
return self._file[name]
Expand Down Expand Up @@ -1677,13 +1681,13 @@ def writable(self) -> bool:
return False

def __enter__(self) -> GridOut:
"""Makes it possible to use :class:`AsyncGridOut` files
"""Makes it possible to use :class:`GridOut` files
with the async context manager protocol.
"""
return self

def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> Any:
"""Makes it possible to use :class:`AsyncGridOut` files
"""Makes it possible to use :class:`GridOut` files
with the async context manager protocol.
"""
self.close()
Expand Down
10 changes: 5 additions & 5 deletions pymongo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,9 @@
from pymongo import _csot
from pymongo._version import __version__, get_version_string, version_tuple
from pymongo.asynchronous.mongo_client import AsyncMongoClient
from pymongo.common import MAX_SUPPORTED_WIRE_VERSION, MIN_SUPPORTED_WIRE_VERSION
from pymongo.cursor import CursorType
from pymongo.synchronous.collection import ReturnDocument
from pymongo.synchronous.common import MAX_SUPPORTED_WIRE_VERSION, MIN_SUPPORTED_WIRE_VERSION
from pymongo.synchronous.mongo_client import MongoClient
from pymongo.synchronous.operations import (
from pymongo.operations import (
DeleteMany,
DeleteOne,
IndexModel,
Expand All @@ -102,7 +100,9 @@
UpdateMany,
UpdateOne,
)
from pymongo.synchronous.read_preferences import ReadPreference
from pymongo.read_preferences import ReadPreference
from pymongo.synchronous.collection import ReturnDocument
from pymongo.synchronous.mongo_client import MongoClient
from pymongo.write_concern import WriteConcern

version = __version__
Expand Down
22 changes: 11 additions & 11 deletions pymongo/asynchronous/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
from collections.abc import Callable, Mapping, MutableMapping
from typing import TYPE_CHECKING, Any, Optional, Union

from pymongo.asynchronous import common
from pymongo.asynchronous.collation import validate_collation_or_none
from pymongo.asynchronous.read_preferences import ReadPreference, _AggWritePref
from pymongo import common
from pymongo.collation import validate_collation_or_none
from pymongo.errors import ConfigurationError
from pymongo.read_preferences import ReadPreference, _AggWritePref

if TYPE_CHECKING:
from pymongo.asynchronous.client_session import ClientSession
from pymongo.asynchronous.client_session import AsyncClientSession
from pymongo.asynchronous.collection import AsyncCollection
from pymongo.asynchronous.command_cursor import AsyncCommandCursor
from pymongo.asynchronous.database import AsyncDatabase
from pymongo.asynchronous.pool import Connection
from pymongo.asynchronous.read_preferences import _ServerMode
from pymongo.asynchronous.pool import AsyncConnection
from pymongo.asynchronous.server import Server
from pymongo.asynchronous.typings import _DocumentType, _Pipeline
from pymongo.read_preferences import _ServerMode
from pymongo.typings import _DocumentType, _Pipeline

_IS_SYNC = False

Expand All @@ -53,7 +53,7 @@ def __init__(
explicit_session: bool,
let: Optional[Mapping[str, Any]] = None,
user_fields: Optional[MutableMapping[str, Any]] = None,
result_processor: Optional[Callable[[Mapping[str, Any], Connection], None]] = None,
result_processor: Optional[Callable[[Mapping[str, Any], AsyncConnection], None]] = None,
comment: Any = None,
) -> None:
if "explain" in options:
Expand Down Expand Up @@ -121,7 +121,7 @@ def _database(self) -> AsyncDatabase:
raise NotImplementedError

def get_read_preference(
self, session: Optional[ClientSession]
self, session: Optional[AsyncClientSession]
) -> Union[_AggWritePref, _ServerMode]:
if self._write_preference:
return self._write_preference
Expand All @@ -132,9 +132,9 @@ def get_read_preference(

async def get_cursor(
self,
session: Optional[ClientSession],
session: Optional[AsyncClientSession],
server: Server,
conn: Connection,
conn: AsyncConnection,
read_preference: _ServerMode,
) -> AsyncCommandCursor[_DocumentType]:
# Serialize command.
Expand Down
Loading
Loading