Skip to content

Commit

Permalink
chore: update aio package typing
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Mar 7, 2025
1 parent f64eb58 commit c94065b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/dbus_fast/aio/__init__.py
Original file line number Diff line number Diff line change
@@ -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
32 changes: 17 additions & 15 deletions src/dbus_fast/aio/message_bus.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import array
import asyncio
import contextlib
Expand All @@ -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
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 6 additions & 4 deletions src/dbus_fast/aio/message_reader.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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."""
Expand All @@ -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."""
Expand Down
10 changes: 6 additions & 4 deletions src/dbus_fast/aio/proxy_object.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -79,7 +81,7 @@ class ProxyInterface(BaseProxyInterface):
<dbus_fast.DBusError>` 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(
Expand Down Expand Up @@ -193,13 +195,13 @@ 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)

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()

0 comments on commit c94065b

Please sign in to comment.