Ruff: Add and fix UP rules

This commit is contained in:
Josh Wu
2026-01-01 03:07:06 +08:00
parent 8e28f4e159
commit 3f643de4c1
102 changed files with 922 additions and 999 deletions

View File

@@ -46,7 +46,7 @@ class Keyboard:
def decode_keyboard_report(self, input_report: bytes, report_length: int) -> None:
if report_length >= 8:
modifier = input_report[1]
self.report[0] = [int(x) for x in '{0:08b}'.format(modifier)]
self.report[0] = [int(x) for x in f'{modifier:08b}']
self.report[0].reverse() # type: ignore
modifier_key = str((modifier & 0x22).to_bytes(1, "big").hex())
@@ -106,7 +106,7 @@ class Mouse:
]
def decode_mouse_report(self, input_report: bytes, report_length: int) -> None:
self.report[0] = [int(x) for x in '{0:08b}'.format(input_report[1])]
self.report[0] = [int(x) for x in f'{input_report[1]:08b}']
self.report[0].reverse() # type: ignore
self.report[1] = input_report[2]
self.report[2] = input_report[3]

View File

@@ -49,9 +49,9 @@ async def handle_command_client(
await ams_client.command(RemoteCommandId[command.upper()])
continue
except Exception as error:
writer.write(f"ERROR: {error}\n".encode("utf-8"))
writer.write(f"ERROR: {error}\n".encode())
writer.write(f"unknown command {command}\n".encode("utf-8"))
writer.write(f"unknown command {command}\n".encode())
# -----------------------------------------------------------------------------

View File

@@ -91,15 +91,13 @@ async def process_notifications(ancs_client: AncsClient):
notification.notification_uid, requested_attributes
)
max_attribute_name_width = max(
(len(attribute.attribute_id.name) for attribute in attributes)
len(attribute.attribute_id.name) for attribute in attributes
)
app_identifier = str(
next(
(
attribute.value
for attribute in attributes
if attribute.attribute_id == NotificationAttributeId.APP_IDENTIFIER
)
attribute.value
for attribute in attributes
if attribute.attribute_id == NotificationAttributeId.APP_IDENTIFIER
)
)
if app_identifier not in _cached_app_names:
@@ -145,9 +143,9 @@ async def handle_command_client(
notification_uid = int(command_args)
await ancs_client.perform_negative_action(notification_uid)
else:
writer.write(f"unknown command {command_name}".encode("utf-8"))
writer.write(f"unknown command {command_name}".encode())
except Exception as error:
writer.write(f"ERROR: {error}\n".encode("utf-8"))
writer.write(f"ERROR: {error}\n".encode())
# -----------------------------------------------------------------------------

View File

@@ -18,7 +18,6 @@
import asyncio
import logging
import sys
from typing import Optional
import websockets.asyncio.server
@@ -29,7 +28,7 @@ from bumble.device import AdvertisingParameters, Device
from bumble.profiles import asha
from bumble.transport import open_transport
ws_connection: Optional[websockets.asyncio.server.ServerConnection] = None
ws_connection: websockets.asyncio.server.ServerConnection | None = None
g722_decoder = decoder.G722Decoder()

View File

@@ -21,7 +21,6 @@ import asyncio
import json
import logging
import sys
from typing import Optional
import websockets.asyncio.server
@@ -218,7 +217,7 @@ def on_avrcp_start(avrcp_protocol: avrcp.Protocol, websocket_server: WebSocketSe
# -----------------------------------------------------------------------------
class WebSocketServer:
socket: Optional[websockets.asyncio.server.ServerConnection]
socket: websockets.asyncio.server.ServerConnection | None
def __init__(
self, avrcp_protocol: avrcp.Protocol, avrcp_delegate: Delegate

View File

@@ -52,7 +52,7 @@ async def main() -> None:
if device.host.number_of_supported_advertising_sets >= 1:
advertising_data1 = AdvertisingData(
[(AdvertisingData.COMPLETE_LOCAL_NAME, "Bumble 1".encode("utf-8"))]
[(AdvertisingData.COMPLETE_LOCAL_NAME, b"Bumble 1")]
)
set1 = await device.create_advertising_set(
@@ -61,7 +61,7 @@ async def main() -> None:
print("Selected TX power 1:", set1.selected_tx_power)
advertising_data2 = AdvertisingData(
[(AdvertisingData.COMPLETE_LOCAL_NAME, "Bumble 2".encode("utf-8"))]
[(AdvertisingData.COMPLETE_LOCAL_NAME, b"Bumble 2")]
)
# pylint: disable=possibly-used-before-assignment
@@ -78,7 +78,7 @@ async def main() -> None:
if device.host.number_of_supported_advertising_sets >= 3:
scan_response_data3 = AdvertisingData(
[(AdvertisingData.COMPLETE_LOCAL_NAME, "Bumble 3".encode("utf-8"))]
[(AdvertisingData.COMPLETE_LOCAL_NAME, b"Bumble 3")]
)
set3 = await device.create_advertising_set(

View File

@@ -70,13 +70,13 @@ async def main() -> None:
descriptor = Descriptor(
GATT_CHARACTERISTIC_USER_DESCRIPTION_DESCRIPTOR,
Descriptor.READABLE,
'My Description'.encode(),
b'My Description',
)
manufacturer_name_characteristic = Characteristic[bytes](
GATT_MANUFACTURER_NAME_STRING_CHARACTERISTIC,
Characteristic.Properties.READ,
Characteristic.READABLE,
"Fitbit".encode(),
b"Fitbit",
[descriptor],
)
device_info_service = Service(

View File

@@ -93,13 +93,13 @@ async def main() -> None:
descriptor = Descriptor(
GATT_CHARACTERISTIC_USER_DESCRIPTION_DESCRIPTOR,
Descriptor.READABLE,
'My Description'.encode(),
b'My Description',
)
manufacturer_name_characteristic = Characteristic(
GATT_MANUFACTURER_NAME_STRING_CHARACTERISTIC,
Characteristic.Properties.READ,
Characteristic.READABLE,
'Fitbit'.encode(),
b'Fitbit',
[descriptor],
)
device_info_service = Service(

View File

@@ -24,7 +24,7 @@ import functools
import random
import struct
import sys
from typing import Any, Union
from typing import Any
import bumble.logging
from bumble import core, gatt, gatt_adapters, gatt_client, hci, transport
@@ -118,7 +118,7 @@ async def client(device: Device, address: hci.Address) -> None:
c1 = characteristics[0]
c1_value = await c1.read_value()
print(f"@@@ C1 {c1} value = {c1_value!r} (type={type(c1_value)})")
await c1.write_value("happy π day".encode("utf-8"))
await c1.write_value("happy π day".encode())
await c1.subscribe(functools.partial(on_adapted_characteristic_update, c1))
# Static characteristic with a string value.
@@ -187,7 +187,7 @@ async def client(device: Device, address: hci.Address) -> None:
# -----------------------------------------------------------------------------
def dynamic_read(selector: str) -> Union[bytes, str]:
def dynamic_read(selector: str) -> bytes | str:
if selector == "bytes":
print("$$$ Returning random bytes")
return random.randbytes(7)

View File

@@ -20,7 +20,7 @@ import io
import json
import logging
import sys
from typing import Iterable, Optional
from collections.abc import Iterable
import websockets.asyncio.server
@@ -33,9 +33,9 @@ from bumble.transport import open_transport
logger = logging.getLogger(__name__)
ws: Optional[websockets.asyncio.server.ServerConnection] = None
ag_protocol: Optional[hfp.AgProtocol] = None
source_file: Optional[io.BufferedReader] = None
ws: websockets.asyncio.server.ServerConnection | None = None
ag_protocol: hfp.AgProtocol | None = None
source_file: io.BufferedReader | None = None
def _default_configuration() -> hfp.AgConfiguration:

View File

@@ -20,7 +20,6 @@ import contextlib
import functools
import json
import sys
from typing import Optional
import websockets.asyncio.server
@@ -30,8 +29,8 @@ from bumble.device import Connection, Device
from bumble.hfp import HfProtocol
from bumble.transport import open_transport
ws: Optional[websockets.asyncio.server.ServerConnection] = None
hf_protocol: Optional[HfProtocol] = None
ws: websockets.asyncio.server.ServerConnection | None = None
hf_protocol: HfProtocol | None = None
# -----------------------------------------------------------------------------

View File

@@ -18,7 +18,6 @@
import asyncio
import json
import sys
from typing import Optional
import websockets.asyncio.server
@@ -101,8 +100,8 @@ async def main() -> None:
)
device.add_service(AudioStreamControlService(device, sink_ase_id=[1]))
ws: Optional[websockets.asyncio.server.ServerConnection] = None
mcp: Optional[MediaControlServiceProxy] = None
ws: websockets.asyncio.server.ServerConnection | None = None
mcp: MediaControlServiceProxy | None = None
advertising_data = bytes(
AdvertisingData(

View File

@@ -19,7 +19,6 @@ import asyncio
import json
import secrets
import sys
from typing import Optional
import websockets.asyncio.server
@@ -110,7 +109,7 @@ async def main() -> None:
vcs = VolumeControlService()
device.add_service(vcs)
ws: Optional[websockets.asyncio.server.ServerConnection] = None
ws: websockets.asyncio.server.ServerConnection | None = None
def on_volume_state_change():
if ws: