Skip to content

Commit db2c274

Browse files
authored
Only install async-timeout for Python < 3.11 (#7556)
## 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: #7502
1 parent cf97e5b commit db2c274

File tree

14 files changed

+49
-17
lines changed

14 files changed

+49
-17
lines changed

CHANGES/7502.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Only install async-timeout for Python < 3.11.

aiohttp/client_ws.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
import asyncio
44
import dataclasses
5+
import sys
56
from typing import Any, Final, Optional, cast
67

7-
import async_timeout
8-
98
from .client_exceptions import ClientError
109
from .client_reqrep import ClientResponse
1110
from .helpers import call_later, set_result
@@ -26,6 +25,11 @@
2625
JSONEncoder,
2726
)
2827

28+
if sys.version_info >= (3, 11):
29+
import asyncio as async_timeout
30+
else:
31+
import async_timeout
32+
2933

3034
@dataclasses.dataclass(frozen=True)
3135
class ClientWSTimeout:

aiohttp/helpers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,18 @@
5050
from urllib.parse import quote
5151
from urllib.request import getproxies, proxy_bypass
5252

53-
import async_timeout
5453
from multidict import CIMultiDict, MultiDict, MultiDictProxy
5554
from yarl import URL
5655

5756
from . import hdrs
5857
from .log import client_logger
5958
from .typedefs import PathLike # noqa
6059

60+
if sys.version_info >= (3, 11):
61+
import asyncio as async_timeout
62+
else:
63+
import async_timeout
64+
6165
__all__ = ("BasicAuth", "ChainMapProxy", "ETag")
6266

6367
PY_310 = sys.version_info >= (3, 10)

aiohttp/web_ws.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import dataclasses
55
import hashlib
66
import json
7+
import sys
78
from typing import Any, Final, Iterable, Optional, Tuple, cast
89

9-
import async_timeout
1010
from multidict import CIMultiDict
1111

1212
from . import hdrs
@@ -32,6 +32,11 @@
3232
from .web_request import BaseRequest
3333
from .web_response import StreamResponse
3434

