Update clientsession socket family typing (#122464)

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

View File

@ -1,5 +1,6 @@
"""Test the aiohttp client helper.""" """Test the aiohttp client helper."""
import socket
from unittest.mock import Mock, patch from unittest.mock import Mock, patch
import aiohttp import aiohttp
@ -83,7 +84,14 @@ async def test_get_clientsession_without_ssl(hass: HomeAssistant) -> None:
@pytest.mark.parametrize( @pytest.mark.parametrize(
("verify_ssl", "expected_family"), ("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( async def test_get_clientsession(
hass: HomeAssistant, verify_ssl: bool, expected_family: int hass: HomeAssistant, verify_ssl: bool, expected_family: int