Skip to content

Commit

Permalink
Only install async-timeout for Python < 3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Aug 25, 2023
1 parent cf97e5b commit 7fcdcf0
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 18 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
7 changes: 6 additions & 1 deletion aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,22 @@
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)
PY_311 = sys.version_info >= (3, 11)

COOKIE_MAX_LENGTH = 4096

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
22 changes: 17 additions & 5 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from yarl import URL

from aiohttp import helpers
from aiohttp.helpers import is_expected_content_type, parse_http_date
from aiohttp.helpers import PY_311, is_expected_content_type, parse_http_date

IS_PYPY = platform.python_implementation() == "PyPy"

Expand Down Expand Up @@ -448,25 +448,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 PY_311:
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 PY_311:
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 PY_311:
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 PY_311:
frac, integer = modf(cm.when())
else:
frac, integer = modf(cm.deadline)
assert frac == 0


Expand Down

0 comments on commit 7fcdcf0

Please sign in to comment.