Skip to content

Commit 102d90e

Browse files
committed
Make pytest import lazy in test/__init__.py
The integration tests import from test/__init__.py but don't have pytest installed. Wrap the pytest import and skip_if_rust_bson marker definition in a try/except to gracefully handle when pytest is not available.
1 parent efe3fd2 commit 102d90e

2 files changed

Lines changed: 54 additions & 28 deletions

File tree

test/__init__.py

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,17 @@
3232
import warnings
3333
from inspect import iscoroutinefunction
3434

35-
import pytest
36-
35+
from pymongo.synchronous.uri_parser import parse_uri
3736
from pymongo.encryption_options import _HAVE_PYMONGOCRYPT
3837
from pymongo.errors import AutoReconnect
39-
from pymongo.synchronous.uri_parser import parse_uri
4038

4139
try:
4240
import ipaddress
4341

4442
HAVE_IPADDRESS = True
4543
except ImportError:
4644
HAVE_IPADDRESS = False
47-
from contextlib import contextmanager
45+
from contextlib import contextmanager, contextmanager
4846
from functools import partial, wraps
4947
from typing import Any, Callable, Dict, Generator, overload
5048
from unittest import SkipTest
@@ -53,12 +51,12 @@
5351
import pymongo
5452
import pymongo.errors
5553
from bson.son import SON
54+
from pymongo.synchronous.database import Database
55+
from pymongo.synchronous.mongo_client import MongoClient
5656
from pymongo.common import partition_node
5757
from pymongo.hello import HelloCompat
5858
from pymongo.server_api import ServerApi
5959
from 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

6361
sys.path[0:0] = [""]
6462

@@ -86,13 +84,20 @@
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

98103
def _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:

test/asynchronous/__init__.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
import warnings
3333
from inspect import iscoroutinefunction
3434

35-
import pytest
36-
3735
from pymongo.asynchronous.uri_parser import parse_uri
3836
from pymongo.encryption_options import _HAVE_PYMONGOCRYPT
3937
from pymongo.errors import AutoReconnect
@@ -86,13 +84,20 @@
8684

8785
_IS_SYNC = False
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

98103
def _connection_string(h):

0 commit comments

Comments
 (0)