From c94065bcbd9afe61ea98a3ed41355236ba7a8435 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 6 Mar 2025 14:14:27 -1000 Subject: [PATCH] chore: update aio package typing --- src/dbus_fast/aio/__init__.py | 2 ++ src/dbus_fast/aio/message_bus.py | 32 +++++++++++++++-------------- src/dbus_fast/aio/message_reader.py | 10 +++++---- src/dbus_fast/aio/proxy_object.py | 10 +++++---- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/dbus_fast/aio/__init__.py b/src/dbus_fast/aio/__init__.py index 82353324..0a975ca3 100644 --- a/src/dbus_fast/aio/__init__.py +++ b/src/dbus_fast/aio/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from .message_bus import MessageBus as MessageBus from .proxy_object import ProxyInterface as ProxyInterface from .proxy_object import ProxyObject as ProxyObject diff --git a/src/dbus_fast/aio/message_bus.py b/src/dbus_fast/aio/message_bus.py index 52babe84..8bbb0f4d 100644 --- a/src/dbus_fast/aio/message_bus.py +++ b/src/dbus_fast/aio/message_bus.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import array import asyncio import contextlib @@ -6,7 +8,7 @@ from collections import deque from copy import copy from functools import partial -from typing import Any, Callable, Optional +from typing import Any, Callable from .. import introspection as intr from ..auth import Authenticator, AuthExternal @@ -56,20 +58,20 @@ def _future_set_result(fut: asyncio.Future, result: Any) -> None: class _MessageWriter: """A class to handle writing messages to the message bus.""" - def __init__(self, bus: "MessageBus") -> None: + def __init__(self, bus: MessageBus) -> None: """A class to handle writing messages to the message bus.""" self.messages: deque[ - tuple[bytearray, Optional[list[int]], Optional[asyncio.Future]] + tuple[bytearray, list[int] | None, asyncio.Future | None] ] = deque() self.negotiate_unix_fd = bus._negotiate_unix_fd self.bus = bus self.sock = bus._sock self.loop = bus._loop - self.buf: Optional[memoryview] = None + self.buf: memoryview | None = None self.fd = bus._fd self.offset = 0 - self.unix_fds: Optional[list[int]] = None - self.fut: Optional[asyncio.Future] = None + self.unix_fds: list[int] | None = None + self.fut: asyncio.Future | None = None def write_callback(self, remove_writer: bool = True) -> None: """The callback to write messages to the message bus.""" @@ -119,7 +121,7 @@ def write_callback(self, remove_writer: bool = True) -> None: self.bus._finalize(e) def buffer_message( - self, msg: Message, future: Optional[asyncio.Future] = None + self, msg: Message, future: asyncio.Future | None = None ) -> None: """Buffer a message to be sent later.""" unix_fds = msg.unix_fds @@ -136,7 +138,7 @@ def _write_without_remove_writer(self) -> None: self.write_callback(remove_writer=False) def schedule_write( - self, msg: Optional[Message] = None, future: Optional[asyncio.Future] = None + self, msg: Message | None = None, future: asyncio.Future | None = None ) -> None: """Schedule a message to be written.""" queue_is_empty = not self.messages @@ -194,9 +196,9 @@ class MessageBus(BaseMessageBus): def __init__( self, - bus_address: Optional[str] = None, + bus_address: str | None = None, bus_type: BusType = BusType.SESSION, - auth: Optional[Authenticator] = None, + auth: Authenticator | None = None, negotiate_unix_fd: bool = False, ) -> None: super().__init__(bus_address, bus_type, ProxyObject, negotiate_unix_fd) @@ -212,7 +214,7 @@ def __init__( self._disconnect_future = self._loop.create_future() self._pending_futures: set[asyncio.Future] = set() - async def connect(self) -> "MessageBus": + async def connect(self) -> MessageBus: """Connect this message bus to the DBus daemon. This method must be called before the message bus can be used. @@ -366,7 +368,7 @@ async def release_name(self, name: str) -> ReleaseNameReply: return await future - async def call(self, msg: Message) -> Optional[Message]: + async def call(self, msg: Message) -> Message | None: """Send a method call and wait for a reply from the DBus daemon. :param msg: The method call message to send. @@ -443,7 +445,7 @@ def _future_exception_no_reply(self, fut: asyncio.Future) -> None: logging.exception("unexpected exception in future", exc_info=e) def _make_method_handler( - self, interface: "ServiceInterface", method: "_Method" + self, interface: ServiceInterface, method: _Method ) -> Callable[[Message, Callable[[Message], None]], None]: if not asyncio.iscoroutinefunction(method.fn): return super()._make_method_handler(interface, method) @@ -534,7 +536,7 @@ def disconnect(self) -> None: except Exception: logging.warning("could not close socket", exc_info=True) - def _finalize(self, err: Optional[Exception] = None) -> None: + def _finalize(self, err: Exception | None = None) -> None: try: self._loop.remove_reader(self._fd) except Exception: @@ -565,7 +567,7 @@ def _finalize(self, err: Optional[Exception] = None) -> None: _future_set_result(self._disconnect_future, None) def _reply_handler( - self, future: asyncio.Future, reply: Optional[Any], err: Optional[Exception] + self, future: asyncio.Future, reply: Any | None, err: Exception | None ) -> None: """The reply handler for method calls.""" if err: diff --git a/src/dbus_fast/aio/message_reader.py b/src/dbus_fast/aio/message_reader.py index 3964d1b4..06a6b979 100644 --- a/src/dbus_fast/aio/message_reader.py +++ b/src/dbus_fast/aio/message_reader.py @@ -1,7 +1,9 @@ +from __future__ import annotations + import logging import socket from functools import partial -from typing import Callable, Optional +from typing import Callable from .._private.unmarshaller import Unmarshaller from ..message import Message @@ -10,7 +12,7 @@ def _message_reader( unmarshaller: Unmarshaller, process: Callable[[Message], None], - finalize: Callable[[Optional[Exception]], None], + finalize: Callable[[Exception | None], None], negotiate_unix_fd: bool, ) -> None: """Reads messages from the unmarshaller and passes them to the process function.""" @@ -35,9 +37,9 @@ def _message_reader( def build_message_reader( - sock: Optional[socket.socket], + sock: socket.socket | None, process: Callable[[Message], None], - finalize: Callable[[Optional[Exception]], None], + finalize: Callable[[Exception | None], None], negotiate_unix_fd: bool, ) -> Callable[[], None]: """Build a callable that reads messages from the unmarshaller and passes them to the process function.""" diff --git a/src/dbus_fast/aio/proxy_object.py b/src/dbus_fast/aio/proxy_object.py index 75e1ba00..6fc1ac6a 100644 --- a/src/dbus_fast/aio/proxy_object.py +++ b/src/dbus_fast/aio/proxy_object.py @@ -1,5 +1,7 @@ +from __future__ import annotations + import xml.etree.ElementTree as ET -from typing import TYPE_CHECKING, Any, Union +from typing import TYPE_CHECKING, Any from .. import introspection as intr from .._private.util import replace_fds_with_idx, replace_idx_with_fds @@ -79,7 +81,7 @@ class ProxyInterface(BaseProxyInterface): ` will be raised with information about the error. """ - bus: "AioMessageBus" + bus: AioMessageBus def _add_method(self, intr_method: intr.Method) -> None: async def method_fn( @@ -193,7 +195,7 @@ def __init__( self, bus_name: str, path: str, - introspection: Union[intr.Node, str, ET.Element], + introspection: intr.Node | str | ET.Element, bus: BaseMessageBus, ) -> None: super().__init__(bus_name, path, introspection, bus, ProxyInterface) @@ -201,5 +203,5 @@ def __init__( def get_interface(self, name: str) -> ProxyInterface: return super().get_interface(name) - def get_children(self) -> list["ProxyObject"]: + def get_children(self) -> list[ProxyObject]: return super().get_children()