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 6 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
54 changes: 29 additions & 25 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 @@ -163,7 +164,7 @@ def get(self, file_id: Any, session: Optional[ClientSession] = None) -> GridOut:

:param file_id: ``"_id"`` of the file to get
:param session: a
:class:`~pymongo.client_session.ClientSession`
:class:`~pymongo.client_session.AsyncClientSession`
ShaneHarvey marked this conversation as resolved.
Show resolved Hide resolved

.. versionchanged:: 3.6
Added ``session`` parameter.
Expand Down Expand Up @@ -205,7 +206,7 @@ def get_version(
:param version: version of the file to get (defaults
to -1, the most recent version uploaded)
:param session: a
:class:`~pymongo.client_session.ClientSession`
:class:`~pymongo.client_session.AsyncClientSession`
:param kwargs: find files by custom metadata.

.. versionchanged:: 3.6
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 All @@ -244,7 +248,7 @@ def get_last_version(

:param filename: ``"filename"`` of the file to get, or `None`
:param session: a
:class:`~pymongo.client_session.ClientSession`
:class:`~pymongo.client_session.AsyncClientSession`
:param kwargs: find files by custom metadata.

.. versionchanged:: 3.6
Expand All @@ -269,7 +273,7 @@ def delete(self, file_id: Any, session: Optional[ClientSession] = None) -> None:

:param file_id: ``"_id"`` of the file to delete
:param session: a
:class:`~pymongo.client_session.ClientSession`
:class:`~pymongo.client_session.AsyncClientSession`

.. versionchanged:: 3.6
Added ``session`` parameter.
Expand All @@ -286,7 +290,7 @@ def list(self, session: Optional[ClientSession] = None) -> list[str]:
:class:`GridFS`.

:param session: a
:class:`~pymongo.client_session.ClientSession`
:class:`~pymongo.client_session.AsyncClientSession`

.. versionchanged:: 3.6
Added ``session`` parameter.
Expand Down Expand Up @@ -325,7 +329,7 @@ def find_one(
:param args: any additional positional arguments are
the same as the arguments to :meth:`find`.
:param session: a
:class:`~pymongo.client_session.ClientSession`
:class:`~pymongo.client_session.AsyncClientSession`
:param kwargs: any additional keyword arguments
are the same as the arguments to :meth:`find`.

Expand Down Expand Up @@ -368,7 +372,7 @@ def find(self, *args: Any, **kwargs: Any) -> GridOutCursor:
:meth:`~pymongo.collection.Collection.find`
in :class:`~pymongo.collection.Collection`.

If a :class:`~pymongo.client_session.ClientSession` is passed to
If a :class:`~pymongo.client_session.AsyncClientSession` is passed to
:meth:`find`, all returned :class:`~gridfs.grid_file.GridOut` instances
are associated with that session.

Expand Down Expand Up @@ -436,7 +440,7 @@ def exists(
:param document_or_id: query document, or _id of the
document to check for
:param session: a
:class:`~pymongo.client_session.ClientSession`
:class:`~pymongo.client_session.AsyncClientSession`
:param kwargs: keyword arguments are used as a
query document, if they're present.

Expand Down Expand Up @@ -554,7 +558,7 @@ def open_upload_stream(
files collection document. If not provided the metadata field will
be omitted from the files collection document.
:param session: a
:class:`~pymongo.client_session.ClientSession`
:class:`~pymongo.client_session.AsyncClientSession`

.. versionchanged:: 3.6
Added ``session`` parameter.
Expand Down Expand Up @@ -613,7 +617,7 @@ def open_upload_stream_with_id(
files collection document. If not provided the metadata field will
be omitted from the files collection document.
:param session: a
:class:`~pymongo.client_session.ClientSession`
:class:`~pymongo.client_session.AsyncClientSession`

.. versionchanged:: 3.6
Added ``session`` parameter.
Expand Down Expand Up @@ -670,7 +674,7 @@ def upload_from_stream(
files collection document. If not provided the metadata field will
be omitted from the files collection document.
:param session: a
:class:`~pymongo.client_session.ClientSession`
:class:`~pymongo.client_session.AsyncClientSession`

.. versionchanged:: 3.6
Added ``session`` parameter.
Expand Down Expand Up @@ -720,7 +724,7 @@ def upload_from_stream_with_id(
files collection document. If not provided the metadata field will
be omitted from the files collection document.
:param session: a
:class:`~pymongo.client_session.ClientSession`
:class:`~pymongo.client_session.AsyncClientSession`

.. versionchanged:: 3.6
Added ``session`` parameter.
Expand Down Expand Up @@ -751,7 +755,7 @@ def open_download_stream(

:param file_id: The _id of the file to be downloaded.
:param session: a
:class:`~pymongo.client_session.ClientSession`
:class:`~pymongo.client_session.AsyncClientSession`

.. versionchanged:: 3.6
Added ``session`` parameter.
Expand Down Expand Up @@ -786,7 +790,7 @@ def download_to_stream(
:param file_id: The _id of the file to be downloaded.
:param destination: a file-like object implementing :meth:`write`.
:param session: a
:class:`~pymongo.client_session.ClientSession`
:class:`~pymongo.client_session.AsyncClientSession`

.. versionchanged:: 3.6
Added ``session`` parameter.
Expand Down Expand Up @@ -815,7 +819,7 @@ def delete(self, file_id: Any, session: Optional[ClientSession] = None) -> None:

:param file_id: The _id of the file to be deleted.
:param session: a
:class:`~pymongo.client_session.ClientSession`
:class:`~pymongo.client_session.AsyncClientSession`

.. versionchanged:: 3.6
Added ``session`` parameter.
Expand Down Expand Up @@ -855,7 +859,7 @@ def find(self, *args: Any, **kwargs: Any) -> GridOutCursor:
:meth:`~pymongo.collection.Collection.find`
in :class:`~pymongo.collection.Collection`.

If a :class:`~pymongo.client_session.ClientSession` is passed to
If a :class:`~pymongo.client_session.AsyncClientSession` is passed to
:meth:`find`, all returned :class:`~gridfs.grid_file.GridOut` instances
are associated with that session.

Expand Down Expand Up @@ -898,7 +902,7 @@ def open_download_stream_by_name(
filename and different uploadDate) of the file to retrieve.
Defaults to -1 (the most recent revision).
:param session: a
:class:`~pymongo.client_session.ClientSession`
:class:`~pymongo.client_session.AsyncClientSession`

:Note: Revision numbers are defined as follows:

Expand Down Expand Up @@ -957,7 +961,7 @@ def download_to_stream_by_name(
filename and different uploadDate) of the file to retrieve.
Defaults to -1 (the most recent revision).
:param session: a
:class:`~pymongo.client_session.ClientSession`
:class:`~pymongo.client_session.AsyncClientSession`

:Note: Revision numbers are defined as follows:

Expand Down Expand Up @@ -996,7 +1000,7 @@ def rename(
:param file_id: The _id of the file to be renamed.
:param new_filename: The new name of the file.
:param session: a
:class:`~pymongo.client_session.ClientSession`
:class:`~pymongo.client_session.AsyncClientSession`

.. versionchanged:: 3.6
Added ``session`` parameter.
Expand Down Expand Up @@ -1053,7 +1057,7 @@ def __init__(

:param root_collection: root collection to write to
:param session: a
:class:`~pymongo.client_session.ClientSession` to use for all
:class:`~pymongo.client_session.AsyncClientSession` to use for all
commands
:param kwargs: Any: file level options (see above)

Expand Down Expand Up @@ -1408,7 +1412,7 @@ def __init__(
:param file_document: file document from
`root_collection.files`
:param session: a
:class:`~pymongo.client_session.ClientSession` to use for all
:class:`~pymongo.client_session.AsyncClientSession` to use for all
commands

.. versionchanged:: 3.8
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