Skip to content

PYTHON-3737 Use __future__ annotations for forward reference type hints #1234

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

Merged
merged 2 commits into from
Jun 15, 2023
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
5 changes: 3 additions & 2 deletions pymongo/change_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# permissions and limitations under the License.

"""Watch changes on a collection, a database, or the entire cluster."""
from __future__ import annotations

import copy
from typing import TYPE_CHECKING, Any, Dict, Generic, Mapping, Optional, Union
Expand Down Expand Up @@ -96,7 +97,7 @@ class ChangeStream(Generic[_DocumentType]):
def __init__(
self,
target: Union[
"MongoClient[_DocumentType]", "Database[_DocumentType]", "Collection[_DocumentType]"
Copy link
Member

Choose a reason for hiding this comment

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

Is this allowed even though the MongoClient import is under if TYPE_CHECKING:?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, because it is converted to a string before it is interpreted.

Copy link
Member

Choose a reason for hiding this comment

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

Neat!

MongoClient[_DocumentType], Database[_DocumentType], Collection[_DocumentType]
],
pipeline: Optional[_Pipeline],
full_document: Optional[str],
Expand All @@ -105,7 +106,7 @@ def __init__(
batch_size: Optional[int],
collation: Optional[_CollationIn],
start_at_operation_time: Optional[Timestamp],
session: Optional["ClientSession"],
session: Optional[ClientSession],
start_after: Optional[Mapping[str, Any]],
comment: Optional[Any] = None,
full_document_before_change: Optional[str] = None,
Expand Down
6 changes: 4 additions & 2 deletions pymongo/client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@
=======
"""

from __future__ import annotations

import collections
import time
import uuid
Expand Down Expand Up @@ -478,7 +480,7 @@ class ClientSession:

def __init__(
self,
client: "MongoClient",
client: MongoClient,
server_session: Any,
options: SessionOptions,
implicit: bool,
Expand Down Expand Up @@ -524,7 +526,7 @@ def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
self._end_session(lock=True)

@property
def client(self) -> "MongoClient":
def client(self) -> MongoClient:
"""The :class:`~pymongo.mongo_client.MongoClient` this session was
created from.
"""
Expand Down
71 changes: 36 additions & 35 deletions pymongo/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

"""Collection level utilities for Mongo."""
from __future__ import annotations

from collections import abc
from typing import (
Expand Down Expand Up @@ -114,14 +115,14 @@ class Collection(common.BaseObject, Generic[_DocumentType]):

def __init__(
self,
database: "Database[_DocumentType]",
database: Database[_DocumentType],
name: str,
create: Optional[bool] = False,
codec_options: Optional["CodecOptions[_DocumentTypeArg]"] = None,
codec_options: Optional[CodecOptions[_DocumentTypeArg]] = None,
read_preference: Optional[_ServerMode] = None,
write_concern: Optional[WriteConcern] = None,
read_concern: Optional["ReadConcern"] = None,
session: Optional["ClientSession"] = None,
read_concern: Optional[ReadConcern] = None,
session: Optional[ClientSession] = None,
**kwargs: Any,
) -> None:
"""Get / create a Mongo collection.
Expand Down Expand Up @@ -335,7 +336,7 @@ def __create(
session=session,
)

def __getattr__(self, name: str) -> "Collection[_DocumentType]":
def __getattr__(self, name: str) -> Collection[_DocumentType]:
"""Get a sub-collection of this collection by name.

Raises InvalidName if an invalid collection name is used.
Expand All @@ -351,7 +352,7 @@ def __getattr__(self, name: str) -> "Collection[_DocumentType]":
)
return self.__getitem__(name)

def __getitem__(self, name: str) -> "Collection[_DocumentType]":
def __getitem__(self, name: str) -> Collection[_DocumentType]:
return Collection(
self.__database,
f"{self.__name}.{name}",
Expand Down Expand Up @@ -397,19 +398,19 @@ def name(self) -> str:
return self.__name

@property
def database(self) -> "Database[_DocumentType]":
def database(self) -> Database[_DocumentType]:
"""The :class:`~pymongo.database.Database` that this
:class:`Collection` is a part of.
"""
return self.__database

def with_options(
self,
codec_options: Optional["bson.CodecOptions[_DocumentTypeArg]"] = None,
codec_options: Optional[bson.CodecOptions[_DocumentTypeArg]] = None,
read_preference: Optional[_ServerMode] = None,
write_concern: Optional[WriteConcern] = None,
read_concern: Optional["ReadConcern"] = None,
) -> "Collection[_DocumentType]":
read_concern: Optional[ReadConcern] = None,
) -> Collection[_DocumentType]:
"""Get a clone of this collection changing the specified settings.

>>> coll1.read_preference
Expand Down Expand Up @@ -455,7 +456,7 @@ def bulk_write(
requests: Sequence[_WriteOp[_DocumentType]],
ordered: bool = True,
bypass_document_validation: bool = False,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
let: Optional[Mapping] = None,
) -> BulkWriteResult:
Expand Down Expand Up @@ -585,7 +586,7 @@ def insert_one(
self,
document: Union[_DocumentType, RawBSONDocument],
bypass_document_validation: bool = False,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
) -> InsertOneResult:
"""Insert a single document.
Expand Down Expand Up @@ -653,7 +654,7 @@ def insert_many(
documents: Iterable[Union[_DocumentType, RawBSONDocument]],
ordered: bool = True,
bypass_document_validation: bool = False,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
) -> InsertManyResult:
"""Insert an iterable of documents.
Expand Down Expand Up @@ -855,7 +856,7 @@ def replace_one(
bypass_document_validation: bool = False,
collation: Optional[_CollationIn] = None,
hint: Optional[_IndexKeyHint] = None,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
let: Optional[Mapping[str, Any]] = None,
comment: Optional[Any] = None,
) -> UpdateResult:
Expand Down Expand Up @@ -959,7 +960,7 @@ def update_one(
collation: Optional[_CollationIn] = None,
array_filters: Optional[Sequence[Mapping[str, Any]]] = None,
hint: Optional[_IndexKeyHint] = None,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
let: Optional[Mapping[str, Any]] = None,
comment: Optional[Any] = None,
) -> UpdateResult:
Expand Down Expand Up @@ -1073,7 +1074,7 @@ def update_many(
bypass_document_validation: Optional[bool] = None,
collation: Optional[_CollationIn] = None,
hint: Optional[_IndexKeyHint] = None,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
let: Optional[Mapping[str, Any]] = None,
comment: Optional[Any] = None,
) -> UpdateResult:
Expand Down Expand Up @@ -1168,7 +1169,7 @@ def update_many(

def drop(
self,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
encrypted_fields: Optional[Mapping[str, Any]] = None,
) -> None:
Expand Down Expand Up @@ -1306,7 +1307,7 @@ def delete_one(
filter: Mapping[str, Any],
collation: Optional[_CollationIn] = None,
hint: Optional[_IndexKeyHint] = None,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
let: Optional[Mapping[str, Any]] = None,
comment: Optional[Any] = None,
) -> DeleteResult:
Expand Down Expand Up @@ -1373,7 +1374,7 @@ def delete_many(
filter: Mapping[str, Any],
collation: Optional[_CollationIn] = None,
hint: Optional[_IndexKeyHint] = None,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
let: Optional[Mapping[str, Any]] = None,
comment: Optional[Any] = None,
) -> DeleteResult:
Expand Down Expand Up @@ -1769,7 +1770,7 @@ def _cmd(session, server, sock_info, read_preference):
def count_documents(
self,
filter: Mapping[str, Any],
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
**kwargs: Any,
) -> int:
Expand Down Expand Up @@ -1860,7 +1861,7 @@ def _retryable_non_cursor_read(self, func, session):
def create_indexes(
self,
indexes: Sequence[IndexModel],
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
**kwargs: Any,
) -> List[str]:
Expand Down Expand Up @@ -1952,7 +1953,7 @@ def gen_indexes():
def create_index(
self,
keys: _IndexKeyHint,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
**kwargs: Any,
) -> str:
Expand Down Expand Up @@ -2071,7 +2072,7 @@ def create_index(

def drop_indexes(
self,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
**kwargs: Any,
) -> None:
Expand Down Expand Up @@ -2107,7 +2108,7 @@ def drop_indexes(
def drop_index(
self,
index_or_name: _IndexKeyHint,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
**kwargs: Any,
) -> None:
Expand Down Expand Up @@ -2174,7 +2175,7 @@ def drop_index(

def list_indexes(
self,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
) -> CommandCursor[MutableMapping[str, Any]]:
"""Get a cursor over the index documents for this collection.
Expand Down Expand Up @@ -2239,7 +2240,7 @@ def _cmd(session, server, sock_info, read_preference):

def index_information(
self,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
) -> MutableMapping[str, Any]:
"""Get information on this collection's indexes.
Expand Down Expand Up @@ -2282,7 +2283,7 @@ def index_information(

def options(
self,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
) -> MutableMapping[str, Any]:
"""Get the options set on this collection.
Expand Down Expand Up @@ -2361,7 +2362,7 @@ def _aggregate(
def aggregate(
self,
pipeline: _Pipeline,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
let: Optional[Mapping[str, Any]] = None,
comment: Optional[Any] = None,
**kwargs: Any,
Expand Down Expand Up @@ -2458,7 +2459,7 @@ def aggregate(
def aggregate_raw_batches(
self,
pipeline: _Pipeline,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
**kwargs: Any,
) -> RawBatchCursor[_DocumentType]:
Expand Down Expand Up @@ -2509,7 +2510,7 @@ def watch(
batch_size: Optional[int] = None,
collation: Optional[_CollationIn] = None,
start_at_operation_time: Optional[Timestamp] = None,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
start_after: Optional[Mapping[str, Any]] = None,
comment: Optional[Any] = None,
full_document_before_change: Optional[str] = None,
Expand Down Expand Up @@ -2644,7 +2645,7 @@ def watch(
def rename(
self,
new_name: str,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
**kwargs: Any,
) -> MutableMapping[str, Any]:
Expand Down Expand Up @@ -2709,7 +2710,7 @@ def distinct(
self,
key: str,
filter: Optional[Mapping[str, Any]] = None,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
**kwargs: Any,
) -> List:
Expand Down Expand Up @@ -2860,7 +2861,7 @@ def find_one_and_delete(
projection: Optional[Union[Mapping[str, Any], Iterable[str]]] = None,
sort: Optional[_IndexList] = None,
hint: Optional[_IndexKeyHint] = None,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
let: Optional[Mapping[str, Any]] = None,
comment: Optional[Any] = None,
**kwargs: Any,
Expand Down Expand Up @@ -2953,7 +2954,7 @@ def find_one_and_replace(
upsert: bool = False,
return_document: bool = ReturnDocument.BEFORE,
hint: Optional[_IndexKeyHint] = None,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
let: Optional[Mapping[str, Any]] = None,
comment: Optional[Any] = None,
**kwargs: Any,
Expand Down Expand Up @@ -3062,7 +3063,7 @@ def find_one_and_update(
return_document: bool = ReturnDocument.BEFORE,
array_filters: Optional[Sequence[Mapping[str, Any]]] = None,
hint: Optional[_IndexKeyHint] = None,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
let: Optional[Mapping[str, Any]] = None,
comment: Optional[Any] = None,
**kwargs: Any,
Expand Down
11 changes: 6 additions & 5 deletions pymongo/command_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

"""CommandCursor class to iterate over command results."""
from __future__ import annotations

from collections import deque
from typing import TYPE_CHECKING, Any, Generic, Iterator, Mapping, NoReturn, Optional
Expand All @@ -36,12 +37,12 @@ class CommandCursor(Generic[_DocumentType]):

def __init__(
self,
collection: "Collection[_DocumentType]",
collection: Collection[_DocumentType],
cursor_info: Mapping[str, Any],
address: Optional[_Address],
batch_size: int = 0,
max_await_time_ms: Optional[int] = None,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
explicit_session: bool = False,
comment: Any = None,
) -> None:
Expand Down Expand Up @@ -267,7 +268,7 @@ def address(self) -> Optional[_Address]:
return self.__address

@property
def session(self) -> Optional["ClientSession"]:
def session(self) -> Optional[ClientSession]:
"""The cursor's :class:`~pymongo.client_session.ClientSession`, or None.

.. versionadded:: 3.6
Expand Down Expand Up @@ -312,12 +313,12 @@ class RawBatchCommandCursor(CommandCursor, Generic[_DocumentType]):

def __init__(
self,
collection: "Collection[_DocumentType]",
collection: Collection[_DocumentType],
cursor_info: Mapping[str, Any],
address: Optional[_Address],
batch_size: int = 0,
max_await_time_ms: Optional[int] = None,
session: Optional["ClientSession"] = None,
session: Optional[ClientSession] = None,
explicit_session: bool = False,
comment: Any = None,
) -> None:
Expand Down
Loading