From 7a7fa7317f50f1ced576445075c4efc9bf16779f Mon Sep 17 00:00:00 2001 From: Noah Stapp Date: Wed, 11 Nov 2026 15:28:48 -0500 Subject: [PATCH] PYTHON-6259 Enable ruff TC003 rule --- bson/_helpers.py | 6 ++++-- bson/binary.py | 2 +- bson/dbref.py | 6 ++++-- gridfs/asynchronous/grid_file.py | 8 +++++--- gridfs/synchronous/grid_file.py | 8 +++++--- pymongo/__init__.py | 6 ++++-- pymongo/_asyncio_lock.py | 8 +++++--- pymongo/_client_bulk_shared.py | 3 ++- pymongo/_csot.py | 3 ++- pymongo/_op_id.py | 6 ++++-- pymongo/_telemetry.py | 5 +++-- pymongo/asynchronous/aggregation.py | 3 ++- pymongo/asynchronous/auth.py | 3 ++- pymongo/asynchronous/auth_aws.py | 3 ++- pymongo/asynchronous/auth_oidc.py | 5 +++-- pymongo/asynchronous/bulk.py | 3 ++- pymongo/asynchronous/change_stream.py | 3 ++- pymongo/asynchronous/client_bulk.py | 3 ++- pymongo/asynchronous/client_session.py | 4 ++-- pymongo/asynchronous/command_cursor.py | 3 ++- pymongo/asynchronous/command_runner.py | 3 ++- pymongo/asynchronous/cursor.py | 3 ++- pymongo/asynchronous/cursor_base.py | 5 +++-- pymongo/asynchronous/encryption.py | 5 +++-- pymongo/asynchronous/pool.py | 2 +- pymongo/asynchronous/settings.py | 7 +++++-- pymongo/asynchronous/srv_resolver.py | 3 ++- pymongo/asynchronous/topology.py | 5 +++-- pymongo/asynchronous/uri_parser.py | 6 ++++-- pymongo/auth_oidc_shared.py | 3 ++- pymongo/auth_shared.py | 3 ++- pymongo/bulk_shared.py | 3 ++- pymongo/change_stream_shared.py | 3 ++- pymongo/collation.py | 6 ++++-- pymongo/compression_support.py | 6 ++++-- pymongo/daemon.py | 6 ++++-- pymongo/encryption_options.py | 3 ++- pymongo/encryption_shared.py | 6 ++++-- pymongo/errors.py | 3 ++- pymongo/hello.py | 8 +++++--- pymongo/helpers_shared.py | 3 ++- pymongo/message.py | 3 ++- pymongo/monitoring.py | 4 ++-- pymongo/network_layer.py | 5 +++-- pymongo/ocsp_support.py | 3 ++- pymongo/pool_options.py | 3 ++- pymongo/pool_shared.py | 3 ++- pymongo/pyopenssl_context.py | 4 ++-- pymongo/response.py | 2 +- pymongo/server_api.py | 6 ++++-- pymongo/server_description.py | 6 ++++-- pymongo/settings_shared.py | 6 ++++-- pymongo/ssl_support.py | 6 ++++-- pymongo/synchronous/aggregation.py | 3 ++- pymongo/synchronous/auth.py | 3 ++- pymongo/synchronous/auth_aws.py | 3 ++- pymongo/synchronous/auth_oidc.py | 5 +++-- pymongo/synchronous/bulk.py | 3 ++- pymongo/synchronous/change_stream.py | 3 ++- pymongo/synchronous/client_bulk.py | 3 ++- pymongo/synchronous/client_session.py | 4 ++-- pymongo/synchronous/command_cursor.py | 3 ++- pymongo/synchronous/command_runner.py | 3 ++- pymongo/synchronous/cursor.py | 3 ++- pymongo/synchronous/cursor_base.py | 5 +++-- pymongo/synchronous/encryption.py | 5 +++-- pymongo/synchronous/pool.py | 2 +- pymongo/synchronous/settings.py | 7 +++++-- pymongo/synchronous/srv_resolver.py | 3 ++- pymongo/synchronous/topology.py | 5 +++-- pymongo/synchronous/uri_parser.py | 6 ++++-- pymongo/topology_shared.py | 5 +++-- pymongo/uri_parser_shared.py | 3 ++- pyproject.toml | 1 + test/__init__.py | 6 ++++-- test/asynchronous/__init__.py | 6 ++++-- test/asynchronous/test_encryption.py | 6 ++++-- test/asynchronous/test_index_management.py | 6 ++++-- test/asynchronous/test_json_util_integration.py | 6 ++++-- test/asynchronous/test_session.py | 6 ++++-- test/asynchronous/unified_format.py | 6 ++++-- test/test_encryption.py | 6 ++++-- test/test_index_management.py | 6 ++++-- test/test_json_util_integration.py | 6 ++++-- test/test_kms_connect.py | 6 ++++-- test/test_session.py | 6 ++++-- test/unified_format.py | 6 ++++-- test/unified_format_shared.py | 6 ++++-- 88 files changed, 256 insertions(+), 140 deletions(-) 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 a8a90ce1aa..0bff95a017 100644 --- a/gridfs/asynchronous/grid_file.py +++ b/gridfs/asynchronous/grid_file.py @@ -21,9 +21,7 @@ import io import math from collections import abc -from collections.abc import Iterable, Mapping -from types import TracebackType -from typing import Any, NoReturn, Self, cast +from typing import TYPE_CHECKING, Any, NoReturn, Self, cast from bson.int64 import Int64 from bson.objectid import ObjectId @@ -62,6 +60,10 @@ from pymongo.helpers_shared import _check_write_command_response from pymongo.read_preferences import ReadPreference, _ServerMode +if TYPE_CHECKING: + from collections.abc import Iterable, Mapping + from types import TracebackType + _IS_SYNC = False diff --git a/gridfs/synchronous/grid_file.py b/gridfs/synchronous/grid_file.py index 99b805e447..c61a7b31da 100644 --- a/gridfs/synchronous/grid_file.py +++ b/gridfs/synchronous/grid_file.py @@ -21,9 +21,7 @@ import io import math from collections import abc -from collections.abc import Iterable, Mapping -from types import TracebackType -from typing import Any, NoReturn, Self, cast +from typing import TYPE_CHECKING, Any, NoReturn, Self, cast from bson.int64 import Int64 from bson.objectid import ObjectId @@ -62,6 +60,10 @@ from pymongo.synchronous.database import Database from pymongo.synchronous.helpers import next +if TYPE_CHECKING: + from collections.abc import Iterable, Mapping + from types import TracebackType + _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 bc47e93ff7..f2bf4f3a99 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 @@ -33,6 +32,8 @@ from pymongo.write_concern import WriteConcern if TYPE_CHECKING: + from collections.abc import Mapping, MutableMapping + from pymongo.typings import _AgnosticMongoClient, _DocumentOut, _Pipeline 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 d26ddb1dd0..c5f5a0d0ae 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, @@ -57,6 +56,8 @@ from pymongo.write_concern import WriteConcern if TYPE_CHECKING: + from collections.abc import Iterator, Mapping, MutableMapping + from pymongo.asynchronous.collection import AsyncCollection from pymongo.asynchronous.mongo_client import AsyncMongoClient from pymongo.asynchronous.pool import AsyncConnection 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 639eb66e5f..75a1ee9cc1 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, @@ -39,6 +38,8 @@ from pymongo.asynchronous.helpers import _handle_reauth if TYPE_CHECKING: + from collections.abc import Mapping, MutableMapping + from pymongo.asynchronous.mongo_client import AsyncMongoClient from pymongo.asynchronous.pool import AsyncConnection from pymongo._client_bulk_shared import ( diff --git a/pymongo/asynchronous/client_session.py b/pymongo/asynchronous/client_session.py index 615e228671..af470e2d24 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, @@ -176,6 +174,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 95d08a67bb..270aa90d0e 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, @@ -94,6 +92,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 diff --git a/pymongo/asynchronous/pool.py b/pymongo/asynchronous/pool.py index 2f17aa2b2b..b24d746547 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, @@ -74,6 +73,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 ad70bd03e3..d8a08ac1c3 100644 --- a/pymongo/asynchronous/settings.py +++ b/pymongo/asynchronous/settings.py @@ -16,8 +16,7 @@ from __future__ import annotations -import threading -from collections.abc import Callable, Collection +from typing import TYPE_CHECKING from bson.objectid import ObjectId from pymongo import common @@ -28,6 +27,10 @@ from pymongo.settings_shared import _BaseTopologySettings 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 5ab2938385..ba78ffe21e 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 import CodecOptions @@ -34,6 +33,8 @@ from pymongo.typings import _DocumentType if TYPE_CHECKING: + from collections.abc import Mapping + from pymongo.typings import ( _AgnosticClientSession, _AgnosticCollection, 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 1a78c70b43..736beeec7f 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 e0c23b6773..f33c999b6d 100644 --- a/pymongo/encryption_shared.py +++ b/pymongo/encryption_shared.py @@ -18,8 +18,7 @@ import contextlib import enum -from collections.abc import Iterator -from typing import Any +from typing import TYPE_CHECKING, Any try: from pymongocrypt.mongocrypt import MongoCryptOptions # type:ignore[import-untyped] @@ -30,6 +29,9 @@ from pymongo.errors import EncryptionError 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 e6e97bfbae..83ccc3b876 100644 --- a/pymongo/hello.py +++ b/pymongo/hello.py @@ -17,16 +17,18 @@ from __future__ import annotations import copy -import datetime import itertools -from collections.abc import Mapping -from typing import Any, Generic +from typing import TYPE_CHECKING, Any, Generic from bson.objectid import ObjectId from pymongo import common from pymongo.server_type import SERVER_TYPE from pymongo.typings import ClusterTime, _DocumentType +if TYPE_CHECKING: + import datetime + from collections.abc import Mapping + def _get_server_type(doc: Mapping[str, Any]) -> int: """Determine the server type from a hello response.""" 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 d89b467a4a..2a89832c34 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, @@ -58,6 +57,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.read_concern import ReadConcern from pymongo.typings import ( diff --git a/pymongo/monitoring.py b/pymongo/monitoring.py index 57bd34d08c..5a086147fe 100644 --- a/pymongo/monitoring.py +++ b/pymongo/monitoring.py @@ -188,9 +188,7 @@ 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 bson.objectid import ObjectId @@ -199,6 +197,8 @@ def connection_checked_in(self, event): from pymongo.typings import _Address, _DocumentOut if TYPE_CHECKING: + import datetime + from collections.abc import Mapping, Sequence from datetime import timedelta from pymongo.server_description import ServerDescription 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 040b62a4e8..479eee73f3 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, @@ -68,6 +67,8 @@ SSLErrors = (PYSSLError, SSLError) if TYPE_CHECKING: + from collections.abc import MutableMapping + from bson.objectid import ObjectId from pymongo.compression_support import SnappyContext, ZlibContext, ZstdContext from pymongo.pyopenssl_context import _sslConn 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 836436e450..b7113cf468 100644 --- a/pymongo/server_description.py +++ b/pymongo/server_description.py @@ -21,8 +21,7 @@ import time import warnings -from collections.abc import Mapping -from typing import Any +from typing import TYPE_CHECKING, Any from bson import EPOCH_NAIVE from bson.objectid import ObjectId @@ -30,6 +29,9 @@ from pymongo.server_type import SERVER_TYPE 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 ab50a96522..50d06beeda 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, @@ -57,6 +56,8 @@ from pymongo.write_concern import WriteConcern if TYPE_CHECKING: + from collections.abc import Iterator, Mapping, MutableMapping + from pymongo.synchronous.collection import Collection from pymongo.synchronous.mongo_client import MongoClient from pymongo.synchronous.pool import Connection 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 b649313932..379a78b253 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, @@ -39,6 +38,8 @@ from pymongo.synchronous.helpers import _handle_reauth if TYPE_CHECKING: + from collections.abc import Mapping, MutableMapping + from pymongo.synchronous.mongo_client import MongoClient from pymongo.synchronous.pool import Connection from pymongo._client_bulk_shared import ( diff --git a/pymongo/synchronous/client_session.py b/pymongo/synchronous/client_session.py index ba7ee39450..e8c0066a82 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, @@ -175,6 +173,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.synchronous.mongo_client import MongoClient 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 4eb9df0228..9ec0137fe3 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, @@ -93,6 +91,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 diff --git a/pymongo/synchronous/pool.py b/pymongo/synchronous/pool.py index f746631616..2af133e237 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, @@ -74,6 +73,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 c6a0187276..499170ff68 100644 --- a/pymongo/synchronous/settings.py +++ b/pymongo/synchronous/settings.py @@ -16,8 +16,7 @@ from __future__ import annotations -import threading -from collections.abc import Callable, Collection +from typing import TYPE_CHECKING from bson.objectid import ObjectId from pymongo import common @@ -28,6 +27,10 @@ from pymongo.synchronous.pool import Pool 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 e6775b8668..a6801c6866 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, @@ -42,6 +41,8 @@ from pymongo.typings import _Address if TYPE_CHECKING: + from collections.abc import Mapping, MutableMapping, Sized + from pymongo.pyopenssl_context import SSLContext diff --git a/pyproject.toml b/pyproject.toml index 5fb4c85e6a..028c234376 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -218,6 +218,7 @@ extend-select = [ "YTT", # flake8-2020 "EXE", # flake8-executable "ASYNC", # flake8-async + "TC003", # flake8-type-checking: stdlib imports used only for typing. ] ignore = [ "PLR", # Design related pylint codes diff --git a/test/__init__.py b/test/__init__.py index 2220bf3661..d5a2f988d4 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -43,10 +43,9 @@ 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 Any, overload +from typing import TYPE_CHECKING, Any, overload from unittest import SkipTest from urllib.parse import quote_plus @@ -84,6 +83,9 @@ ) from test.version import Version +if TYPE_CHECKING: + from collections.abc import Callable, Generator + _IS_SYNC = True diff --git a/test/asynchronous/__init__.py b/test/asynchronous/__init__.py index 57ee8e9a99..28eea1cd29 100644 --- a/test/asynchronous/__init__.py +++ b/test/asynchronous/__init__.py @@ -43,10 +43,9 @@ 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 Any, overload +from typing import TYPE_CHECKING, Any, overload from unittest import SkipTest from urllib.parse import quote_plus @@ -84,6 +83,9 @@ ) from test.version import Version +if TYPE_CHECKING: + from collections.abc import Callable, Generator + _IS_SYNC = False diff --git a/test/asynchronous/test_encryption.py b/test/asynchronous/test_encryption.py index 535d56d779..19bd1e9a1a 100644 --- a/test/asynchronous/test_encryption.py +++ b/test/asynchronous/test_encryption.py @@ -31,9 +31,8 @@ import traceback import uuid import warnings -from collections.abc import Mapping from threading import Thread -from typing import Any, Optional +from typing import TYPE_CHECKING, Any, Optional import pytest @@ -115,6 +114,9 @@ ) from test.version import Version +if TYPE_CHECKING: + from collections.abc import Mapping + _IS_SYNC = False pytestmark = pytest.mark.encryption 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 94ce837726..25d08efa5f 100644 --- a/test/asynchronous/unified_format.py +++ b/test/asynchronous/unified_format.py @@ -31,10 +31,9 @@ import time import traceback from collections import defaultdict -from collections.abc import Mapping from inspect import iscoroutinefunction from pathlib import Path -from typing import Any, Optional +from typing import TYPE_CHECKING, Any, Optional import pytest @@ -113,6 +112,9 @@ ) from test.version import Version +if TYPE_CHECKING: + from collections.abc import Mapping + _IS_SYNC = False IS_INTERRUPTED = False diff --git a/test/test_encryption.py b/test/test_encryption.py index c7717737d4..da193cea37 100644 --- a/test/test_encryption.py +++ b/test/test_encryption.py @@ -31,9 +31,8 @@ import traceback import uuid import warnings -from collections.abc import Mapping from threading import Thread -from typing import Any, Optional +from typing import TYPE_CHECKING, Any, Optional import pytest @@ -115,6 +114,9 @@ ) from test.version import Version +if TYPE_CHECKING: + from collections.abc import Mapping + _IS_SYNC = True pytestmark = pytest.mark.encryption 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 f39e97365e..8e8cf5d66a 100644 --- a/test/unified_format.py +++ b/test/unified_format.py @@ -31,10 +31,9 @@ import time import traceback from collections import defaultdict -from collections.abc import Mapping from inspect import iscoroutinefunction from pathlib import Path -from typing import Any, Optional +from typing import TYPE_CHECKING, Any, Optional import pytest @@ -112,6 +111,9 @@ from test.utils_spec_runner import SpecRunnerThread from test.version import Version +if TYPE_CHECKING: + from collections.abc import Mapping + _IS_SYNC = True IS_INTERRUPTED = False diff --git a/test/unified_format_shared.py b/test/unified_format_shared.py index 4a328467b3..08d6ccaa30 100644 --- a/test/unified_format_shared.py +++ b/test/unified_format_shared.py @@ -26,8 +26,7 @@ import time import types from collections import abc -from collections.abc import MutableMapping -from typing import Any +from typing import TYPE_CHECKING, Any from bson import ( RE_TYPE, @@ -94,6 +93,9 @@ ) from test.utils_shared import CMAPListener, camel_to_snake, parse_collection_options +if TYPE_CHECKING: + from collections.abc import MutableMapping + JSON_OPTS = json_util.JSONOptions(tz_aware=False) IS_INTERRUPTED = False