Skip to content

Commit 1f64ecd

Browse files
authored
PYTHON-6158 Clean up version-gated code paths (#3112)
1 parent 0cc64d6 commit 1f64ecd

21 files changed

Lines changed: 46 additions & 165 deletions

‎pymongo/_asyncio_task.py‎

Lines changed: 0 additions & 49 deletions
This file was deleted.

‎pymongo/asynchronous/helpers.py‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import builtins
2121
import functools
2222
import socket
23-
import sys
2423
from typing import (
2524
Any,
2625
Callable,
@@ -94,15 +93,5 @@ async def _getaddrinfo(
9493
return socket.getaddrinfo(host, port, **kwargs)
9594

9695

97-
if sys.version_info >= (3, 10):
98-
anext = builtins.anext
99-
aiter = builtins.aiter
100-
else:
101-
102-
async def anext(cls: Any) -> Any:
103-
"""Compatibility function until we drop 3.9 support: https://docs.python.org/3/library/functions.html#anext."""
104-
return await cls.__anext__()
105-
106-
def aiter(cls: Any) -> Any:
107-
"""Compatibility function until we drop 3.9 support: https://docs.python.org/3/library/functions.html#anext."""
108-
return cls.__aiter__()
96+
anext = builtins.anext
97+
aiter = builtins.aiter

‎pymongo/cursor_shared.py‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,14 @@
4343
from pymongo.write_concern import validate_boolean
4444

4545
if TYPE_CHECKING:
46-
import sys
46+
from typing import Self
4747

4848
from _typeshed import SupportsItems
4949

5050
from bson.codec_options import CodecOptions
5151
from pymongo.message import _OpMsg
5252
from pymongo.read_preferences import _ServerMode
5353

54-
if sys.version_info >= (3, 11):
55-
from typing import Self
56-
else:
57-
from typing_extensions import Self
58-
5954
_CURSOR_DOC_FIELDS = {"cursor": {"firstBatch": 1, "nextBatch": 1}}
6055

6156

‎pymongo/network_layer.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
)
3434

3535
from pymongo import _csot, ssl_support
36-
from pymongo._asyncio_task import create_task
3736
from pymongo.common import MAX_MESSAGE_SIZE
3837
from pymongo.compression_support import decompress
3938
from pymongo.errors import ProtocolError, _OperationCancelled
@@ -711,8 +710,8 @@ async def async_receive_message(
711710
# timeouts on AWS Lambda and other FaaS environments.
712711
timeout = max(deadline - time.monotonic(), 0)
713712

714-
cancellation_task = create_task(_poll_cancellation(conn))
715-
read_task = create_task(conn.conn.get_conn.read(request_id, max_message_size))
713+
cancellation_task = asyncio.create_task(_poll_cancellation(conn))
714+
read_task = asyncio.create_task(conn.conn.get_conn.read(request_id, max_message_size))
716715
tasks = [read_task, cancellation_task]
717716
try:
718717
done, pending = await asyncio.wait(

‎pymongo/periodic_executor.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from typing import Any, Optional
2525

2626
from pymongo import _csot, _op_id
27-
from pymongo._asyncio_task import create_task
2827
from pymongo.lock import _create_lock
2928

3029
_IS_SYNC = False
@@ -65,9 +64,9 @@ def open(self) -> None:
6564
self._stopped = False
6665

6766
if self._task is None or (
68-
self._task.done() and not self._task.cancelled() and not self._task.cancelling() # type: ignore[unused-ignore, attr-defined]
67+
self._task.done() and not self._task.cancelled() and not self._task.cancelling()
6968
):
70-
self._task = create_task(self._run(), name=self._name)
69+
self._task = asyncio.create_task(self._run(), name=self._name)
7170

7271
def close(self, dummy: Any = None) -> None:
7372
"""Stop. To restart, call open().
@@ -98,7 +97,7 @@ async def _run(self) -> None:
9897
_csot.reset_all()
9998
_op_id.reset()
10099
while not self._stopped:
101-
if self._task and self._task.cancelling(): # type: ignore[unused-ignore, attr-defined]
100+
if self._task and self._task.cancelling():
102101
raise asyncio.CancelledError
103102
try:
104103
if not await self._target():

‎pymongo/pool_options.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,7 @@ def _update_metadata(self, driver: DriverInfo) -> None:
472472
# raises.
473473
names = metadata["driver"]["name"].split("|")
474474
versions = metadata["driver"]["version"].split("|")
475-
if sys.version_info >= (3, 10):
476-
pairs = zip(names, versions, strict=True)
477-
else:
478-
pairs = zip(names, versions)
475+
pairs = zip(names, versions, strict=True)
479476
if (driver.name, driver.version) in pairs:
480477
self.__appended_drivers.add(driver)
481478

‎pymongo/pool_shared.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,11 @@ async def _configured_protocol_interface(
417417

418418
host = address[0]
419419
# asyncio does not support TLS session resumption natively (cpython#79152,
420-
# closed without a fix). On Python 3.11+ SSLProtocol.__init__ calls
421-
# wrap_bio() synchronously before the first event-loop yield, so setting
422-
# sslobject_class is race-free. Session injection is skipped on older
423-
# Python versions. (The async path always uses stdlib ssl, never PyOpenSSL.)
424-
if ssl_session_cache is not None and sys.version_info >= (3, 11):
420+
# closed without a fix). SSLProtocol.__init__ calls wrap_bio()
421+
# synchronously before the first event-loop yield, so setting
422+
# sslobject_class is race-free. (The async path always uses stdlib ssl,
423+
# never PyOpenSSL.)
424+
if ssl_session_cache is not None:
425425
session = ssl_session_cache[0]
426426
if session is not None:
427427
_session = session

‎pymongo/synchronous/helpers.py‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import builtins
2121
import functools
2222
import socket
23-
import sys
2423
from typing import (
2524
Any,
2625
Callable,
@@ -94,15 +93,5 @@ def _getaddrinfo(
9493
return socket.getaddrinfo(host, port, **kwargs)
9594

9695

97-
if sys.version_info >= (3, 10):
98-
next = builtins.next
99-
iter = builtins.iter
100-
else:
101-
102-
def next(cls: Any) -> Any:
103-
"""Compatibility function until we drop 3.9 support: https://docs.python.org/3/library/functions.html#next."""
104-
return cls.__next__()
105-
106-
def iter(cls: Any) -> Any:
107-
"""Compatibility function until we drop 3.9 support: https://docs.python.org/3/library/functions.html#next."""
108-
return cls.__iter__()
96+
next = builtins.next
97+
iter = builtins.iter

‎test/asynchronous/helpers.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
from bson import SON
2626
from pymongo import common
27-
from pymongo._asyncio_task import create_task
2827
from pymongo.read_preferences import ReadPreference
2928

3029
_IS_SYNC = False
@@ -140,7 +139,7 @@ def __init__(self, **kwargs):
140139
if not _IS_SYNC:
141140

142141
async def start(self):
143-
self.task = create_task(self.run(), name=self.name)
142+
self.task = asyncio.create_task(self.run(), name=self.name)
144143

145144
async def join(self, timeout: Optional[float] = None): # type: ignore[override]
146145
if self.task is not None:

‎test/asynchronous/test_client.py‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,17 +1678,15 @@ async def test_contextlib(self):
16781678
# pool
16791679
self.assertEqual(1, len((await async_get_pool(client)).conns))
16801680

1681-
# contextlib async support was added in Python 3.10
1682-
if _IS_SYNC or sys.version_info >= (3, 10):
1683-
async with contextlib.aclosing(client):
1684-
self.assertEqual("bar", (await client.pymongo_test.coll.find_one())["foo"])
1685-
with self.assertRaises(InvalidOperation):
1686-
await client.pymongo_test.coll.find_one()
1687-
client = await self.async_rs_or_single_client()
1688-
async with client as client:
1689-
self.assertEqual("bar", (await client.pymongo_test.coll.find_one())["foo"])
1690-
with self.assertRaises(InvalidOperation):
1691-
await client.pymongo_test.coll.find_one()
1681+
async with contextlib.aclosing(client):
1682+
self.assertEqual("bar", (await client.pymongo_test.coll.find_one())["foo"])
1683+
with self.assertRaises(InvalidOperation):
1684+
await client.pymongo_test.coll.find_one()
1685+
client = await self.async_rs_or_single_client()
1686+
async with client as client:
1687+
self.assertEqual("bar", (await client.pymongo_test.coll.find_one())["foo"])
1688+
with self.assertRaises(InvalidOperation):
1689+
await client.pymongo_test.coll.find_one()
16921690

16931691
@async_client_context.require_sync
16941692
def test_interrupt_signal(self):

0 commit comments

Comments
 (0)