Update clientsession socket family typing (#122464)

pull/122409/head^2
Marc Mueller 2024-07-23 16:04:09 +02:00 committed by GitHub
parent f260d63c58
commit da6a7ebd42
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 7 deletions

View File

@ -83,7 +83,9 @@ class HassClientResponse(aiohttp.ClientResponse):
@callback
@bind_hass
def async_get_clientsession(
hass: HomeAssistant, verify_ssl: bool = True, family: int = 0
hass: HomeAssistant,
verify_ssl: bool = True,
family: socket.AddressFamily = socket.AF_UNSPEC,
) -> aiohttp.ClientSession:
"""Return default aiohttp ClientSession.
@ -112,7 +114,7 @@ def async_create_clientsession(
hass: HomeAssistant,
verify_ssl: bool = True,
auto_cleanup: bool = True,
family: int = 0,
family: socket.AddressFamily = socket.AF_UNSPEC,
**kwargs: Any,
) -> aiohttp.ClientSession:
"""Create a new ClientSession with kwargs, i.e. for cookies.
@ -143,7 +145,7 @@ def _async_create_clientsession(
verify_ssl: bool = True,
auto_cleanup_method: Callable[[HomeAssistant, aiohttp.ClientSession], None]
| None = None,
family: int = 0,
family: socket.AddressFamily = socket.AF_UNSPEC,
**kwargs: Any,
) -> aiohttp.ClientSession:
"""Create a new ClientSession with kwargs, i.e. for cookies."""
@ -276,14 +278,18 @@ def _async_register_default_clientsession_shutdown(
@callback
def _make_key(verify_ssl: bool = True, family: int = 0) -> tuple[bool, int]:
def _make_key(
verify_ssl: bool = True, family: socket.AddressFamily = socket.AF_UNSPEC
) -> tuple[bool, socket.AddressFamily]:
"""Make a key for connector or session pool."""
return (verify_ssl, family)
@callback
def _async_get_connector(
hass: HomeAssistant, verify_ssl: bool = True, family: int = 0
hass: HomeAssistant,
verify_ssl: bool = True,
family: socket.AddressFamily = socket.AF_UNSPEC,
) -> aiohttp.BaseConnector:
"""Return the connector pool for aiohttp.
@ -301,7 +307,7 @@ def _async_get_connector(
ssl_context = ssl_util.get_default_no_verify_context()
connector = aiohttp.TCPConnector(
family=socket.AddressFamily(family),
family=family,
enable_cleanup_closed=ENABLE_CLEANUP_CLOSED,
ssl=ssl_context,
limit=MAXIMUM_CONNECTIONS,

View File

@ -1,5 +1,6 @@
"""Test the aiohttp client helper."""
import socket
from unittest.mock import Mock, patch
import aiohttp
@ -83,7 +84,14 @@ async def test_get_clientsession_without_ssl(hass: HomeAssistant) -> None:
@pytest.mark.parametrize(
("verify_ssl", "expected_family"),
[(True, 0), (False, 0), (True, 4), (False, 4), (True, 6), (False, 6)],
[
(True, socket.AF_UNSPEC),
(False, socket.AF_UNSPEC),
(True, socket.AF_INET),
(False, socket.AF_INET),
(True, socket.AF_INET6),
(False, socket.AF_INET6),
],
)
async def test_get_clientsession(
hass: HomeAssistant, verify_ssl: bool, expected_family: int