35+
if sys.version_info >= (3, 11):
36+
import asyncio as async_timeout
37+
else:
38+
import async_timeout
39+
3540
__all__ = (
3641
"WebSocketResponse",
3742
"WebSocketReady",

requirements/base.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ aiodns==3.0.0 ; sys_platform == "linux" or sys_platform == "darwin"
88
# via -r requirements/runtime-deps.in
99
aiosignal==1.3.1
1010
# via -r requirements/runtime-deps.in
11-
async-timeout==4.0.3
11+
async-timeout==4.0.3 ; python_version < "3.11"
1212
# via -r requirements/runtime-deps.in
1313
brotli==1.0.9
1414
# via -r requirements/runtime-deps.in

requirements/constraints.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ aiosignal==1.3.1
1414
# via -r requirements/runtime-deps.in
1515
alabaster==0.7.13
1616
# via sphinx
17-
async-timeout==4.0.3
17+
async-timeout==4.0.3 ; python_version < "3.11"
1818
# via
1919
# -r requirements/runtime-deps.in
2020
# aioredis

requirements/dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ aiosignal==1.3.1
1414
# via -r requirements/runtime-deps.in
1515
alabaster==0.7.13
1616
# via sphinx
17-
async-timeout==4.0.3
17+
async-timeout==4.0.3 ; python_version < "3.11"
1818
# via
1919
# -r requirements/runtime-deps.in
2020
# aioredis

requirements/lint.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77
aioredis==2.0.1
88
# via -r requirements/lint.in
9-
async-timeout==4.0.3
9+
async-timeout==4.0.3 ; python_version < "3.11"
1010
# via aioredis
1111
cfgv==3.3.1
1212
# via pre-commit

requirements/runtime-deps.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
charset-normalizer >=2.0, < 4.0
44
multidict >=4.5, < 7.0
5-
async-timeout >= 4.0, < 5.0
5+
async-timeout >= 4.0, < 5.0 ; python_version < "3.11"
66
yarl >= 1.0, < 2.0
77
frozenlist >= 1.1.1
88
aiosignal >= 1.1.2

requirements/runtime-deps.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ aiodns==3.0.0 ; sys_platform == "linux" or sys_platform == "darwin"
88
# via -r requirements/runtime-deps.in
99
aiosignal==1.3.1
1010
# via -r requirements/runtime-deps.in
11-
async-timeout==4.0.3
11+
async-timeout==4.0.3 ; python_version < "3.11"
1212
# via -r requirements/runtime-deps.in
1313
brotli==1.0.9
1414
# via -r requirements/runtime-deps.in

requirements/test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ aiodns==3.0.0 ; sys_platform == "linux" or sys_platform == "darwin"
88
# via -r requirements/runtime-deps.in
99
aiosignal==1.3.1
1010
# via -r requirements/runtime-deps.in
11-
async-timeout==4.0.3
11+
async-timeout==4.0.3 ; python_version < "3.11"
1212
# via -r requirements/runtime-deps.in
1313
brotli==1.0.9
1414
# via -r requirements/runtime-deps.in

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ include_package_data = True
4949
install_requires =
5050
charset-normalizer >=2.0, < 4.0
5151
multidict >=4.5, < 7.0
52-
async-timeout >= 4.0, < 5.0
52+
async-timeout >= 4.0, < 5.0 ; python_version < "3.11"
5353
yarl >= 1.0, < 2.0
5454
frozenlist >= 1.1.1
5555
aiosignal >= 1.1.2

tests/test_client_ws_functional.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
# type: ignore
22
import asyncio
3+
import sys
34
from typing import Any
45

5-
import async_timeout
66
import pytest
77

88
import aiohttp
99
from aiohttp import hdrs, web
1010
from aiohttp.client_ws import ClientWSTimeout
1111

12+
if sys.version_info >= (3, 11):
13+
import asyncio as async_timeout
14+
else:
15+
import async_timeout
16+
1217

1318
async def test_send_recv_text(aiohttp_client: Any) -> None:
1419
async def handler(request):

tests/test_helpers.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import datetime
55
import gc
66
import platform
7+
import sys
78
import weakref
89
from math import ceil, modf
910
from pathlib import Path
@@ -448,25 +449,37 @@ def test_ceil_call_later_no_timeout() -> None:
448449

449450
async def test_ceil_timeout_none(loop) -> None:
450451
async with helpers.ceil_timeout(None) as cm:
451-
assert cm.deadline is None
452+
if sys.version_info >= (3, 11):
453+
assert cm.when() is None
454+
else:
455+
assert cm.deadline is None
452456

453457

454458
async def test_ceil_timeout_round(loop) -> None:
455459
async with helpers.ceil_timeout(7.5) as cm:
456-
frac, integer = modf(cm.deadline)
460+
if sys.version_info >= (3, 11):
461+
frac, integer = modf(cm.when())
462+
else:
463+
frac, integer = modf(cm.deadline)
457464
assert frac == 0
458465

459466

460467
async def test_ceil_timeout_small(loop) -> None:
461468
async with helpers.ceil_timeout(1.1) as cm:
462-
frac, integer = modf(cm.deadline)
469+
if sys.version_info >= (3, 11):
470+
frac, integer = modf(cm.when())
471+
else:
472+
frac, integer = modf(cm.deadline)
463473
# a chance for exact integer with zero fraction is negligible
464474
assert frac != 0
465475

466476

467477
async def test_ceil_timeout_small_with_overriden_threshold(loop) -> None:
468478
async with helpers.ceil_timeout(1.5, ceil_threshold=1) as cm:
469-
frac, integer = modf(cm.deadline)
479+
if sys.version_info >= (3, 11):
480+
frac, integer = modf(cm.when())
481+
else:
482+
frac, integer = modf(cm.deadline)
470483
assert frac == 0
471484

472485

0 commit comments

Comments
 (0)