diff --git a/bson/_helpers.py b/bson/_helpers.py index 6f05878b5d..596965a138 100644 --- a/bson/_helpers.py +++ b/bson/_helpers.py @@ -18,8 +18,10 @@ from __future__ import annotations -from collections.abc import Mapping -from typing import Any +from typing import TYPE_CHECKING, Any + +if TYPE_CHECKING: + from collections.abc import Mapping def _setstate_slots(self: Any, state: Any) -> None: diff --git a/bson/binary.py b/bson/binary.py index 7f8dd69431..2a05970f2f 100644 --- a/bson/binary.py +++ b/bson/binary.py @@ -15,7 +15,6 @@ import struct import warnings -from collections.abc import Sequence from enum import Enum from typing import TYPE_CHECKING, Any, Self, overload from uuid import UUID @@ -64,6 +63,7 @@ if TYPE_CHECKING: from array import array as _array + from collections.abc import Sequence from mmap import mmap as _mmap import numpy as np diff --git a/bson/dbref.py b/bson/dbref.py index 01ecfe6a14..90f0d7d733 100644 --- a/bson/dbref.py +++ b/bson/dbref.py @@ -16,13 +16,15 @@ from __future__ import annotations -from collections.abc import Mapping from copy import deepcopy -from typing import Any +from typing import TYPE_CHECKING, Any from bson._helpers import _getstate_slots, _setstate_slots from bson.son import SON +if TYPE_CHECKING: + from collections.abc import Mapping + class DBRef: """A reference to a document stored in MongoDB.""" diff --git a/gridfs/asynchronous/grid_file.py b/gridfs/asynchronous/grid_file.py index 1f1e860aea..c6d84a9c2e 100644 --- a/gridfs/asynchronous/grid_file.py +++ b/gridfs/asynchronous/grid_file.py @@ -21,8 +21,6 @@ import io import math from collections import abc -from collections.abc import Iterable, Mapping -from types import TracebackType from typing import TYPE_CHECKING, Any, NoReturn, Self, cast from bson.int64 import Int64 @@ -62,6 +60,9 @@ from pymongo.read_preferences import ReadPreference, _ServerMode if TYPE_CHECKING: + from collections.abc import Iterable, Mapping + from types import TracebackType + from pymongo.asynchronous.client_session import AsyncClientSession _IS_SYNC = False diff --git a/gridfs/synchronous/grid_file.py b/gridfs/synchronous/grid_file.py index 44cc097f7e..da2bc57fc3 100644 --- a/gridfs/synchronous/grid_file.py +++ b/gridfs/synchronous/grid_file.py @@ -21,8 +21,6 @@ import io import math from collections import abc -from collections.abc import Iterable, Mapping -from types import TracebackType from typing import TYPE_CHECKING, Any, NoReturn, Self, cast from bson.int64 import Int64 @@ -62,6 +60,9 @@ from pymongo.synchronous.helpers import next if TYPE_CHECKING: + from collections.abc import Iterable, Mapping + from types import TracebackType + from pymongo.synchronous.client_session import ClientSession _IS_SYNC = True diff --git a/pymongo/__init__.py b/pymongo/__init__.py index 943f88e34c..ee8997f1b4 100644 --- a/pymongo/__init__.py +++ b/pymongo/__init__.py @@ -16,8 +16,6 @@ from __future__ import annotations -from contextlib import AbstractContextManager - __all__ = [ "ASCENDING", "DESCENDING", @@ -114,6 +112,10 @@ from pymongo import collection # noqa: F401 from pymongo import command_cursor # noqa: F401 from pymongo import database # noqa: F401 +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from contextlib import AbstractContextManager # isort: on version = __version__ diff --git a/pymongo/_asyncio_lock.py b/pymongo/_asyncio_lock.py index 481703f4bb..7b4a00c779 100644 --- a/pymongo/_asyncio_lock.py +++ b/pymongo/_asyncio_lock.py @@ -9,9 +9,11 @@ import collections import threading from asyncio import events, exceptions -from collections.abc import Coroutine -from types import TracebackType -from typing import Any +from typing import TYPE_CHECKING, Any + +if TYPE_CHECKING: + from collections.abc import Coroutine + from types import TracebackType _global_lock = threading.Lock() diff --git a/pymongo/_client_bulk_shared.py b/pymongo/_client_bulk_shared.py index 409b9d3411..fdb40a25c6 100644 --- a/pymongo/_client_bulk_shared.py +++ b/pymongo/_client_bulk_shared.py @@ -17,7 +17,6 @@ from __future__ import annotations -from collections.abc import Mapping, MutableMapping from typing import TYPE_CHECKING, Any, NoReturn from bson.objectid import ObjectId @@ -32,6 +31,8 @@ from pymongo.message import _ClientBulkWriteContext if TYPE_CHECKING: + from collections.abc import Mapping, MutableMapping + from pymongo.typings import _AgnosticMongoClient, _DocumentOut, _Pipeline from pymongo.write_concern import WriteConcern diff --git a/pymongo/_csot.py b/pymongo/_csot.py index 14402d3d02..ea541b40ed 100644 --- a/pymongo/_csot.py +++ b/pymongo/_csot.py @@ -23,10 +23,11 @@ from collections.abc import Callable, MutableMapping from contextlib import AbstractContextManager from contextvars import ContextVar, Token -from types import TracebackType from typing import TYPE_CHECKING, Any, TypeVar, cast if TYPE_CHECKING: + from types import TracebackType + from pymongo.write_concern import WriteConcern TIMEOUT: ContextVar[float | None] = ContextVar("TIMEOUT", default=None) diff --git a/pymongo/_op_id.py b/pymongo/_op_id.py index 1173d10c7f..4b966d6509 100644 --- a/pymongo/_op_id.py +++ b/pymongo/_op_id.py @@ -24,8 +24,10 @@ from contextlib import AbstractContextManager from contextvars import ContextVar -from types import TracebackType -from typing import Any +from typing import TYPE_CHECKING, Any + +if TYPE_CHECKING: + from types import TracebackType OP_ID: ContextVar[int | None] = ContextVar("OP_ID", default=None) diff --git a/pymongo/_telemetry.py b/pymongo/_telemetry.py index 1596f82fb9..b18276e608 100644 --- a/pymongo/_telemetry.py +++ b/pymongo/_telemetry.py @@ -17,9 +17,7 @@ from __future__ import annotations import datetime -import queue import time -from collections.abc import MutableMapping from typing import TYPE_CHECKING, Any from pymongo import _op_id @@ -39,6 +37,9 @@ from pymongo.message import _randint if TYPE_CHECKING: + import queue + from collections.abc import MutableMapping + from bson.objectid import ObjectId from pymongo.hello import Hello from pymongo.monitoring import _EventListeners diff --git a/pymongo/asynchronous/aggregation.py b/pymongo/asynchronous/aggregation.py index 766672aee4..586aaef317 100644 --- a/pymongo/asynchronous/aggregation.py +++ b/pymongo/asynchronous/aggregation.py @@ -16,7 +16,6 @@ from __future__ import annotations -from collections.abc import Callable, Mapping, MutableMapping from typing import TYPE_CHECKING, Any from pymongo import common @@ -25,6 +24,8 @@ from pymongo.read_preferences import ReadPreference, _AggWritePref if TYPE_CHECKING: + from collections.abc import Callable, Mapping, MutableMapping + from pymongo.asynchronous.client_session import AsyncClientSession from pymongo.asynchronous.collection import AsyncCollection from pymongo.asynchronous.command_cursor import AsyncCommandCursor diff --git a/pymongo/asynchronous/auth.py b/pymongo/asynchronous/auth.py index 2a50ff7ce8..64008c7e2f 100644 --- a/pymongo/asynchronous/auth.py +++ b/pymongo/asynchronous/auth.py @@ -21,7 +21,6 @@ import hmac import socket from base64 import standard_b64decode, standard_b64encode -from collections.abc import Callable, Coroutine, Mapping from typing import ( TYPE_CHECKING, Any, @@ -49,6 +48,8 @@ from pymongo.saslprep import saslprep if TYPE_CHECKING: + from collections.abc import Callable, Coroutine, Mapping + from pymongo.asynchronous.pool import AsyncConnection HAVE_KERBEROS = True diff --git a/pymongo/asynchronous/auth_aws.py b/pymongo/asynchronous/auth_aws.py index 7982936948..3baab405e0 100644 --- a/pymongo/asynchronous/auth_aws.py +++ b/pymongo/asynchronous/auth_aws.py @@ -16,7 +16,6 @@ from __future__ import annotations -from collections.abc import Mapping from typing import TYPE_CHECKING, Any import bson @@ -24,6 +23,8 @@ from pymongo.errors import ConfigurationError, OperationFailure if TYPE_CHECKING: + from collections.abc import Mapping + from bson.typings import _ReadableBuffer from pymongo.asynchronous.pool import AsyncConnection from pymongo.auth_shared import MongoCredential diff --git a/pymongo/asynchronous/auth_oidc.py b/pymongo/asynchronous/auth_oidc.py index 70e8fae2e9..f9c98847b0 100644 --- a/pymongo/asynchronous/auth_oidc.py +++ b/pymongo/asynchronous/auth_oidc.py @@ -17,9 +17,7 @@ from __future__ import annotations import asyncio -import threading import time -from collections.abc import Mapping, MutableMapping from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any @@ -41,6 +39,9 @@ from pymongo.lock import Lock, _async_create_lock if TYPE_CHECKING: + import threading + from collections.abc import Mapping, MutableMapping + from pymongo.asynchronous.pool import AsyncConnection from pymongo.auth_shared import MongoCredential diff --git a/pymongo/asynchronous/bulk.py b/pymongo/asynchronous/bulk.py index f64fbe6ee0..b30210b8cd 100644 --- a/pymongo/asynchronous/bulk.py +++ b/pymongo/asynchronous/bulk.py @@ -20,7 +20,6 @@ from __future__ import annotations import copy -from collections.abc import Iterator, Mapping, MutableMapping from itertools import islice from typing import ( TYPE_CHECKING, @@ -56,6 +55,8 @@ from pymongo.write_concern import WriteConcern if TYPE_CHECKING: + from collections.abc import Iterator, Mapping, MutableMapping + from pymongo.asynchronous.client_session import AsyncClientSession from pymongo.asynchronous.collection import AsyncCollection from pymongo.asynchronous.mongo_client import AsyncMongoClient diff --git a/pymongo/asynchronous/change_stream.py b/pymongo/asynchronous/change_stream.py index aad9ffe385..868b330802 100644 --- a/pymongo/asynchronous/change_stream.py +++ b/pymongo/asynchronous/change_stream.py @@ -16,7 +16,6 @@ from __future__ import annotations -from types import TracebackType from typing import TYPE_CHECKING from bson import _bson_to_dict @@ -43,6 +42,8 @@ _IS_SYNC = False if TYPE_CHECKING: + from types import TracebackType + from pymongo.asynchronous.client_session import AsyncClientSession from pymongo.asynchronous.collection import AsyncCollection from pymongo.asynchronous.database import AsyncDatabase diff --git a/pymongo/asynchronous/client_bulk.py b/pymongo/asynchronous/client_bulk.py index 3727d56048..7b2b1833ae 100644 --- a/pymongo/asynchronous/client_bulk.py +++ b/pymongo/asynchronous/client_bulk.py @@ -20,7 +20,6 @@ from __future__ import annotations import copy -from collections.abc import Mapping, MutableMapping from itertools import islice from typing import ( TYPE_CHECKING, @@ -38,6 +37,8 @@ from pymongo.asynchronous.helpers import _handle_reauth if TYPE_CHECKING: + from collections.abc import Mapping, MutableMapping + from pymongo.asynchronous.client_session import AsyncClientSession from pymongo.asynchronous.mongo_client import AsyncMongoClient from pymongo.asynchronous.pool import AsyncConnection diff --git a/pymongo/asynchronous/client_session.py b/pymongo/asynchronous/client_session.py index 23df3dda01..68180c5514 100644 --- a/pymongo/asynchronous/client_session.py +++ b/pymongo/asynchronous/client_session.py @@ -138,8 +138,6 @@ import asyncio import random import time -from collections.abc import Awaitable, Callable -from contextlib import AbstractAsyncContextManager from contextvars import ContextVar, Token from typing import ( TYPE_CHECKING, @@ -174,6 +172,8 @@ from pymongo.write_concern import WriteConcern if TYPE_CHECKING: + from collections.abc import Awaitable, Callable + from contextlib import AbstractAsyncContextManager from types import TracebackType from pymongo.asynchronous.mongo_client import AsyncMongoClient diff --git a/pymongo/asynchronous/command_cursor.py b/pymongo/asynchronous/command_cursor.py index cedc4f8f2f..7cb454a68d 100644 --- a/pymongo/asynchronous/command_cursor.py +++ b/pymongo/asynchronous/command_cursor.py @@ -17,7 +17,6 @@ from __future__ import annotations from collections import deque -from collections.abc import AsyncIterator, Mapping from typing import ( TYPE_CHECKING, Any, @@ -33,6 +32,8 @@ from pymongo.typings import _Address, _DocumentType if TYPE_CHECKING: + from collections.abc import AsyncIterator, Mapping + from pymongo.asynchronous.client_session import AsyncClientSession from pymongo.asynchronous.collection import AsyncCollection from pymongo.asynchronous.pool import AsyncConnection diff --git a/pymongo/asynchronous/command_runner.py b/pymongo/asynchronous/command_runner.py index f6b5455018..b1c5421537 100644 --- a/pymongo/asynchronous/command_runner.py +++ b/pymongo/asynchronous/command_runner.py @@ -37,7 +37,6 @@ import datetime import time -from collections.abc import Callable, Mapping, MutableMapping, Sequence from typing import ( TYPE_CHECKING, Any, @@ -54,6 +53,8 @@ from pymongo.monitoring import _is_speculative_authenticate if TYPE_CHECKING: + from collections.abc import Callable, Mapping, MutableMapping, Sequence + from bson import CodecOptions from bson.objectid import ObjectId from pymongo.asynchronous.client_session import AsyncClientSession diff --git a/pymongo/asynchronous/cursor.py b/pymongo/asynchronous/cursor.py index 34c0aa1305..7829db9d1f 100644 --- a/pymongo/asynchronous/cursor.py +++ b/pymongo/asynchronous/cursor.py @@ -17,7 +17,6 @@ from __future__ import annotations from collections import deque -from collections.abc import Mapping from typing import ( TYPE_CHECKING, Any, @@ -47,6 +46,8 @@ from pymongo.typings import _DocumentOut, _DocumentType if TYPE_CHECKING: + from collections.abc import Mapping + from bson.codec_options import CodecOptions from pymongo.asynchronous.collection import AsyncCollection diff --git a/pymongo/asynchronous/cursor_base.py b/pymongo/asynchronous/cursor_base.py index 365ef7321a..ac1410ddcb 100644 --- a/pymongo/asynchronous/cursor_base.py +++ b/pymongo/asynchronous/cursor_base.py @@ -17,8 +17,6 @@ from __future__ import annotations from abc import abstractmethod -from collections.abc import Mapping, Sequence -from types import TracebackType from typing import TYPE_CHECKING, Any, cast from pymongo import _csot @@ -31,6 +29,9 @@ from pymongo.typings import _DocumentOut, _DocumentType if TYPE_CHECKING: + from collections.abc import Mapping, Sequence + from types import TracebackType + from pymongo.asynchronous.client_session import AsyncClientSession from pymongo.asynchronous.pool import AsyncConnection from pymongo.read_preferences import _ServerMode diff --git a/pymongo/asynchronous/encryption.py b/pymongo/asynchronous/encryption.py index e12c001b68..2f996dfe2c 100644 --- a/pymongo/asynchronous/encryption.py +++ b/pymongo/asynchronous/encryption.py @@ -20,9 +20,7 @@ import time as time # noqa: PLC0414 # needed in sync version import uuid import weakref -from collections.abc import AsyncGenerator, Mapping, MutableMapping, Sequence from copy import deepcopy -from types import TracebackType from typing import ( TYPE_CHECKING, Any, @@ -91,6 +89,9 @@ from pymongo.write_concern import WriteConcern if TYPE_CHECKING: + from collections.abc import AsyncGenerator, Mapping, MutableMapping, Sequence + from types import TracebackType + from pymongocrypt.mongocrypt import MongoCryptKmsContext from pymongo._kms_connect_shared import AsyncKMSConnectCallback diff --git a/pymongo/asynchronous/pool.py b/pymongo/asynchronous/pool.py index 1d49a6ffb7..e154d912a9 100644 --- a/pymongo/asynchronous/pool.py +++ b/pymongo/asynchronous/pool.py @@ -18,7 +18,6 @@ import collections import os import time -from collections.abc import Mapping, MutableMapping, Sequence from typing import ( TYPE_CHECKING, Any, @@ -76,6 +75,7 @@ from pymongo.server_type import SERVER_TYPE if TYPE_CHECKING: + from collections.abc import Mapping, MutableMapping, Sequence from types import TracebackType from bson import CodecOptions diff --git a/pymongo/asynchronous/settings.py b/pymongo/asynchronous/settings.py index cb247ab48d..f4cae1a29d 100644 --- a/pymongo/asynchronous/settings.py +++ b/pymongo/asynchronous/settings.py @@ -31,6 +31,10 @@ from pymongo.pool_options import PoolOptions from pymongo.topology_description import _ServerSelector +if TYPE_CHECKING: + import threading + from collections.abc import Callable, Collection + _IS_SYNC = False diff --git a/pymongo/asynchronous/srv_resolver.py b/pymongo/asynchronous/srv_resolver.py index b7d7b68b05..a95f7570b0 100644 --- a/pymongo/asynchronous/srv_resolver.py +++ b/pymongo/asynchronous/srv_resolver.py @@ -18,7 +18,6 @@ import ipaddress import random -from collections.abc import Callable from typing import TYPE_CHECKING, Any from pymongo._psl import SPECIAL_USE_LABELS, _to_punycode, is_public_suffix @@ -27,6 +26,8 @@ from pymongo.errors import ConfigurationError if TYPE_CHECKING: + from collections.abc import Callable + from dns import resolver _IS_SYNC = False diff --git a/pymongo/asynchronous/topology.py b/pymongo/asynchronous/topology.py index b5194ed937..c356a5321c 100644 --- a/pymongo/asynchronous/topology.py +++ b/pymongo/asynchronous/topology.py @@ -18,13 +18,11 @@ import asyncio import os -import queue import random import sys import time import warnings import weakref -from collections.abc import Callable, Mapping from pathlib import Path from typing import TYPE_CHECKING, Any, cast @@ -73,6 +71,9 @@ ) if TYPE_CHECKING: + import queue + from collections.abc import Callable, Mapping + from pymongo.asynchronous.settings import TopologySettings from pymongo.typings import _Address diff --git a/pymongo/asynchronous/uri_parser.py b/pymongo/asynchronous/uri_parser.py index 16ff5b306d..43c00e515a 100644 --- a/pymongo/asynchronous/uri_parser.py +++ b/pymongo/asynchronous/uri_parser.py @@ -17,8 +17,7 @@ from __future__ import annotations -from collections.abc import Callable -from typing import Any +from typing import TYPE_CHECKING, Any from pymongo.asynchronous.srv_resolver import _SrvResolver from pymongo.common import SRV_SERVICE_NAME, _CaseInsensitiveDictionary @@ -36,6 +35,9 @@ split_options, ) +if TYPE_CHECKING: + from collections.abc import Callable + _IS_SYNC = False diff --git a/pymongo/auth_oidc_shared.py b/pymongo/auth_oidc_shared.py index 512d1d2778..fda0042c1c 100644 --- a/pymongo/auth_oidc_shared.py +++ b/pymongo/auth_oidc_shared.py @@ -19,7 +19,6 @@ import abc import os -from collections.abc import Callable, Mapping, MutableMapping from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any from urllib.parse import quote @@ -32,6 +31,8 @@ from pymongo.helpers_shared import _AUTHENTICATION_FAILURE_CODE if TYPE_CHECKING: + from collections.abc import Callable, Mapping, MutableMapping + from pymongo.auth_shared import MongoCredential diff --git a/pymongo/auth_shared.py b/pymongo/auth_shared.py index a65d1f0d57..d9798c576a 100644 --- a/pymongo/auth_shared.py +++ b/pymongo/auth_shared.py @@ -22,7 +22,6 @@ import typing from base64 import standard_b64encode from collections import namedtuple -from collections.abc import Callable, Mapping, MutableMapping from typing import TYPE_CHECKING, Any, cast from bson import Binary @@ -37,6 +36,8 @@ from pymongo.errors import ConfigurationError if TYPE_CHECKING: + from collections.abc import Callable, Mapping, MutableMapping + from pymongo.hello import Hello MECHANISMS = frozenset( diff --git a/pymongo/bulk_shared.py b/pymongo/bulk_shared.py index 0f7d7a4b04..0d54b73977 100644 --- a/pymongo/bulk_shared.py +++ b/pymongo/bulk_shared.py @@ -17,7 +17,6 @@ from __future__ import annotations -from collections.abc import Iterator, Mapping, MutableMapping from typing import TYPE_CHECKING, Any, NoReturn from bson.objectid import ObjectId @@ -38,6 +37,8 @@ ) if TYPE_CHECKING: + from collections.abc import Iterator, Mapping, MutableMapping + from pymongo.typings import _AgnosticCollection, _DocumentOut, _DocumentType, _Pipeline diff --git a/pymongo/change_stream_shared.py b/pymongo/change_stream_shared.py index 23d5e09173..427f0ac354 100644 --- a/pymongo/change_stream_shared.py +++ b/pymongo/change_stream_shared.py @@ -17,7 +17,6 @@ from __future__ import annotations import copy -from collections.abc import Mapping from typing import TYPE_CHECKING, Any, Generic, TypeVar, cast from bson.raw_bson import RawBSONDocument @@ -32,6 +31,8 @@ from pymongo.typings import _DocumentType if TYPE_CHECKING: + from collections.abc import Mapping + from bson import CodecOptions from bson.timestamp import Timestamp from pymongo.typings import ( diff --git a/pymongo/collation.py b/pymongo/collation.py index 0ed28d13fe..396560bcd2 100644 --- a/pymongo/collation.py +++ b/pymongo/collation.py @@ -21,12 +21,14 @@ from __future__ import annotations -from collections.abc import Mapping -from typing import Any +from typing import TYPE_CHECKING, Any from pymongo import common from pymongo.write_concern import validate_boolean +if TYPE_CHECKING: + from collections.abc import Mapping + class CollationStrength: """ diff --git a/pymongo/compression_support.py b/pymongo/compression_support.py index 4cf730334c..84ef43e6f2 100644 --- a/pymongo/compression_support.py +++ b/pymongo/compression_support.py @@ -15,13 +15,15 @@ import sys import warnings -from collections.abc import Iterable -from typing import Any +from typing import TYPE_CHECKING, Any from pymongo.errors import ProtocolError from pymongo.hello import HelloCompat from pymongo.helpers_shared import _SENSITIVE_COMMANDS +if TYPE_CHECKING: + from collections.abc import Iterable + _SUPPORTED_COMPRESSORS = {"snappy", "zlib", "zstd"} _NO_COMPRESSION = {HelloCompat.CMD, HelloCompat.LEGACY_CMD} _NO_COMPRESSION.update(_SENSITIVE_COMMANDS) diff --git a/pymongo/daemon.py b/pymongo/daemon.py index 65cf6a3248..756ea53423 100644 --- a/pymongo/daemon.py +++ b/pymongo/daemon.py @@ -25,8 +25,10 @@ import subprocess import sys import warnings -from collections.abc import Sequence -from typing import Any +from typing import TYPE_CHECKING, Any + +if TYPE_CHECKING: + from collections.abc import Sequence # The maximum amount of time to wait for the intermediate subprocess. _WAIT_TIMEOUT = 10 diff --git a/pymongo/encryption_options.py b/pymongo/encryption_options.py index 70b371796a..094d0e0bcd 100644 --- a/pymongo/encryption_options.py +++ b/pymongo/encryption_options.py @@ -19,7 +19,6 @@ from __future__ import annotations -from collections.abc import Callable, Mapping from typing import TYPE_CHECKING, Any, TypedDict from pymongo.uri_parser_shared import _parse_kms_tls_options @@ -45,6 +44,8 @@ from pymongo.errors import ConfigurationError if TYPE_CHECKING: + from collections.abc import Callable, Mapping + from pymongo.pyopenssl_context import SSLContext from pymongo.typings import _AgnosticMongoClient diff --git a/pymongo/encryption_shared.py b/pymongo/encryption_shared.py index b09eef9c2f..28909f428b 100644 --- a/pymongo/encryption_shared.py +++ b/pymongo/encryption_shared.py @@ -32,6 +32,9 @@ if TYPE_CHECKING: from pymongo.results import BulkWriteResult +if TYPE_CHECKING: + from collections.abc import Iterator + @contextlib.contextmanager def _wrap_encryption_errors() -> Iterator[None]: diff --git a/pymongo/errors.py b/pymongo/errors.py index cc643ae810..70d436923c 100644 --- a/pymongo/errors.py +++ b/pymongo/errors.py @@ -19,13 +19,14 @@ from __future__ import annotations -from collections.abc import Iterable, Mapping, Sequence from ssl import SSLCertVerificationError as _CertificateError # noqa: F401 from typing import TYPE_CHECKING, Any from bson.errors import InvalidDocument if TYPE_CHECKING: + from collections.abc import Iterable, Mapping, Sequence + from pymongo.results import ClientBulkWriteResult from pymongo.typings import _DocumentOut diff --git a/pymongo/hello.py b/pymongo/hello.py index 2fac9f34af..b567df747e 100644 --- a/pymongo/hello.py +++ b/pymongo/hello.py @@ -17,9 +17,7 @@ from __future__ import annotations import copy -import datetime import itertools -from collections.abc import Mapping from typing import TYPE_CHECKING, Any, Generic from pymongo import common @@ -27,6 +25,9 @@ from pymongo.typings import ClusterTime, _DocumentType if TYPE_CHECKING: + import datetime + from collections.abc import Mapping + from bson.objectid import ObjectId diff --git a/pymongo/helpers_shared.py b/pymongo/helpers_shared.py index 0a59f1a760..594f55fb26 100644 --- a/pymongo/helpers_shared.py +++ b/pymongo/helpers_shared.py @@ -21,7 +21,6 @@ import time import traceback from collections import abc -from collections.abc import Container, Iterable, Mapping, Sequence from typing import ( TYPE_CHECKING, Any, @@ -43,6 +42,8 @@ from pymongo.hello import HelloCompat if TYPE_CHECKING: + from collections.abc import Container, Iterable, Mapping, Sequence + from pymongo.cursor_shared import _Hint from pymongo.operations import _IndexList from pymongo.pool_options import PoolOptions diff --git a/pymongo/message.py b/pymongo/message.py index 7cecf217a3..18c4ae162a 100644 --- a/pymongo/message.py +++ b/pymongo/message.py @@ -24,7 +24,6 @@ import random import struct -from collections.abc import Callable, Iterable, Mapping, MutableMapping from io import BytesIO as _BytesIO from typing import ( TYPE_CHECKING, @@ -57,6 +56,8 @@ from pymongo.read_preferences import ReadPreference, _ServerMode if TYPE_CHECKING: + from collections.abc import Callable, Iterable, Mapping, MutableMapping + from pymongo.compression_support import SnappyContext, ZlibContext, ZstdContext from pymongo.monitoring import _EventListeners from pymongo.read_concern import ReadConcern diff --git a/pymongo/monitoring.py b/pymongo/monitoring.py index 98caff0088..52a63b7cd8 100644 --- a/pymongo/monitoring.py +++ b/pymongo/monitoring.py @@ -188,15 +188,15 @@ def connection_checked_in(self, event): from __future__ import annotations -import datetime from collections import abc, namedtuple -from collections.abc import Mapping, Sequence from typing import TYPE_CHECKING, Any from pymongo.hello import Hello, HelloCompat from pymongo.helpers_shared import _SENSITIVE_COMMANDS, _handle_exception if TYPE_CHECKING: + import datetime + from collections.abc import Mapping, Sequence from datetime import timedelta from bson.objectid import ObjectId diff --git a/pymongo/network_layer.py b/pymongo/network_layer.py index 042ecb4ae4..f60e27738c 100644 --- a/pymongo/network_layer.py +++ b/pymongo/network_layer.py @@ -19,12 +19,10 @@ import asyncio import collections import errno -import socket import struct import sys import time from asyncio import AbstractEventLoop, BaseTransport, BufferedProtocol, Future, Transport -from collections.abc import Callable from typing import ( TYPE_CHECKING, Any, @@ -59,6 +57,9 @@ ) if TYPE_CHECKING: + import socket + from collections.abc import Callable + from pymongo.asynchronous.pool import AsyncConnection from pymongo.synchronous.pool import Connection diff --git a/pymongo/ocsp_support.py b/pymongo/ocsp_support.py index b73e87acbe..9c7f3ecab8 100644 --- a/pymongo/ocsp_support.py +++ b/pymongo/ocsp_support.py @@ -18,7 +18,6 @@ import logging as _logging import re as _re -from collections.abc import Iterable from datetime import datetime as _datetime from datetime import timezone from typing import TYPE_CHECKING @@ -68,6 +67,8 @@ from pymongo.ocsp_cache import _next_update, _this_update if TYPE_CHECKING: + from collections.abc import Iterable + from cryptography.hazmat.primitives.asymmetric import ( dsa, ec, diff --git a/pymongo/pool_options.py b/pymongo/pool_options.py index 855c7cd2c9..d6985c549f 100644 --- a/pymongo/pool_options.py +++ b/pymongo/pool_options.py @@ -23,7 +23,6 @@ import os import platform import sys -from collections.abc import MutableMapping from contextlib import AbstractContextManager, nullcontext from pathlib import Path from typing import TYPE_CHECKING, Any @@ -41,6 +40,8 @@ from pymongo.lock import _create_lock if TYPE_CHECKING: + from collections.abc import MutableMapping + from pymongo.auth_shared import MongoCredential from pymongo.compression_support import CompressionSettings from pymongo.driver_info import DriverInfo diff --git a/pymongo/pool_shared.py b/pymongo/pool_shared.py index 71cfc4d3b4..83a10ef5ae 100644 --- a/pymongo/pool_shared.py +++ b/pymongo/pool_shared.py @@ -26,7 +26,6 @@ import sys import time import weakref -from collections.abc import MutableMapping from typing import ( TYPE_CHECKING, Any, @@ -66,6 +65,8 @@ SSLErrors = (PYSSLError, SSLError) if TYPE_CHECKING: + from collections.abc import MutableMapping + from bson.objectid import ObjectId from pymongo.auth_shared import _AuthContext from pymongo.compression_support import SnappyContext, ZlibContext, ZstdContext diff --git a/pymongo/pyopenssl_context.py b/pymongo/pyopenssl_context.py index 9fc29d2bad..10e8af1d0b 100644 --- a/pymongo/pyopenssl_context.py +++ b/pymongo/pyopenssl_context.py @@ -20,11 +20,9 @@ from __future__ import annotations -import socket as _socket import ssl as _stdlibssl import sys as _sys import time as _time -from collections.abc import Callable from errno import EINTR as _EINTR from ipaddress import ip_address as _ip_address from typing import TYPE_CHECKING, Any, TypeVar @@ -43,6 +41,8 @@ from pymongo.write_concern import validate_boolean if TYPE_CHECKING: + import socket as _socket + from collections.abc import Callable from ssl import VerifyMode diff --git a/pymongo/response.py b/pymongo/response.py index 3ea4fa5c5b..366d3fee93 100644 --- a/pymongo/response.py +++ b/pymongo/response.py @@ -16,10 +16,10 @@ from __future__ import annotations -from collections.abc import Mapping, Sequence from typing import TYPE_CHECKING, Any if TYPE_CHECKING: + from collections.abc import Mapping, Sequence from datetime import timedelta from pymongo.message import _OpMsg diff --git a/pymongo/server_api.py b/pymongo/server_api.py index afc54c5e39..736b654bb9 100644 --- a/pymongo/server_api.py +++ b/pymongo/server_api.py @@ -86,8 +86,10 @@ from __future__ import annotations -from collections.abc import MutableMapping -from typing import Any +from typing import TYPE_CHECKING, Any + +if TYPE_CHECKING: + from collections.abc import MutableMapping class ServerApiVersion: diff --git a/pymongo/server_description.py b/pymongo/server_description.py index c82fc6d8f6..df8665d9e0 100644 --- a/pymongo/server_description.py +++ b/pymongo/server_description.py @@ -32,6 +32,9 @@ from bson.objectid import ObjectId from pymongo.typings import ClusterTime, _Address +if TYPE_CHECKING: + from collections.abc import Mapping + class ServerDescription: """Immutable representation of one server. diff --git a/pymongo/settings_shared.py b/pymongo/settings_shared.py index 87e7066754..d36876006d 100644 --- a/pymongo/settings_shared.py +++ b/pymongo/settings_shared.py @@ -18,8 +18,7 @@ import threading import traceback -from collections.abc import Callable, Collection -from typing import Any, Generic, TypeVar +from typing import TYPE_CHECKING, Any, Generic, TypeVar from bson.objectid import ObjectId from pymongo import common @@ -29,6 +28,9 @@ from pymongo.server_description import ServerDescription from pymongo.topology_description import TOPOLOGY_TYPE, _ServerSelector +if TYPE_CHECKING: + from collections.abc import Callable, Collection + _PoolClassType = TypeVar("_PoolClassType") _MonitorClassType = TypeVar("_MonitorClassType") diff --git a/pymongo/ssl_support.py b/pymongo/ssl_support.py index fff71e3870..4a95420c89 100644 --- a/pymongo/ssl_support.py +++ b/pymongo/ssl_support.py @@ -18,12 +18,14 @@ import os import sys -import types import warnings -from typing import Any +from typing import TYPE_CHECKING, Any from pymongo.errors import ConfigurationError +if TYPE_CHECKING: + import types + HAVE_SSL = True HAVE_PYSSL = True diff --git a/pymongo/synchronous/aggregation.py b/pymongo/synchronous/aggregation.py index 2aad007143..3c81078eff 100644 --- a/pymongo/synchronous/aggregation.py +++ b/pymongo/synchronous/aggregation.py @@ -16,7 +16,6 @@ from __future__ import annotations -from collections.abc import Callable, Mapping, MutableMapping from typing import TYPE_CHECKING, Any from pymongo import common @@ -25,6 +24,8 @@ from pymongo.read_preferences import ReadPreference, _AggWritePref if TYPE_CHECKING: + from collections.abc import Callable, Mapping, MutableMapping + from pymongo.read_preferences import _ServerMode from pymongo.synchronous.client_session import ClientSession from pymongo.synchronous.collection import Collection diff --git a/pymongo/synchronous/auth.py b/pymongo/synchronous/auth.py index e0b24ca4e2..1fc037c47b 100644 --- a/pymongo/synchronous/auth.py +++ b/pymongo/synchronous/auth.py @@ -21,7 +21,6 @@ import hmac import socket from base64 import standard_b64decode, standard_b64encode -from collections.abc import Callable, Mapping from typing import ( TYPE_CHECKING, Any, @@ -49,6 +48,8 @@ from pymongo.synchronous.helpers import _getaddrinfo if TYPE_CHECKING: + from collections.abc import Callable, Mapping + from pymongo.synchronous.pool import Connection HAVE_KERBEROS = True diff --git a/pymongo/synchronous/auth_aws.py b/pymongo/synchronous/auth_aws.py index 777e0620f5..be8011c4f6 100644 --- a/pymongo/synchronous/auth_aws.py +++ b/pymongo/synchronous/auth_aws.py @@ -16,7 +16,6 @@ from __future__ import annotations -from collections.abc import Mapping from typing import TYPE_CHECKING, Any import bson @@ -24,6 +23,8 @@ from pymongo.errors import ConfigurationError, OperationFailure if TYPE_CHECKING: + from collections.abc import Mapping + from bson.typings import _ReadableBuffer from pymongo.auth_shared import MongoCredential from pymongo.synchronous.pool import Connection diff --git a/pymongo/synchronous/auth_oidc.py b/pymongo/synchronous/auth_oidc.py index 652ee44616..299b3eb17b 100644 --- a/pymongo/synchronous/auth_oidc.py +++ b/pymongo/synchronous/auth_oidc.py @@ -17,9 +17,7 @@ from __future__ import annotations import asyncio -import threading import time -from collections.abc import Mapping, MutableMapping from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any @@ -41,6 +39,9 @@ from pymongo.lock import Lock, _create_lock if TYPE_CHECKING: + import threading + from collections.abc import Mapping, MutableMapping + from pymongo.auth_shared import MongoCredential from pymongo.synchronous.pool import Connection diff --git a/pymongo/synchronous/bulk.py b/pymongo/synchronous/bulk.py index c341693a36..b3b54abbe7 100644 --- a/pymongo/synchronous/bulk.py +++ b/pymongo/synchronous/bulk.py @@ -20,7 +20,6 @@ from __future__ import annotations import copy -from collections.abc import Iterator, Mapping, MutableMapping from itertools import islice from typing import ( TYPE_CHECKING, @@ -56,6 +55,8 @@ from pymongo.write_concern import WriteConcern if TYPE_CHECKING: + from collections.abc import Iterator, Mapping, MutableMapping + from pymongo.synchronous.client_session import ClientSession from pymongo.synchronous.collection import Collection from pymongo.synchronous.mongo_client import MongoClient diff --git a/pymongo/synchronous/change_stream.py b/pymongo/synchronous/change_stream.py index e5bc5971ce..3336b4f6e6 100644 --- a/pymongo/synchronous/change_stream.py +++ b/pymongo/synchronous/change_stream.py @@ -16,7 +16,6 @@ from __future__ import annotations -from types import TracebackType from typing import TYPE_CHECKING from bson import _bson_to_dict @@ -43,6 +42,8 @@ _IS_SYNC = True if TYPE_CHECKING: + from types import TracebackType + from pymongo.synchronous.client_session import ClientSession from pymongo.synchronous.collection import Collection from pymongo.synchronous.database import Database diff --git a/pymongo/synchronous/client_bulk.py b/pymongo/synchronous/client_bulk.py index 2c5d0050eb..097df34f89 100644 --- a/pymongo/synchronous/client_bulk.py +++ b/pymongo/synchronous/client_bulk.py @@ -20,7 +20,6 @@ from __future__ import annotations import copy -from collections.abc import Mapping, MutableMapping from itertools import islice from typing import ( TYPE_CHECKING, @@ -38,6 +37,8 @@ from pymongo.synchronous.helpers import _handle_reauth if TYPE_CHECKING: + from collections.abc import Mapping, MutableMapping + from pymongo.synchronous.client_session import ClientSession from pymongo.synchronous.mongo_client import MongoClient from pymongo.synchronous.pool import Connection diff --git a/pymongo/synchronous/client_session.py b/pymongo/synchronous/client_session.py index 0c038561d7..fd24dbf230 100644 --- a/pymongo/synchronous/client_session.py +++ b/pymongo/synchronous/client_session.py @@ -137,8 +137,6 @@ import random import time -from collections.abc import Callable -from contextlib import AbstractContextManager from contextvars import ContextVar, Token from typing import ( TYPE_CHECKING, @@ -173,6 +171,8 @@ from pymongo.write_concern import WriteConcern if TYPE_CHECKING: + from collections.abc import Callable + from contextlib import AbstractContextManager from types import TracebackType from pymongo.read_concern import ReadConcern diff --git a/pymongo/synchronous/command_cursor.py b/pymongo/synchronous/command_cursor.py index b726b9557c..8f8c2961fb 100644 --- a/pymongo/synchronous/command_cursor.py +++ b/pymongo/synchronous/command_cursor.py @@ -17,7 +17,6 @@ from __future__ import annotations from collections import deque -from collections.abc import Iterator, Mapping from typing import ( TYPE_CHECKING, Any, @@ -33,6 +32,8 @@ from pymongo.typings import _Address, _DocumentType if TYPE_CHECKING: + from collections.abc import Iterator, Mapping + from pymongo.synchronous.client_session import ClientSession from pymongo.synchronous.collection import Collection from pymongo.synchronous.pool import Connection diff --git a/pymongo/synchronous/command_runner.py b/pymongo/synchronous/command_runner.py index dd9f5cba3a..3381f117a2 100644 --- a/pymongo/synchronous/command_runner.py +++ b/pymongo/synchronous/command_runner.py @@ -37,7 +37,6 @@ import datetime import time -from collections.abc import Callable, Mapping, MutableMapping, Sequence from typing import ( TYPE_CHECKING, Any, @@ -54,6 +53,8 @@ from pymongo.monitoring import _is_speculative_authenticate if TYPE_CHECKING: + from collections.abc import Callable, Mapping, MutableMapping, Sequence + from bson import CodecOptions from bson.objectid import ObjectId from pymongo.compression_support import SnappyContext, ZlibContext, ZstdContext diff --git a/pymongo/synchronous/cursor.py b/pymongo/synchronous/cursor.py index d9000105b3..5dbf71f523 100644 --- a/pymongo/synchronous/cursor.py +++ b/pymongo/synchronous/cursor.py @@ -17,7 +17,6 @@ from __future__ import annotations from collections import deque -from collections.abc import Mapping from typing import ( TYPE_CHECKING, Any, @@ -47,6 +46,8 @@ from pymongo.typings import _DocumentOut, _DocumentType if TYPE_CHECKING: + from collections.abc import Mapping + from bson.codec_options import CodecOptions from pymongo.synchronous.collection import Collection diff --git a/pymongo/synchronous/cursor_base.py b/pymongo/synchronous/cursor_base.py index 314ddd562d..b123c57880 100644 --- a/pymongo/synchronous/cursor_base.py +++ b/pymongo/synchronous/cursor_base.py @@ -17,8 +17,6 @@ from __future__ import annotations from abc import abstractmethod -from collections.abc import Mapping, Sequence -from types import TracebackType from typing import TYPE_CHECKING, Any, cast from pymongo import _csot @@ -31,6 +29,9 @@ from pymongo.typings import _DocumentOut, _DocumentType if TYPE_CHECKING: + from collections.abc import Mapping, Sequence + from types import TracebackType + from pymongo.read_preferences import _ServerMode from pymongo.synchronous.client_session import ClientSession from pymongo.synchronous.pool import Connection diff --git a/pymongo/synchronous/encryption.py b/pymongo/synchronous/encryption.py index 0cea32c29a..95ff05eaef 100644 --- a/pymongo/synchronous/encryption.py +++ b/pymongo/synchronous/encryption.py @@ -19,9 +19,7 @@ import time as time # noqa: PLC0414 # needed in sync version import uuid import weakref -from collections.abc import Generator, Mapping, MutableMapping, Sequence from copy import deepcopy -from types import TracebackType from typing import ( TYPE_CHECKING, Any, @@ -90,6 +88,9 @@ from pymongo.write_concern import WriteConcern if TYPE_CHECKING: + from collections.abc import Generator, Mapping, MutableMapping, Sequence + from types import TracebackType + from pymongocrypt.mongocrypt import MongoCryptKmsContext from pymongo._kms_connect_shared import KMSConnectCallback diff --git a/pymongo/synchronous/pool.py b/pymongo/synchronous/pool.py index ebb03dfeb2..45f8af4ec6 100644 --- a/pymongo/synchronous/pool.py +++ b/pymongo/synchronous/pool.py @@ -18,7 +18,6 @@ import collections import os import time -from collections.abc import Mapping, MutableMapping, Sequence from typing import ( TYPE_CHECKING, Any, @@ -76,6 +75,7 @@ from pymongo.synchronous.helpers import _handle_reauth if TYPE_CHECKING: + from collections.abc import Mapping, MutableMapping, Sequence from types import TracebackType from bson import CodecOptions diff --git a/pymongo/synchronous/settings.py b/pymongo/synchronous/settings.py index 301d7ca77d..372f6e7b83 100644 --- a/pymongo/synchronous/settings.py +++ b/pymongo/synchronous/settings.py @@ -31,6 +31,10 @@ from pymongo.pool_options import PoolOptions from pymongo.topology_description import _ServerSelector +if TYPE_CHECKING: + import threading + from collections.abc import Callable, Collection + _IS_SYNC = True diff --git a/pymongo/synchronous/srv_resolver.py b/pymongo/synchronous/srv_resolver.py index 38ddbdafe3..774c36bb48 100644 --- a/pymongo/synchronous/srv_resolver.py +++ b/pymongo/synchronous/srv_resolver.py @@ -18,7 +18,6 @@ import ipaddress import random -from collections.abc import Callable from typing import TYPE_CHECKING, Any from pymongo._psl import SPECIAL_USE_LABELS, _to_punycode, is_public_suffix @@ -27,6 +26,8 @@ from pymongo.errors import ConfigurationError if TYPE_CHECKING: + from collections.abc import Callable + from dns import resolver _IS_SYNC = True diff --git a/pymongo/synchronous/topology.py b/pymongo/synchronous/topology.py index 62c170ef20..df7600b33e 100644 --- a/pymongo/synchronous/topology.py +++ b/pymongo/synchronous/topology.py @@ -18,13 +18,11 @@ import asyncio import os -import queue import random import sys import time import warnings import weakref -from collections.abc import Callable, Mapping from pathlib import Path from typing import TYPE_CHECKING, Any, cast @@ -73,6 +71,9 @@ ) if TYPE_CHECKING: + import queue + from collections.abc import Callable, Mapping + from pymongo.synchronous.settings import TopologySettings from pymongo.typings import _Address diff --git a/pymongo/synchronous/uri_parser.py b/pymongo/synchronous/uri_parser.py index 462f7d23dd..2133795784 100644 --- a/pymongo/synchronous/uri_parser.py +++ b/pymongo/synchronous/uri_parser.py @@ -17,8 +17,7 @@ from __future__ import annotations -from collections.abc import Callable -from typing import Any +from typing import TYPE_CHECKING, Any from pymongo.common import SRV_SERVICE_NAME, _CaseInsensitiveDictionary from pymongo.errors import ConfigurationError, InvalidURI @@ -36,6 +35,9 @@ split_options, ) +if TYPE_CHECKING: + from collections.abc import Callable + _IS_SYNC = True diff --git a/pymongo/topology_shared.py b/pymongo/topology_shared.py index 0dbe001fb0..ce5bd5f9a8 100644 --- a/pymongo/topology_shared.py +++ b/pymongo/topology_shared.py @@ -17,8 +17,6 @@ from __future__ import annotations import queue -import weakref -from collections.abc import Callable, Mapping from typing import TYPE_CHECKING, Any, Generic, TypeVar from pymongo import _csot @@ -29,6 +27,9 @@ from pymongo.topology_description import TOPOLOGY_TYPE, TopologyDescription if TYPE_CHECKING: + import weakref + from collections.abc import Callable, Mapping + from bson import ObjectId from pymongo.server_description import ServerDescription from pymongo.settings_shared import _BaseTopologySettings diff --git a/pymongo/uri_parser_shared.py b/pymongo/uri_parser_shared.py index cf5037b101..db29bb1d3f 100644 --- a/pymongo/uri_parser_shared.py +++ b/pymongo/uri_parser_shared.py @@ -23,7 +23,6 @@ import re import sys import warnings -from collections.abc import Mapping, MutableMapping, Sized from typing import ( TYPE_CHECKING, Any, @@ -41,6 +40,8 @@ from pymongo.errors import ConfigurationError, InvalidURI if TYPE_CHECKING: + from collections.abc import Mapping, MutableMapping, Sized + from pymongo.pyopenssl_context import SSLContext from pymongo.typings import _Address diff --git a/pyproject.toml b/pyproject.toml index 6d099e93c4..ffc826f294 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -219,6 +219,7 @@ extend-select = [ "EXE", # flake8-executable "ASYNC", # flake8-async "TC001", # flake8-type-checking: typing-only first-party imports + "TC003", # flake8-type-checking: typing-only stdlib imports ] ignore = [ "PLR", # Design related pylint codes diff --git a/test/__init__.py b/test/__init__.py index 2d3bc76e5d..5345375e03 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -43,7 +43,6 @@ HAVE_IPADDRESS = True except ImportError: HAVE_IPADDRESS = False -from collections.abc import Callable, Generator from contextlib import contextmanager from functools import partial, wraps from typing import TYPE_CHECKING, Any, overload @@ -84,6 +83,8 @@ from test.version import Version if TYPE_CHECKING: + from collections.abc import Callable, Generator + from pymongo.synchronous.database import Database _IS_SYNC = True diff --git a/test/asynchronous/__init__.py b/test/asynchronous/__init__.py index 59b3168048..d79054c539 100644 --- a/test/asynchronous/__init__.py +++ b/test/asynchronous/__init__.py @@ -43,7 +43,6 @@ HAVE_IPADDRESS = True except ImportError: HAVE_IPADDRESS = False -from collections.abc import Callable, Generator from contextlib import asynccontextmanager, contextmanager from functools import partial, wraps from typing import TYPE_CHECKING, Any, overload @@ -84,6 +83,8 @@ from test.version import Version if TYPE_CHECKING: + from collections.abc import Callable, Generator + from pymongo.asynchronous.database import AsyncDatabase _IS_SYNC = False diff --git a/test/asynchronous/test_encryption.py b/test/asynchronous/test_encryption.py index 15212bf5cc..549f1ddd28 100644 --- a/test/asynchronous/test_encryption.py +++ b/test/asynchronous/test_encryption.py @@ -31,7 +31,6 @@ import traceback import uuid import warnings -from collections.abc import Mapping from threading import Thread from typing import TYPE_CHECKING, Any @@ -115,6 +114,8 @@ from test.version import Version if TYPE_CHECKING: + from collections.abc import Mapping + from pymongo.asynchronous.collection import AsyncCollection _IS_SYNC = False diff --git a/test/asynchronous/test_index_management.py b/test/asynchronous/test_index_management.py index 3fe2be9534..6a53531f74 100644 --- a/test/asynchronous/test_index_management.py +++ b/test/asynchronous/test_index_management.py @@ -21,8 +21,7 @@ import sys import time import uuid -from collections.abc import Mapping -from typing import Any +from typing import TYPE_CHECKING, Any import pytest @@ -38,6 +37,9 @@ from test.asynchronous.unified_format import generate_test_classes, get_test_path from test.utils_shared import AllowListEventListener, OvertCommandListener +if TYPE_CHECKING: + from collections.abc import Mapping + _IS_SYNC = False pytestmark = pytest.mark.search_index diff --git a/test/asynchronous/test_json_util_integration.py b/test/asynchronous/test_json_util_integration.py index cbc307c0c0..b4c86f0bca 100644 --- a/test/asynchronous/test_json_util_integration.py +++ b/test/asynchronous/test_json_util_integration.py @@ -1,12 +1,14 @@ from __future__ import annotations -from collections.abc import MutableMapping -from typing import Any +from typing import TYPE_CHECKING, Any from bson import Binary, Code, DBRef, ObjectId, json_util from bson.binary import USER_DEFINED_SUBTYPE from test.asynchronous import AsyncIntegrationTest +if TYPE_CHECKING: + from collections.abc import MutableMapping + _IS_SYNC = False diff --git a/test/asynchronous/test_session.py b/test/asynchronous/test_session.py index 0e3fb96576..c6e3d58db8 100644 --- a/test/asynchronous/test_session.py +++ b/test/asynchronous/test_session.py @@ -19,10 +19,9 @@ import copy import sys import time -from collections.abc import Callable from inspect import iscoroutinefunction from io import BytesIO -from typing import Any +from typing import TYPE_CHECKING, Any from test.asynchronous.helpers import ExceptionCatchingTask @@ -53,6 +52,9 @@ async_wait_until, ) +if TYPE_CHECKING: + from collections.abc import Callable + _IS_SYNC = False diff --git a/test/asynchronous/unified_format.py b/test/asynchronous/unified_format.py index 3a21c664a0..cbeb819ef2 100644 --- a/test/asynchronous/unified_format.py +++ b/test/asynchronous/unified_format.py @@ -31,7 +31,6 @@ import time import traceback from collections import defaultdict -from collections.abc import Mapping from inspect import iscoroutinefunction from pathlib import Path from typing import TYPE_CHECKING, Any, Optional @@ -112,6 +111,8 @@ from test.version import Version if TYPE_CHECKING: + from collections.abc import Mapping + from bson.objectid import ObjectId from pymongo.typings import _Address diff --git a/test/test_encryption.py b/test/test_encryption.py index 462da30dbc..cd6c7bbd12 100644 --- a/test/test_encryption.py +++ b/test/test_encryption.py @@ -31,7 +31,6 @@ import traceback import uuid import warnings -from collections.abc import Mapping from threading import Thread from typing import TYPE_CHECKING, Any @@ -115,6 +114,8 @@ from test.version import Version if TYPE_CHECKING: + from collections.abc import Mapping + from pymongo.synchronous.collection import Collection _IS_SYNC = True diff --git a/test/test_index_management.py b/test/test_index_management.py index 904c3fe665..1531d10107 100644 --- a/test/test_index_management.py +++ b/test/test_index_management.py @@ -21,8 +21,7 @@ import sys import time import uuid -from collections.abc import Mapping -from typing import Any +from typing import TYPE_CHECKING, Any import pytest @@ -38,6 +37,9 @@ from test.unified_format import generate_test_classes, get_test_path from test.utils_shared import AllowListEventListener, OvertCommandListener +if TYPE_CHECKING: + from collections.abc import Mapping + _IS_SYNC = True pytestmark = pytest.mark.search_index diff --git a/test/test_json_util_integration.py b/test/test_json_util_integration.py index 5772309842..9277b12d99 100644 --- a/test/test_json_util_integration.py +++ b/test/test_json_util_integration.py @@ -1,12 +1,14 @@ from __future__ import annotations -from collections.abc import MutableMapping -from typing import Any +from typing import TYPE_CHECKING, Any from bson import Binary, Code, DBRef, ObjectId, json_util from bson.binary import USER_DEFINED_SUBTYPE from test import IntegrationTest +if TYPE_CHECKING: + from collections.abc import MutableMapping + _IS_SYNC = True diff --git a/test/test_kms_connect.py b/test/test_kms_connect.py index f0873fcd34..084cfae266 100644 --- a/test/test_kms_connect.py +++ b/test/test_kms_connect.py @@ -37,10 +37,9 @@ import threading import time from asyncio.trsock import TransportSocket -from collections.abc import Callable from concurrent.futures import ThreadPoolExecutor from contextlib import contextmanager -from typing import Any +from typing import TYPE_CHECKING, Any from unittest import mock import pytest @@ -59,6 +58,9 @@ from pymongo.ssl_support import get_ssl_context from test.helpers_shared import CA_PEM, CERT_PATH, CLIENT_PEM +if TYPE_CHECKING: + from collections.abc import Callable + pytestmark = [pytest.mark.encryption, pytest.mark.asyncio] _KMS_ADDRESS = ("kms.example.com", 443) diff --git a/test/test_session.py b/test/test_session.py index 2f0a98a933..3b48acc8de 100644 --- a/test/test_session.py +++ b/test/test_session.py @@ -19,10 +19,9 @@ import copy import sys import time -from collections.abc import Callable from inspect import iscoroutinefunction from io import BytesIO -from typing import Any +from typing import TYPE_CHECKING, Any from test.helpers import ExceptionCatchingTask @@ -53,6 +52,9 @@ wait_until, ) +if TYPE_CHECKING: + from collections.abc import Callable + _IS_SYNC = True diff --git a/test/unified_format.py b/test/unified_format.py index 8b6f7c60de..bb8ff71506 100644 --- a/test/unified_format.py +++ b/test/unified_format.py @@ -31,7 +31,6 @@ import time import traceback from collections import defaultdict -from collections.abc import Mapping from inspect import iscoroutinefunction from pathlib import Path from typing import TYPE_CHECKING, Any, Optional @@ -111,6 +110,8 @@ from test.version import Version if TYPE_CHECKING: + from collections.abc import Mapping + from bson.objectid import ObjectId from pymongo.typings import _Address diff --git a/test/unified_format_shared.py b/test/unified_format_shared.py index af64559e6b..7e5595baac 100644 --- a/test/unified_format_shared.py +++ b/test/unified_format_shared.py @@ -26,7 +26,6 @@ import time import types from collections import abc -from collections.abc import MutableMapping from typing import TYPE_CHECKING, Any from bson import ( @@ -93,6 +92,8 @@ from test.utils_shared import CMAPListener, camel_to_snake, parse_collection_options if TYPE_CHECKING: + from collections.abc import MutableMapping + from pymongo.server_description import ServerDescription from pymongo.topology_description import TopologyDescription