Bump black from 23.12.1 to 24.1.1 (#812)

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
dependabot[bot] 2024-02-04 21:39:47 -06:00 committed by GitHub
parent 40ac989a10
commit d63f10d6e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 174 additions and 115 deletions

View File

@ -23,7 +23,7 @@ repos:
args: args:
- --fix - --fix
- repo: https://github.com/psf/black-pre-commit-mirror - repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.11.0 rev: 24.1.1
hooks: hooks:
- id: black - id: black
args: args:

View File

@ -776,8 +776,9 @@ class APIClient:
async def _bluetooth_gatt_read( async def _bluetooth_gatt_read(
self, self,
req_type: type[BluetoothGATTReadDescriptorRequest] req_type: (
| type[BluetoothGATTReadRequest], type[BluetoothGATTReadDescriptorRequest] | type[BluetoothGATTReadRequest]
),
address: int, address: int,
handle: int, handle: int,
timeout: float, timeout: float,

View File

@ -114,11 +114,13 @@ def on_bluetooth_device_connection_response(
def on_bluetooth_handle_message( def on_bluetooth_handle_message(
address: int, address: int,
handle: int, handle: int,
msg: BluetoothGATTErrorResponse msg: (
| BluetoothGATTNotifyResponse BluetoothGATTErrorResponse
| BluetoothGATTReadResponse | BluetoothGATTNotifyResponse
| BluetoothGATTWriteResponse | BluetoothGATTReadResponse
| BluetoothDeviceConnectionResponse, | BluetoothGATTWriteResponse
| BluetoothDeviceConnectionResponse
),
) -> bool: ) -> bool:
"""Filter a Bluetooth message for an address and handle.""" """Filter a Bluetooth message for an address and handle."""
if type(msg) is BluetoothDeviceConnectionResponse: if type(msg) is BluetoothDeviceConnectionResponse:
@ -129,14 +131,16 @@ def on_bluetooth_handle_message(
def on_bluetooth_message_types( def on_bluetooth_message_types(
address: int, address: int,
msg_types: tuple[type[message.Message]], msg_types: tuple[type[message.Message]],
msg: BluetoothGATTErrorResponse msg: (
| BluetoothGATTNotifyResponse BluetoothGATTErrorResponse
| BluetoothGATTReadResponse | BluetoothGATTNotifyResponse
| BluetoothGATTWriteResponse | BluetoothGATTReadResponse
| BluetoothDeviceConnectionResponse | BluetoothGATTWriteResponse
| BluetoothGATTGetServicesResponse | BluetoothDeviceConnectionResponse
| BluetoothGATTGetServicesDoneResponse | BluetoothGATTGetServicesResponse
| BluetoothGATTErrorResponse, | BluetoothGATTGetServicesDoneResponse
| BluetoothGATTErrorResponse
),
) -> bool: ) -> bool:
"""Filter Bluetooth messages of a specific type and address.""" """Filter Bluetooth messages of a specific type and address."""
return type(msg) in msg_types and bool(msg.address == address) return type(msg) in msg_types and bool(msg.address == address)

View File

@ -221,9 +221,9 @@ class APIConnection:
self._params = params self._params = params
self.on_stop: Callable[[bool], None] | None = on_stop self.on_stop: Callable[[bool], None] | None = on_stop
self._socket: socket.socket | None = None self._socket: socket.socket | None = None
self._frame_helper: None | ( self._frame_helper: None | (APINoiseFrameHelper | APIPlaintextFrameHelper) = (
APINoiseFrameHelper | APIPlaintextFrameHelper None
) = None )
self.api_version: APIVersion | None = None self.api_version: APIVersion | None = None
self.connection_state = CONNECTION_STATE_INITIALIZED self.connection_state = CONNECTION_STATE_INITIALIZED

View File

@ -1,5 +1,5 @@
pylint==3.0.3 pylint==3.0.3
black==23.12.1 black==24.1.1
flake8==7.0.0 flake8==7.0.0
isort==5.13.2 isort==5.13.2
mypy==1.8.0 mypy==1.8.0

View File

@ -1,4 +1,5 @@
"""Init tests.""" """Init tests."""
from __future__ import annotations from __future__ import annotations
import logging import logging

View File

@ -1,4 +1,5 @@
"""Test fixtures.""" """Test fixtures."""
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
@ -217,11 +218,14 @@ async def api_client(
password=None, password=None,
) )
with patch.object( with (
event_loop, patch.object(
"create_connection", event_loop,
side_effect=partial(_create_mock_transport_protocol, transport, connected), "create_connection",
), patch("aioesphomeapi.client.APIConnection", PatchableAPIConnection): side_effect=partial(_create_mock_transport_protocol, transport, connected),
),
patch("aioesphomeapi.client.APIConnection", PatchableAPIConnection),
):
connect_task = asyncio.create_task(connect_client(client, login=False)) connect_task = asyncio.create_task(connect_client(client, login=False))
await connected.wait() await connected.wait()
conn = client._connection conn = client._connection

View File

@ -186,9 +186,10 @@ async def test_connect_backwards_compat() -> None:
pass pass
cli = PatchableApiClient("host", 1234, None) cli = PatchableApiClient("host", 1234, None)
with patch.object(cli, "start_connection") as mock_start_connection, patch.object( with (
cli, "finish_connection" patch.object(cli, "start_connection") as mock_start_connection,
) as mock_finish_connection: patch.object(cli, "finish_connection") as mock_finish_connection,
):
await cli.connect() await cli.connect()
assert mock_start_connection.mock_calls == [call(None)] assert mock_start_connection.mock_calls == [call(None)]
@ -244,9 +245,12 @@ async def test_connection_released_if_connecting_is_cancelled() -> None:
mock_socket.getpeername.return_value = ("4.3.3.3", 323) mock_socket.getpeername.return_value = ("4.3.3.3", 323)
return mock_socket return mock_socket
with patch("aioesphomeapi.client.APIConnection", PatchableAPIConnection), patch( with (
"aioesphomeapi.connection.aiohappyeyeballs.start_connection", patch("aioesphomeapi.client.APIConnection", PatchableAPIConnection),
_start_connection_without_delay, patch(
"aioesphomeapi.connection.aiohappyeyeballs.start_connection",
_start_connection_without_delay,
),
): ):
await cli.start_connection() await cli.start_connection()
await asyncio.sleep(0) await asyncio.sleep(0)
@ -268,10 +272,13 @@ async def test_request_while_handshaking() -> None:
pass pass
cli = PatchableApiClient("host", 1234, None) cli = PatchableApiClient("host", 1234, None)
with patch( with (
"aioesphomeapi.connection.aiohappyeyeballs.start_connection", patch(
side_effect=partial(asyncio.sleep, 1), "aioesphomeapi.connection.aiohappyeyeballs.start_connection",
), patch.object(cli, "finish_connection"): side_effect=partial(asyncio.sleep, 1),
),
patch.object(cli, "finish_connection"),
):
connect_task = asyncio.create_task(cli.connect()) connect_task = asyncio.create_task(cli.connect())
await asyncio.sleep(0) await asyncio.sleep(0)
@ -1500,11 +1507,14 @@ async def test_bluetooth_gatt_start_notify_fails(
handlers_before = len(list(itertools.chain(*connection._message_handlers.values()))) handlers_before = len(list(itertools.chain(*connection._message_handlers.values())))
with patch.object( with (
connection, patch.object(
"send_messages_await_response_complex", connection,
side_effect=APIConnectionError, "send_messages_await_response_complex",
), pytest.raises(APIConnectionError): side_effect=APIConnectionError,
),
pytest.raises(APIConnectionError),
):
await client.bluetooth_gatt_start_notify(1234, 1, on_bluetooth_gatt_notify) await client.bluetooth_gatt_start_notify(1234, 1, on_bluetooth_gatt_notify)
assert ( assert (

View File

@ -135,8 +135,9 @@ async def test_disconnect_when_not_fully_connected(
await asyncio.sleep(0) await asyncio.sleep(0)
transport.reset_mock() transport.reset_mock()
with patch("aioesphomeapi.connection.DISCONNECT_CONNECT_TIMEOUT", 0.0), patch( with (
"aioesphomeapi.connection.DISCONNECT_RESPONSE_TIMEOUT", 0.0 patch("aioesphomeapi.connection.DISCONNECT_CONNECT_TIMEOUT", 0.0),
patch("aioesphomeapi.connection.DISCONNECT_RESPONSE_TIMEOUT", 0.0),
): ):
await conn.disconnect() await conn.disconnect()
@ -344,10 +345,13 @@ async def test_start_connection_times_out(
async def _mock_socket_connect(*args, **kwargs): async def _mock_socket_connect(*args, **kwargs):
await asyncio.sleep(500) await asyncio.sleep(500)
with patch( with (
"aioesphomeapi.connection.aiohappyeyeballs.start_connection", patch(
side_effect=_mock_socket_connect, "aioesphomeapi.connection.aiohappyeyeballs.start_connection",
), patch("aioesphomeapi.connection.TCP_CONNECT_TIMEOUT", 0.0): side_effect=_mock_socket_connect,
),
patch("aioesphomeapi.connection.TCP_CONNECT_TIMEOUT", 0.0),
):
connect_task = asyncio.create_task(connect(conn, login=False)) connect_task = asyncio.create_task(connect(conn, login=False))
await asyncio.sleep(0) await asyncio.sleep(0)
@ -497,14 +501,17 @@ async def test_plaintext_connection_fails_handshake(
remove = conn.add_message_callback(on_msg, (HelloResponse, DeviceInfoResponse)) remove = conn.add_message_callback(on_msg, (HelloResponse, DeviceInfoResponse))
transport = MagicMock() transport = MagicMock()
with patch( with (
"aioesphomeapi.connection.APIPlaintextFrameHelper", patch(
APIPlaintextFrameHelperHandshakeException, "aioesphomeapi.connection.APIPlaintextFrameHelper",
), patch.object( APIPlaintextFrameHelperHandshakeException,
loop, ),
"create_connection", patch.object(
side_effect=partial( loop,
_create_failing_mock_transport_protocol, transport, connected "create_connection",
side_effect=partial(
_create_failing_mock_transport_protocol, transport, connected
),
), ),
): ):
connect_task = asyncio.create_task(connect(conn, login=False)) connect_task = asyncio.create_task(connect(conn, login=False))
@ -537,12 +544,10 @@ async def test_plaintext_connection_fails_handshake(
def _frame_helper_close_call(): def _frame_helper_close_call():
call_order.append("frame_helper_close") call_order.append("frame_helper_close")
with patch.object( with (
conn._socket, "close", side_effect=_socket_close_call patch.object(conn._socket, "close", side_effect=_socket_close_call),
), patch.object( patch.object(conn._frame_helper, "close", side_effect=_frame_helper_close_call),
conn._frame_helper, "close", side_effect=_frame_helper_close_call pytest.raises(raised_exception),
), pytest.raises(
raised_exception
): ):
await asyncio.sleep(0) await asyncio.sleep(0)
await connect_task await connect_task
@ -658,16 +663,20 @@ async def test_connect_resolver_times_out(
connected = asyncio.Event() connected = asyncio.Event()
event_loop = asyncio.get_running_loop() event_loop = asyncio.get_running_loop()
with patch( with (
"aioesphomeapi.host_resolver.async_resolve_host", patch(
side_effect=asyncio.TimeoutError, "aioesphomeapi.host_resolver.async_resolve_host",
), patch.object( side_effect=asyncio.TimeoutError,
event_loop, ),
"create_connection", patch.object(
side_effect=partial(_create_mock_transport_protocol, transport, connected), event_loop,
), pytest.raises( "create_connection",
ResolveAPIError, side_effect=partial(_create_mock_transport_protocol, transport, connected),
match="Timeout while resolving IP address for fake.address", ),
pytest.raises(
ResolveAPIError,
match="Timeout while resolving IP address for fake.address",
),
): ):
await connect(conn, login=False) await connect(conn, login=False)

View File

@ -45,9 +45,10 @@ async def test_resolve_host_zeroconf(async_zeroconf: AsyncZeroconf, addr_infos):
[ipv6], [ipv6],
] ]
info.async_request = AsyncMock(return_value=True) info.async_request = AsyncMock(return_value=True)
with patch( with (
"aioesphomeapi.host_resolver.AsyncServiceInfo", return_value=info patch("aioesphomeapi.host_resolver.AsyncServiceInfo", return_value=info),
), patch("aioesphomeapi.zeroconf.AsyncZeroconf", return_value=async_zeroconf): patch("aioesphomeapi.zeroconf.AsyncZeroconf", return_value=async_zeroconf),
):
ret = await hr._async_resolve_host_zeroconf("asdf", 6052) ret = await hr._async_resolve_host_zeroconf("asdf", 6052)
info.async_request.assert_called_once() info.async_request.assert_called_once()
@ -86,20 +87,26 @@ async def test_resolve_host_zeroconf_empty(async_zeroconf: AsyncZeroconf):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_resolve_host_zeroconf_fails(async_zeroconf: AsyncZeroconf): async def test_resolve_host_zeroconf_fails(async_zeroconf: AsyncZeroconf):
with patch( with (
"aioesphomeapi.host_resolver.AsyncServiceInfo.async_request", patch(
side_effect=Exception("no buffers"), "aioesphomeapi.host_resolver.AsyncServiceInfo.async_request",
), pytest.raises(ResolveAPIError, match="no buffers"): side_effect=Exception("no buffers"),
),
pytest.raises(ResolveAPIError, match="no buffers"),
):
await hr._async_resolve_host_zeroconf("asdf.local", 6052) await hr._async_resolve_host_zeroconf("asdf.local", 6052)
@pytest.mark.asyncio @pytest.mark.asyncio
@patch("aioesphomeapi.host_resolver._async_resolve_host_getaddrinfo", return_value=[]) @patch("aioesphomeapi.host_resolver._async_resolve_host_getaddrinfo", return_value=[])
async def test_resolve_host_zeroconf_fails_end_to_end(async_zeroconf: AsyncZeroconf): async def test_resolve_host_zeroconf_fails_end_to_end(async_zeroconf: AsyncZeroconf):
with patch( with (
"aioesphomeapi.host_resolver.AsyncServiceInfo.async_request", patch(
side_effect=Exception("no buffers"), "aioesphomeapi.host_resolver.AsyncServiceInfo.async_request",
), pytest.raises(ResolveAPIError, match="no buffers"): side_effect=Exception("no buffers"),
),
pytest.raises(ResolveAPIError, match="no buffers"),
):
await hr.async_resolve_host(["asdf.local"], 6052) await hr.async_resolve_host(["asdf.local"], 6052)
@ -226,13 +233,13 @@ async def test_resolve_host_zeroconf_service_info_oserror(
ip_address(b" \x01\r\xb8\x85\xa3\x00\x00\x00\x00\x8a.\x03ps4"), ip_address(b" \x01\r\xb8\x85\xa3\x00\x00\x00\x00\x8a.\x03ps4"),
] ]
info.async_request = AsyncMock(return_value=True) info.async_request = AsyncMock(return_value=True)
with patch( with (
"aioesphomeapi.host_resolver.AsyncServiceInfo.async_request", patch(
side_effect=OSError("out of buffers"), "aioesphomeapi.host_resolver.AsyncServiceInfo.async_request",
), patch( side_effect=OSError("out of buffers"),
"aioesphomeapi.zeroconf.AsyncZeroconf", return_value=async_zeroconf ),
), pytest.raises( patch("aioesphomeapi.zeroconf.AsyncZeroconf", return_value=async_zeroconf),
ResolveAPIError, match="out of buffers" pytest.raises(ResolveAPIError, match="out of buffers"),
): ):
await hr._async_resolve_host_zeroconf("asdf", 6052) await hr._async_resolve_host_zeroconf("asdf", 6052)
@ -247,9 +254,13 @@ async def test_resolve_host_create_zeroconf_oserror(
ip_address(b" \x01\r\xb8\x85\xa3\x00\x00\x00\x00\x8a.\x03ps4"), ip_address(b" \x01\r\xb8\x85\xa3\x00\x00\x00\x00\x8a.\x03ps4"),
] ]
info.async_request = AsyncMock(return_value=True) info.async_request = AsyncMock(return_value=True)
with patch( with (
"aioesphomeapi.zeroconf.AsyncZeroconf", side_effect=OSError("out of buffers") patch(
), pytest.raises(ResolveAPIError, match="out of buffers"): "aioesphomeapi.zeroconf.AsyncZeroconf",
side_effect=OSError("out of buffers"),
),
pytest.raises(ResolveAPIError, match="out of buffers"),
):
await hr._async_resolve_host_zeroconf("asdf", 6052) await hr._async_resolve_host_zeroconf("asdf", 6052)

View File

@ -72,9 +72,12 @@ async def test_log_runner(
await original_subscribe_logs(*args, **kwargs) await original_subscribe_logs(*args, **kwargs)
subscribed.set() subscribed.set()
with patch.object( with (
loop, "create_connection", side_effect=_create_mock_transport_protocol patch.object(
), patch.object(cli, "subscribe_logs", _wait_subscribe_cli): loop, "create_connection", side_effect=_create_mock_transport_protocol
),
patch.object(cli, "subscribe_logs", _wait_subscribe_cli),
):
stop = await async_run(cli, on_log, aio_zeroconf_instance=async_zeroconf) stop = await async_run(cli, on_log, aio_zeroconf_instance=async_zeroconf)
await connected.wait() await connected.wait()
protocol = cli._connection._frame_helper protocol = cli._connection._frame_helper
@ -138,9 +141,12 @@ async def test_log_runner_reconnects_on_disconnect(
await original_subscribe_logs(*args, **kwargs) await original_subscribe_logs(*args, **kwargs)
subscribed.set() subscribed.set()
with patch.object( with (
loop, "create_connection", side_effect=_create_mock_transport_protocol patch.object(
), patch.object(cli, "subscribe_logs", _wait_subscribe_cli): loop, "create_connection", side_effect=_create_mock_transport_protocol
),
patch.object(cli, "subscribe_logs", _wait_subscribe_cli),
):
stop = await async_run(cli, on_log, aio_zeroconf_instance=async_zeroconf) stop = await async_run(cli, on_log, aio_zeroconf_instance=async_zeroconf)
await connected.wait() await connected.wait()
protocol = cli._connection._frame_helper protocol = cli._connection._frame_helper
@ -214,9 +220,10 @@ async def test_log_runner_reconnects_on_subscribe_failure(
subscribed.set() subscribed.set()
raise APIConnectionError("subscribed force to fail") raise APIConnectionError("subscribed force to fail")
with patch.object( with (
cli, "disconnect", partial(cli.disconnect, force=True) patch.object(cli, "disconnect", partial(cli.disconnect, force=True)),
), patch.object(cli, "subscribe_logs", _wait_and_fail_subscribe_cli): patch.object(cli, "subscribe_logs", _wait_and_fail_subscribe_cli),
):
with patch.object( with patch.object(
loop, "create_connection", side_effect=_create_mock_transport_protocol loop, "create_connection", side_effect=_create_mock_transport_protocol
): ):
@ -230,9 +237,12 @@ async def test_log_runner_reconnects_on_subscribe_failure(
assert cli._connection is None assert cli._connection is None
with patch.object( with (
loop, "create_connection", side_effect=_create_mock_transport_protocol patch.object(
), patch.object(cli, "subscribe_logs"): loop, "create_connection", side_effect=_create_mock_transport_protocol
),
patch.object(cli, "subscribe_logs"),
):
connected.clear() connected.clear()
await asyncio.sleep(0) await asyncio.sleep(0)
async_fire_time_changed( async_fire_time_changed(

View File

@ -215,8 +215,9 @@ async def test_reconnect_logic_state(patchable_api_client: APIClient):
assert rl._connection_state is ReconnectLogicState.DISCONNECTED assert rl._connection_state is ReconnectLogicState.DISCONNECTED
assert rl._tries == 1 assert rl._tries == 1
with patch.object(cli, "start_connection"), patch.object( with (
cli, "finish_connection", side_effect=RequiresEncryptionAPIError patch.object(cli, "start_connection"),
patch.object(cli, "finish_connection", side_effect=RequiresEncryptionAPIError),
): ):
await rl.start() await rl.start()
await asyncio.sleep(0) await asyncio.sleep(0)
@ -428,8 +429,9 @@ async def test_reconnect_zeroconf(
assert not rl._is_stopped assert not rl._is_stopped
caplog.clear() caplog.clear()
with patch.object(cli, "start_connection") as mock_start_connection, patch.object( with (
cli, "finish_connection" patch.object(cli, "start_connection") as mock_start_connection,
patch.object(cli, "finish_connection"),
): ):
assert rl._zc_listening is True assert rl._zc_listening is True
rl.async_update_records( rl.async_update_records(
@ -482,9 +484,12 @@ async def test_reconnect_zeroconf_not_while_handshaking(
assert mock_start_connection.call_count == 1 assert mock_start_connection.call_count == 1
with patch.object(cli, "start_connection") as mock_start_connection, patch.object( with (
cli, "finish_connection", side_effect=slow_connect_fail patch.object(cli, "start_connection") as mock_start_connection,
) as mock_finish_connection: patch.object(
cli, "finish_connection", side_effect=slow_connect_fail
) as mock_finish_connection,
):
assert rl._connection_state is ReconnectLogicState.DISCONNECTED assert rl._connection_state is ReconnectLogicState.DISCONNECTED
assert rl._accept_zeroconf_records is True assert rl._accept_zeroconf_records is True
assert not rl._is_stopped assert not rl._is_stopped
@ -536,9 +541,12 @@ async def test_connect_task_not_cancelled_while_handshaking(
assert mock_start_connection.call_count == 1 assert mock_start_connection.call_count == 1
with patch.object(cli, "start_connection") as mock_start_connection, patch.object( with (
cli, "finish_connection", side_effect=slow_connect_fail patch.object(cli, "start_connection") as mock_start_connection,
) as mock_finish_connection: patch.object(
cli, "finish_connection", side_effect=slow_connect_fail
) as mock_finish_connection,
):
assert rl._connection_state is ReconnectLogicState.DISCONNECTED assert rl._connection_state is ReconnectLogicState.DISCONNECTED
assert rl._accept_zeroconf_records is True assert rl._accept_zeroconf_records is True
assert not rl._is_stopped assert not rl._is_stopped
@ -647,8 +655,9 @@ async def test_reconnect_logic_stop_callback_waits_for_handshake(
) )
assert rl._connection_state is ReconnectLogicState.DISCONNECTED assert rl._connection_state is ReconnectLogicState.DISCONNECTED
with patch.object(cli, "start_connection"), patch.object( with (
cli, "finish_connection", side_effect=slow_connect_fail patch.object(cli, "start_connection"),
patch.object(cli, "finish_connection", side_effect=slow_connect_fail),
): ):
await rl.start() await rl.start()
for _ in range(3): for _ in range(3):