mirror of
https://github.com/google/bumble.git
synced 2026-04-18 00:45:32 +00:00
Ruff: Add and fix UP rules
This commit is contained in:
@@ -19,7 +19,7 @@ This module implement the Pandora Bluetooth test APIs for the Bumble stack.
|
||||
|
||||
__version__ = "0.0.1"
|
||||
|
||||
from typing import Callable, Optional
|
||||
from collections.abc import Callable
|
||||
|
||||
import grpc
|
||||
import grpc.aio
|
||||
@@ -58,7 +58,7 @@ def register_servicer_hook(
|
||||
async def serve(
|
||||
bumble: PandoraDevice,
|
||||
config: Config = Config(),
|
||||
grpc_server: Optional[grpc.aio.Server] = None,
|
||||
grpc_server: grpc.aio.Server | None = None,
|
||||
port: int = 0,
|
||||
) -> None:
|
||||
# initialize a gRPC server if not provided.
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Optional
|
||||
from typing import Any
|
||||
|
||||
from bumble import transport
|
||||
from bumble.core import (
|
||||
@@ -54,7 +54,7 @@ class PandoraDevice:
|
||||
|
||||
# HCI transport name & instance.
|
||||
_hci_name: str
|
||||
_hci: Optional[transport.Transport] # type: ignore[name-defined]
|
||||
_hci: transport.Transport | None # type: ignore[name-defined]
|
||||
|
||||
def __init__(self, config: dict[str, Any]) -> None:
|
||||
self.config = config
|
||||
@@ -98,7 +98,7 @@ class PandoraDevice:
|
||||
await self.close()
|
||||
await self.open()
|
||||
|
||||
def info(self) -> Optional[dict[str, str]]:
|
||||
def info(self) -> dict[str, str] | None:
|
||||
return {
|
||||
'public_bd_address': str(self.device.public_address),
|
||||
'random_address': str(self.device.random_address),
|
||||
|
||||
@@ -17,7 +17,8 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import logging
|
||||
import struct
|
||||
from typing import AsyncGenerator, Optional, cast
|
||||
from collections.abc import AsyncGenerator
|
||||
from typing import cast
|
||||
|
||||
import grpc
|
||||
import grpc.aio
|
||||
@@ -623,7 +624,7 @@ class HostService(HostServicer):
|
||||
self.log.debug('Inquiry')
|
||||
|
||||
inquiry_queue: asyncio.Queue[
|
||||
Optional[tuple[Address, int, AdvertisingData, int]]
|
||||
tuple[Address, int, AdvertisingData, int] | None
|
||||
] = asyncio.Queue()
|
||||
complete_handler = self.device.on(
|
||||
self.device.EVENT_INQUIRY_COMPLETE, lambda: inquiry_queue.put_nowait(None)
|
||||
|
||||
@@ -18,8 +18,8 @@ import json
|
||||
import logging
|
||||
from asyncio import Future
|
||||
from asyncio import Queue as AsyncQueue
|
||||
from collections.abc import AsyncGenerator
|
||||
from dataclasses import dataclass
|
||||
from typing import AsyncGenerator, Optional, Union
|
||||
|
||||
import grpc
|
||||
from google.protobuf import any_pb2, empty_pb2 # pytype: disable=pyi-error
|
||||
@@ -56,7 +56,7 @@ from bumble.l2cap import (
|
||||
from bumble.pandora import utils
|
||||
from bumble.pandora.config import Config
|
||||
|
||||
L2capChannel = Union[ClassicChannel, LeCreditBasedChannel]
|
||||
L2capChannel = ClassicChannel | LeCreditBasedChannel
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -107,10 +107,8 @@ class L2CAPService(L2CAPServicer):
|
||||
oneof = request.WhichOneof('type')
|
||||
self.log.debug(f'WaitConnection channel request type: {oneof}.')
|
||||
channel_type = getattr(request, oneof)
|
||||
spec: Optional[Union[ClassicChannelSpec, LeCreditBasedChannelSpec]] = None
|
||||
l2cap_server: Optional[
|
||||
Union[ClassicChannelServer, LeCreditBasedChannelServer]
|
||||
] = None
|
||||
spec: ClassicChannelSpec | LeCreditBasedChannelSpec | None = None
|
||||
l2cap_server: ClassicChannelServer | LeCreditBasedChannelServer | None = None
|
||||
if isinstance(channel_type, CreditBasedChannelRequest):
|
||||
spec = LeCreditBasedChannelSpec(
|
||||
psm=channel_type.spsm,
|
||||
@@ -217,7 +215,7 @@ class L2CAPService(L2CAPServicer):
|
||||
oneof = request.WhichOneof('type')
|
||||
self.log.debug(f'Channel request type: {oneof}.')
|
||||
channel_type = getattr(request, oneof)
|
||||
spec: Optional[Union[ClassicChannelSpec, LeCreditBasedChannelSpec]] = None
|
||||
spec: ClassicChannelSpec | LeCreditBasedChannelSpec | None = None
|
||||
if isinstance(channel_type, CreditBasedChannelRequest):
|
||||
spec = LeCreditBasedChannelSpec(
|
||||
psm=channel_type.spsm,
|
||||
|
||||
@@ -17,8 +17,8 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import contextlib
|
||||
import logging
|
||||
from collections.abc import Awaitable
|
||||
from typing import Any, AsyncGenerator, AsyncIterator, Callable, Optional, Union
|
||||
from collections.abc import AsyncGenerator, AsyncIterator, Awaitable, Callable
|
||||
from typing import Any
|
||||
|
||||
import grpc
|
||||
from google.protobuf import (
|
||||
@@ -66,7 +66,7 @@ class PairingDelegate(BasePairingDelegate):
|
||||
def __init__(
|
||||
self,
|
||||
connection: BumbleConnection,
|
||||
service: "SecurityService",
|
||||
service: SecurityService,
|
||||
io_capability: BasePairingDelegate.IoCapability = BasePairingDelegate.NO_OUTPUT_NO_INPUT,
|
||||
local_initiator_key_distribution: BasePairingDelegate.KeyDistribution = BasePairingDelegate.DEFAULT_KEY_DISTRIBUTION,
|
||||
local_responder_key_distribution: BasePairingDelegate.KeyDistribution = BasePairingDelegate.DEFAULT_KEY_DISTRIBUTION,
|
||||
@@ -132,7 +132,7 @@ class PairingDelegate(BasePairingDelegate):
|
||||
assert answer.answer_variant() == 'confirm' and answer.confirm is not None
|
||||
return answer.confirm
|
||||
|
||||
async def get_number(self) -> Optional[int]:
|
||||
async def get_number(self) -> int | None:
|
||||
self.log.debug(
|
||||
f"Pairing event: `passkey_entry_request` (io_capability: {self.io_capability})"
|
||||
)
|
||||
@@ -149,7 +149,7 @@ class PairingDelegate(BasePairingDelegate):
|
||||
assert answer.answer_variant() == 'passkey'
|
||||
return answer.passkey
|
||||
|
||||
async def get_string(self, max_length: int) -> Optional[str]:
|
||||
async def get_string(self, max_length: int) -> str | None:
|
||||
self.log.debug(
|
||||
f"Pairing event: `pin_code_request` (io_capability: {self.io_capability})"
|
||||
)
|
||||
@@ -197,8 +197,8 @@ class SecurityService(SecurityServicer):
|
||||
self.log = utils.BumbleServerLoggerAdapter(
|
||||
logging.getLogger(), {'service_name': 'Security', 'device': device}
|
||||
)
|
||||
self.event_queue: Optional[asyncio.Queue[PairingEvent]] = None
|
||||
self.event_answer: Optional[AsyncIterator[PairingEventAnswer]] = None
|
||||
self.event_queue: asyncio.Queue[PairingEvent] | None = None
|
||||
self.event_answer: AsyncIterator[PairingEventAnswer] | None = None
|
||||
self.device = device
|
||||
self.config = config
|
||||
|
||||
@@ -233,7 +233,7 @@ class SecurityService(SecurityServicer):
|
||||
if level == LEVEL2:
|
||||
return connection.encryption != 0 and connection.authenticated
|
||||
|
||||
link_key_type: Optional[int] = None
|
||||
link_key_type: int | None = None
|
||||
if (keystore := connection.device.keystore) and (
|
||||
keys := await keystore.get(str(connection.peer_address))
|
||||
):
|
||||
@@ -412,8 +412,8 @@ class SecurityService(SecurityServicer):
|
||||
wait_for_security: asyncio.Future[str] = (
|
||||
asyncio.get_running_loop().create_future()
|
||||
)
|
||||
authenticate_task: Optional[asyncio.Future[None]] = None
|
||||
pair_task: Optional[asyncio.Future[None]] = None
|
||||
authenticate_task: asyncio.Future[None] | None = None
|
||||
pair_task: asyncio.Future[None] | None = None
|
||||
|
||||
async def authenticate() -> None:
|
||||
if (encryption := connection.encryption) != 0:
|
||||
@@ -459,7 +459,7 @@ class SecurityService(SecurityServicer):
|
||||
if self.need_pairing(connection, level):
|
||||
bumble.utils.AsyncRunner.spawn(connection.pair())
|
||||
|
||||
listeners: dict[str, Callable[..., Union[None, Awaitable[None]]]] = {
|
||||
listeners: dict[str, Callable[..., None | Awaitable[None]]] = {
|
||||
'disconnection': set_failure('connection_died'),
|
||||
'pairing_failure': set_failure('pairing_failure'),
|
||||
'connection_authentication_failure': set_failure('authentication_failure'),
|
||||
@@ -502,7 +502,7 @@ class SecurityService(SecurityServicer):
|
||||
return WaitSecurityResponse(**kwargs)
|
||||
|
||||
async def reached_security_level(
|
||||
self, connection: BumbleConnection, level: Union[SecurityLevel, LESecurityLevel]
|
||||
self, connection: BumbleConnection, level: SecurityLevel | LESecurityLevel
|
||||
) -> bool:
|
||||
self.log.debug(
|
||||
str(
|
||||
|
||||
@@ -18,7 +18,8 @@ import contextlib
|
||||
import functools
|
||||
import inspect
|
||||
import logging
|
||||
from typing import Any, Generator, MutableMapping, Optional
|
||||
from collections.abc import Generator, MutableMapping
|
||||
from typing import Any
|
||||
|
||||
import grpc
|
||||
from google.protobuf.message import Message # pytype: disable=pyi-error
|
||||
@@ -34,7 +35,7 @@ ADDRESS_TYPES: dict[str, AddressType] = {
|
||||
}
|
||||
|
||||
|
||||
def address_from_request(request: Message, field: Optional[str]) -> Address:
|
||||
def address_from_request(request: Message, field: str | None) -> Address:
|
||||
if field is None:
|
||||
return Address.ANY
|
||||
return Address(bytes(reversed(getattr(request, field))), ADDRESS_TYPES[field])
|
||||
@@ -95,8 +96,7 @@ def rpc(func: Any) -> Any:
|
||||
@functools.wraps(func)
|
||||
def gen_wrapper(self: Any, request: Any, context: grpc.ServicerContext) -> Any:
|
||||
with exception_to_rpc_error(context):
|
||||
for v in func(self, request, context):
|
||||
yield v
|
||||
yield from func(self, request, context)
|
||||
|
||||
@functools.wraps(func)
|
||||
def wrapper(self: Any, request: Any, context: grpc.ServicerContext) -> Any:
|
||||
|
||||
Reference in New Issue
Block a user