Skip to content

Commit

Permalink
Only install async-timeout for Python < 3.11 (aio-libs#7556)
Browse files Browse the repository at this point in the history
## What do these changes do?
Replace `async-timeout` with `asyncio.timeout` for Python 3.11+.

Overall the change isn't as clean as I would have liked it to be. That's
mostly a result of the name conflict with `timeout`. I'm open for
suggestions on how to improve it.

## Are there changes in behavior for the user?
`async-timeout` won't be a dependency for Python 3.11+ any longer.

## Related issue number

Fixes: aio-libs#7502
  • Loading branch information
cdce8p committed Aug 26, 2023
1 parent cf97e5b commit db2c274
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGES/7502.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Only install async-timeout for Python < 3.11.
8 changes: 6 additions & 2 deletions aiohttp/client_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import asyncio
import dataclasses
import sys
from typing import Any, Final, Optional, cast

import async_timeout

from .client_exceptions import ClientError
from .client_reqrep import ClientResponse
from .helpers import call_later, set_result
Expand All @@ -26,6 +25,11 @@
JSONEncoder,
)

if sys.version_info >= (3, 11):
import asyncio as async_timeout
else:
import async_timeout


@dataclasses.dataclass(frozen=True)
class ClientWSTimeout:
Expand Down
6 changes: 5 additions & 1 deletion aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,18 @@
from urllib.parse import quote
from urllib.request import getproxies, proxy_bypass

import async_timeout
from multidict import CIMultiDict, MultiDict, MultiDictProxy
from yarl import URL

from . import hdrs
from .log import client_logger
from .typedefs import PathLike # noqa

if sys.version_info >= (3, 11):
import asyncio as async_timeout
else:
import async_timeout

__all__ = ("BasicAuth", "ChainMapProxy", "ETag")

PY_310 = sys.version_info >= (3, 10)
Expand Down
7 changes: 6 additions & 1 deletion aiohttp/web_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import dataclasses
import hashlib
import json
import sys
from typing import Any, Final, Iterable, Optional, Tuple, cast

import async_timeout
from multidict import CIMultiDict

from . import hdrs
Expand All @@ -32,6 +32,11 @@
from .web_request import BaseRequest
from .web_response import StreamResponse

if sys.version_info >= (3, 11):
import asyncio as async_timeout
else:
import async_timeout

__all__ = (
"WebSocketResponse",
"WebSocketReady",
Expand Down
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ aiodns==3.0.0 ; sys_platform == "linux" or sys_platform == "darwin"
# via -r requirements/runtime-deps.in
aiosignal==1.3.1
# via -r requirements/runtime-deps.in
async-timeout==4.0.3
async-timeout==4.0.3 ; python_version < "3.11"
# via -r requirements/runtime-deps.in
brotli==1.0.9
# via -r requirements/runtime-deps.in
Expand Down
2 changes: 1 addition & 1 deletion requirements/constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ aiosignal==1.3.1
# via -r requirements/runtime-deps.in
alabaster==0.7.13
# via sphinx
async-timeout==4.0.3
async-timeout==4.0.3 ; python_version < "3.11"
# via
# -r requirements/runtime-deps.in
# aioredis
Expand Down
2 changes: 1 addition & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ aiosignal==1.3.1
# via -r requirements/runtime-deps.in
alabaster==0.7.13
# via sphinx
async-timeout==4.0.3
async-timeout==4.0.3 ; python_version < "3.11"
# via
# -r requirements/runtime-deps.in
# aioredis
Expand Down
2 changes: 1 addition & 1 deletion requirements/lint.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
aioredis==2.0.1
# via -r requirements/lint.in
async-timeout==4.0.3
async-timeout==4.0.3 ; python_version < "3.11"
# via aioredis
cfgv==3.3.1
# via pre-commit
Expand Down
2 changes: 1 addition & 1 deletion requirements/runtime-deps.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

charset-normalizer >=2.0, < 4.0
multidict >=4.5, < 7.0
async-timeout >= 4.0, < 5.0
async-timeout >= 4.0, < 5.0 ; python_version < "3.11"
yarl >= 1.0, < 2.0
frozenlist >= 1.1.1
aiosignal >= 1.1.2
Expand Down
2 changes: 1 addition & 1 deletion requirements/runtime-deps.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ aiodns==3.0.0 ; sys_platform == "linux" or sys_platform == "darwin"
# via -r requirements/runtime-deps.in
aiosignal==1.3.1
# via -r requirements/runtime-deps.in
async-timeout==4.0.3
async-timeout==4.0.3 ; python_version < "3.11"
# via -r requirements/runtime-deps.in
brotli==1.0.9
# via -r requirements/runtime-deps.in
Expand Down
2 changes: 1 addition & 1 deletion requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ aiodns==3.0.0 ; sys_platform == "linux" or sys_platform == "darwin"
# via -r requirements/runtime-deps.in
aiosignal==1.3.1
# via -r requirements/runtime-deps.in
async-timeout==4.0.3
async-timeout==4.0.3 ; python_version < "3.11"
# via -r requirements/runtime-deps.in
brotli==1.0.9
# via -r requirements/runtime-deps.in
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ include_package_data = True
install_requires =
charset-normalizer >=2.0, < 4.0
multidict >=4.5, < 7.0
async-timeout >= 4.0, < 5.0
async-timeout >= 4.0, < 5.0 ; python_version < "3.11"
yarl >= 1.0, < 2.0
frozenlist >= 1.1.1
aiosignal >= 1.1.2
Expand Down
7 changes: 6 additions & 1 deletion tests/test_client_ws_functional.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# type: ignore
import asyncio
import sys
from typing import Any

import async_timeout
import pytest

import aiohttp
from aiohttp import hdrs, web
from aiohttp.client_ws import ClientWSTimeout

if sys.version_info >= (3, 11):
import asyncio as async_timeout
else:
import async_timeout


async def test_send_recv_text(aiohttp_client: Any) -> None:
async def handler(request):
Expand Down
21 changes: 17 additions & 4 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import datetime
import gc
import platform
import sys
import weakref
from math import ceil, modf
from pathlib import Path
Expand Down Expand Up @@ -448,25 +449,37 @@ def test_ceil_call_later_no_timeout() -> None:

async def test_ceil_timeout_none(loop) -> None:
async with helpers.ceil_timeout(None) as cm:
assert cm.deadline is None
if sys.version_info >= (3, 11):
assert cm.when() is None
else:
assert cm.deadline is None


async def test_ceil_timeout_round(loop) -> None:
async with helpers.ceil_timeout(7.5) as cm:
frac, integer = modf(cm.deadline)
if sys.version_info >= (3, 11):
frac, integer = modf(cm.when())
else:
frac, integer = modf(cm.deadline)
assert frac == 0


async def test_ceil_timeout_small(loop) -> None:
async with helpers.ceil_timeout(1.1) as cm:
frac, integer = modf(cm.deadline)
if sys.version_info >= (3, 11):
frac, integer = modf(cm.when())
else:
frac, integer = modf(cm.deadline)
# a chance for exact integer with zero fraction is negligible
assert frac != 0


async def test_ceil_timeout_small_with_overriden_threshold(loop) -> None:
async with helpers.ceil_timeout(1.5, ceil_threshold=1) as cm:
frac, integer = modf(cm.deadline)
if sys.version_info >= (3, 11):
frac, integer = modf(cm.when())
else:
frac, integer = modf(cm.deadline)
assert frac == 0


Expand Down

0 comments on commit db2c274

Please sign in to comment.