Skip to content

Commit

Permalink
Update clientsession socket family typing (#122464)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Jul 23, 2024
1 parent f260d63 commit da6a7eb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
18 changes: 12 additions & 6 deletions homeassistant/helpers/aiohttp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ async def json(
@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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -276,14 +278,18 @@ def _async_close_websession(event: Event) -> None:


@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.
Expand All @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion tests/helpers/test_aiohttp_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test the aiohttp client helper."""

import socket
from unittest.mock import Mock, patch

import aiohttp
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit da6a7eb

Please sign in to comment.