3232import warnings
3333from inspect import iscoroutinefunction
3434
35- import pytest
36-
35+ from pymongo .synchronous .uri_parser import parse_uri
3736from pymongo .encryption_options import _HAVE_PYMONGOCRYPT
3837from pymongo .errors import AutoReconnect
39- from pymongo .synchronous .uri_parser import parse_uri
4038
4139try :
4240 import ipaddress
4341
4442 HAVE_IPADDRESS = True
4543except ImportError :
4644 HAVE_IPADDRESS = False
47- from contextlib import contextmanager
45+ from contextlib import contextmanager , contextmanager
4846from functools import partial , wraps
4947from typing import Any , Callable , Dict , Generator , overload
5048from unittest import SkipTest
5351import pymongo
5452import pymongo .errors
5553from bson .son import SON
54+ from pymongo .synchronous .database import Database
55+ from pymongo .synchronous .mongo_client import MongoClient
5656from pymongo .common import partition_node
5757from pymongo .hello import HelloCompat
5858from pymongo .server_api import ServerApi
5959from pymongo .ssl_support import HAVE_SSL , _ssl # type:ignore[attr-defined]
60- from pymongo .synchronous .database import Database
61- from pymongo .synchronous .mongo_client import MongoClient
6260
6361sys .path [0 :0 ] = ["" ]
6462
8684
8785_IS_SYNC = True
8886
89- import bson
90-
9187# Skip tests when using Rust BSON extension for features not yet implemented
92- skip_if_rust_bson = pytest .mark .skipif (
93- bson .get_bson_implementation () == "rust" ,
94- reason = "Feature not yet implemented in Rust BSON extension" ,
95- )
88+ # Import pytest lazily to avoid requiring it for integration tests
89+ try :
90+ import bson
91+ import pytest
92+
93+ skip_if_rust_bson = pytest .mark .skipif (
94+ bson .get_bson_implementation () == "rust" ,
95+ reason = "Feature not yet implemented in Rust BSON extension" ,
96+ )
97+ except ImportError :
98+ # pytest not available, define a no-op decorator
99+ def skip_if_rust_bson (func ):
100+ return func
96101
97102
98103def _connection_string (h ):
@@ -1016,7 +1021,9 @@ def _unmanaged_async_mongo_client(
10161021 client ._connect ()
10171022 return client
10181023
1019- def _async_mongo_client (self , host , port , authenticate = True , directConnection = None , ** kwargs ):
1024+ def _async_mongo_client (
1025+ self , host , port , authenticate = True , directConnection = None , ** kwargs
1026+ ):
10201027 """Create a new client over SSL/TLS if necessary."""
10211028 host = host or client_context .host
10221029 port = port or client_context .port
@@ -1063,7 +1070,9 @@ def unmanaged_single_client(
10631070 return cls ._unmanaged_async_mongo_client (h , p , directConnection = True , ** kwargs )
10641071
10651072 @classmethod
1066- def unmanaged_rs_client (cls , h : Any = None , p : Any = None , ** kwargs : Any ) -> MongoClient [dict ]:
1073+ def unmanaged_rs_client (
1074+ cls , h : Any = None , p : Any = None , ** kwargs : Any
1075+ ) -> MongoClient [dict ]:
10671076 """Connect to the replica set and authenticate if necessary."""
10681077 return cls ._unmanaged_async_mongo_client (h , p , ** kwargs )
10691078
@@ -1092,17 +1101,25 @@ def single_client_noauth(
10921101 self , h : Any = None , p : Any = None , ** kwargs : Any
10931102 ) -> MongoClient [dict ]:
10941103 """Make a direct connection. Don't authenticate."""
1095- return self ._async_mongo_client (h , p , authenticate = False , directConnection = True , ** kwargs )
1104+ return self ._async_mongo_client (
1105+ h , p , authenticate = False , directConnection = True , ** kwargs
1106+ )
10961107
1097- def single_client (self , h : Any = None , p : Any = None , ** kwargs : Any ) -> MongoClient [dict ]:
1108+ def single_client (
1109+ self , h : Any = None , p : Any = None , ** kwargs : Any
1110+ ) -> MongoClient [dict ]:
10981111 """Make a direct connection, and authenticate if necessary."""
10991112 return self ._async_mongo_client (h , p , directConnection = True , ** kwargs )
11001113
1101- def rs_client_noauth (self , h : Any = None , p : Any = None , ** kwargs : Any ) -> MongoClient [dict ]:
1114+ def rs_client_noauth (
1115+ self , h : Any = None , p : Any = None , ** kwargs : Any
1116+ ) -> MongoClient [dict ]:
11021117 """Connect to the replica set. Don't authenticate."""
11031118 return self ._async_mongo_client (h , p , authenticate = False , ** kwargs )
11041119
1105- def rs_client (self , h : Any = None , p : Any = None , ** kwargs : Any ) -> MongoClient [dict ]:
1120+ def rs_client (
1121+ self , h : Any = None , p : Any = None , ** kwargs : Any
1122+ ) -> MongoClient [dict ]:
11061123 """Connect to the replica set and authenticate if necessary."""
11071124 return self ._async_mongo_client (h , p , ** kwargs )
11081125
@@ -1115,7 +1132,9 @@ def rs_or_single_client_noauth(
11151132 """
11161133 return self ._async_mongo_client (h , p , authenticate = False , ** kwargs )
11171134
1118- def rs_or_single_client (self , h : Any = None , p : Any = None , ** kwargs : Any ) -> MongoClient [Any ]:
1135+ def rs_or_single_client (
1136+ self , h : Any = None , p : Any = None , ** kwargs : Any
1137+ ) -> MongoClient [Any ]:
11191138 """Connect to the replica set if there is one, otherwise the standalone.
11201139
11211140 Authenticates if necessary.
@@ -1131,7 +1150,9 @@ def simple_client(self, h: Any = None, p: Any = None, **kwargs: Any) -> MongoCli
11311150 return client
11321151
11331152 @classmethod
1134- def unmanaged_simple_client (cls , h : Any = None , p : Any = None , ** kwargs : Any ) -> MongoClient :
1153+ def unmanaged_simple_client (
1154+ cls , h : Any = None , p : Any = None , ** kwargs : Any
1155+ ) -> MongoClient :
11351156 if not h and not p :
11361157 client = MongoClient (** kwargs )
11371158 else :
0 commit comments