Skip to content

Commit

Permalink
[PR #5278/0f0c3759 backport][3.10] Drop Python 3.6 support (#8512)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Svetlov <[email protected]>
  • Loading branch information
bdraco and asvetlov authored Jul 17, 2024
1 parent c12a143 commit 266559c
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 64 deletions.
3 changes: 1 addition & 2 deletions aiohttp/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from multidict import CIMultiDict
from yarl import URL

from .helpers import get_running_loop
from .typedefs import LooseCookies

if TYPE_CHECKING:
Expand Down Expand Up @@ -170,7 +169,7 @@ class AbstractCookieJar(Sized, IterableBase):
"""Abstract Cookie Jar."""

def __init__(self, *, loop: Optional[asyncio.AbstractEventLoop] = None) -> None:
self._loop = get_running_loop(loop)
self._loop = loop or asyncio.get_running_loop()

@abstractmethod
def clear(self, predicate: Optional[ClearCookiePredicate] = None) -> None:
Expand Down
3 changes: 1 addition & 2 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
TimeoutHandle,
ceil_timeout,
get_env_proxy_for_url,
get_running_loop,
method_must_be_empty_body,
sentinel,
strip_auth_from_url,
Expand Down Expand Up @@ -294,7 +293,7 @@ def __init__(
if connector is not None:
loop = connector._loop

loop = get_running_loop(loop)
loop = loop or asyncio.get_running_loop()

if base_url is None or isinstance(base_url, URL):
self._base_url: Optional[URL] = base_url
Expand Down
4 changes: 2 additions & 2 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
)
from .client_proto import ResponseHandler
from .client_reqrep import ClientRequest, Fingerprint, _merge_ssl_params
from .helpers import ceil_timeout, get_running_loop, is_ip_address, noop, sentinel
from .helpers import ceil_timeout, is_ip_address, noop, sentinel
from .locks import EventResultOrError
from .resolver import DefaultResolver

Expand Down Expand Up @@ -231,7 +231,7 @@ def __init__(
if keepalive_timeout is sentinel:
keepalive_timeout = 15.0

loop = get_running_loop(loop)
loop = loop or asyncio.get_running_loop()
self._timeout_ceil_threshold = timeout_ceil_threshold

self._closed = False
Expand Down
39 changes: 3 additions & 36 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import re
import sys
import time
import warnings
import weakref
from collections import namedtuple
from contextlib import suppress
Expand Down Expand Up @@ -52,7 +51,7 @@
from yarl import URL

from . import hdrs
from .log import client_logger, internal_logger
from .log import client_logger

if sys.version_info >= (3, 11):
import asyncio as async_timeout
Expand Down Expand Up @@ -287,38 +286,6 @@ def proxies_from_env() -> Dict[str, ProxyInfo]:
return ret


def current_task(
loop: Optional[asyncio.AbstractEventLoop] = None,
) -> "Optional[asyncio.Task[Any]]":
return asyncio.current_task(loop=loop)


def get_running_loop(
loop: Optional[asyncio.AbstractEventLoop] = None,
) -> asyncio.AbstractEventLoop:
if loop is None:
loop = asyncio.get_event_loop()
if not loop.is_running():
warnings.warn(
"The object should be created within an async function",
DeprecationWarning,
stacklevel=3,
)
if loop.get_debug():
internal_logger.warning(
"The object should be created within an async function", stack_info=True
)
return loop


def isasyncgenfunction(obj: Any) -> bool:
func = getattr(inspect, "isasyncgenfunction", None)
if func is not None:
return func(obj) # type: ignore[no-any-return]
else:
return False


def get_env_proxy_for_url(url: URL) -> Tuple[URL, Optional[BasicAuth]]:
"""Get a permitted proxy for the given URL from the env."""
if url.host is not None and proxy_bypass(url.host):
Expand Down Expand Up @@ -709,7 +676,7 @@ def assert_timeout(self) -> None:
raise asyncio.TimeoutError from None

def __enter__(self) -> BaseTimerContext:
task = current_task(loop=self._loop)
task = asyncio.current_task(loop=self._loop)

if task is None:
raise RuntimeError(
Expand Down Expand Up @@ -749,7 +716,7 @@ def ceil_timeout(
if delay is None or delay <= 0:
return async_timeout.timeout(None)

loop = get_running_loop()
loop = asyncio.get_running_loop()
now = loop.time()
when = now + delay
if delay > ceil_threshold:
Expand Down
4 changes: 2 additions & 2 deletions aiohttp/pytest_plugin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import asyncio
import contextlib
import inspect
import warnings
from typing import Any, Awaitable, Callable, Dict, Iterator, Optional, Type, Union

import pytest

from aiohttp.helpers import isasyncgenfunction
from aiohttp.web import Application

from .test_utils import (
Expand Down Expand Up @@ -57,7 +57,7 @@ def pytest_fixture_setup(fixturedef): # type: ignore[no-untyped-def]
"""
func = fixturedef.func

if isasyncgenfunction(func):
if inspect.isasyncgenfunction(func):
# async generator fixture
is_async_gen = True
elif asyncio.iscoroutinefunction(func):
Expand Down
3 changes: 1 addition & 2 deletions aiohttp/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import Any, Dict, List, Optional, Tuple, Type, Union

from .abc import AbstractResolver, ResolveResult
from .helpers import get_running_loop

__all__ = ("ThreadedResolver", "AsyncResolver", "DefaultResolver")

Expand All @@ -30,7 +29,7 @@ class ThreadedResolver(AbstractResolver):
"""

def __init__(self, loop: Optional[asyncio.AbstractEventLoop] = None) -> None:
self._loop = get_running_loop(loop)
self._loop = loop or asyncio.get_running_loop()

async def resolve(
self, host: str, port: int = 0, family: socket.AddressFamily = socket.AF_INET
Expand Down
3 changes: 1 addition & 2 deletions aiohttp/web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import Any, Awaitable, Callable, Dict, List, Optional # noqa

from .abc import AbstractStreamWriter
from .helpers import get_running_loop
from .http_parser import RawRequestMessage
from .streams import StreamReader
from .web_protocol import RequestHandler, _RequestFactory, _RequestHandler
Expand All @@ -23,7 +22,7 @@ def __init__(
loop: Optional[asyncio.AbstractEventLoop] = None,
**kwargs: Any
) -> None:
self._loop = get_running_loop(loop)
self._loop = loop or asyncio.get_running_loop()
self._connections: Dict[RequestHandler, asyncio.Transport] = {}
self._kwargs = kwargs
self.requests_count = 0
Expand Down
4 changes: 1 addition & 3 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,16 +579,14 @@ async def handler(request):


async def test_format_task_get(aiohttp_server) -> None:
loop = asyncio.get_event_loop()

async def handler(request):
return web.Response(body=b"OK")

app = web.Application()
app.router.add_route("GET", "/", handler)
server = await aiohttp_server(app)
client = aiohttp.ClientSession()
task = loop.create_task(client.get(server.make_url("/")))
task = asyncio.create_task(client.get(server.make_url("/")))
assert f"{task}".startswith("<Task pending")
resp = await task
resp.close()
Expand Down
3 changes: 2 additions & 1 deletion tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2295,7 +2295,8 @@ async def handler(request):
session = aiohttp.ClientSession(connector=conn)
url = srv.make_url("/")

with pytest.raises(aiohttp.ClientConnectorCertificateError) as ctx:
err = aiohttp.ClientConnectorCertificateError
with pytest.raises(err) as ctx:
await session.get(url)

assert isinstance(ctx.value, aiohttp.ClientConnectorCertificateError)
Expand Down
12 changes: 0 additions & 12 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,18 +607,6 @@ def test_proxies_from_env_http_with_auth(url_input, expected_scheme) -> None:
assert proxy_auth.encoding == "latin1"


# ------------ get_running_loop ---------------------------------


def test_get_running_loop_not_running(loop) -> None:
with pytest.warns(DeprecationWarning):
helpers.get_running_loop()


async def test_get_running_loop_ok(loop) -> None:
assert helpers.get_running_loop() is loop


# --------------------- get_env_proxy_for_url ------------------------------


Expand Down

0 comments on commit 266559c

Please sign in to comment.