From 70fb9c20fabde1b856ba9add2313f0b2e339ba40 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 10 Oct 2022 13:10:26 -1000 Subject: [PATCH] feat: add additional typing --- pyproject.toml | 2 +- src/dbus_fast/errors.py | 23 ++++++++++++++--------- src/dbus_fast/message.py | 21 +++++++++------------ 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 578a3aa7..b51802e2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -83,7 +83,7 @@ known_first_party = ["dbus_fast", "tests"] [tool.mypy] check_untyped_defs = true -disallow_any_generics = true +disallow_any_generics = false # turn this on when we drop 3.7/3.8 support disallow_incomplete_defs = true disallow_untyped_defs = true mypy_path = "src/" diff --git a/src/dbus_fast/errors.py b/src/dbus_fast/errors.py index a439ecc0..0ef39c16 100644 --- a/src/dbus_fast/errors.py +++ b/src/dbus_fast/errors.py @@ -1,3 +1,6 @@ +from typing import Optional, Union + + class SignatureBodyMismatchError(ValueError): pass @@ -31,22 +34,22 @@ class SignalDisabledError(Exception): class InvalidBusNameError(TypeError): - def __init__(self, name): + def __init__(self, name: str) -> None: super().__init__(f"invalid bus name: {name}") class InvalidObjectPathError(TypeError): - def __init__(self, path): + def __init__(self, path: str) -> None: super().__init__(f"invalid object path: {path}") class InvalidInterfaceNameError(TypeError): - def __init__(self, name): + def __init__(self, name: str) -> None: super().__init__(f"invalid interface name: {name}") class InvalidMemberNameError(TypeError): - def __init__(self, member): + def __init__(self, member: str) -> None: super().__init__(f"invalid member name: {member}") @@ -56,13 +59,15 @@ def __init__(self, member): class DBusError(Exception): - def __init__(self, type_, text, reply=None): + def __init__( + self, type_: Union[ErrorType, str], text: str, reply: Optional[Message] = None + ) -> None: super().__init__(text) if type(type_) is ErrorType: type_ = type_.value - assert_interface_name_valid(type_) + assert_interface_name_valid(type_) # type: ignore[arg-type] if reply is not None and type(reply) is not Message: raise TypeError("reply must be of type Message") @@ -71,9 +76,9 @@ def __init__(self, type_, text, reply=None): self.reply = reply @staticmethod - def _from_message(msg): + def _from_message(msg: Message) -> "DBusError": assert msg.message_type == MessageType.ERROR - return DBusError(msg.error_name, msg.body[0], reply=msg) + return DBusError(msg.error_name or "unknown", msg.body[0], reply=msg) - def _as_message(self, msg): + def _as_message(self, msg: Message) -> Message: return Message.new_error(msg, self.type, self.text) diff --git a/src/dbus_fast/message.py b/src/dbus_fast/message.py index 453933e0..b123c0db 100644 --- a/src/dbus_fast/message.py +++ b/src/dbus_fast/message.py @@ -100,9 +100,9 @@ def __init__( interface: Optional[str] = None, member: Optional[str] = None, message_type: MessageType = MessageType.METHOD_CALL, - flags: MessageFlag = MessageFlag.NONE, + flags: Union[MessageFlag, int] = MessageFlag.NONE, error_name: Optional[Union[str, ErrorType]] = None, - reply_serial=0, + reply_serial: int = 0, sender: Optional[str] = None, unix_fds: List[int] = [], signature: Optional[Union[SignatureTree, str]] = None, @@ -115,9 +115,7 @@ def __init__( self.interface = interface self.member = member self.message_type = message_type - self.flags = ( - flags if type(flags) is MessageFlag else MessageFlag(bytes([flags])) - ) + self.flags = flags if type(flags) is MessageFlag else MessageFlag(flags) self.error_name = ( str(error_name.value) if type(error_name) is ErrorType else error_name ) @@ -128,7 +126,7 @@ def __init__( self.signature = signature.signature self.signature_tree = signature else: - self.signature = signature or "" + self.signature = signature or "" # type: ignore[assignment] self.signature_tree = get_signature_tree(signature or "") self.body = body self.serial = serial or 0 @@ -144,7 +142,7 @@ def __init__( if self.member is not None: assert_member_name_valid(self.member) if self.error_name is not None: - assert_interface_name_valid(self.error_name) + assert_interface_name_valid(self.error_name) # type: ignore[arg-type] required_fields = REQUIRED_FIELDS.get(self.message_type) if not required_fields: @@ -219,8 +217,8 @@ def new_signal( interface: str, member: str, signature: str = "", - body: List[Any] = None, - unix_fds: List[int] = None, + body: Optional[List[Any]] = None, + unix_fds: Optional[List[int]] = None, ) -> "Message": """A convenience constructor to create a new signal message. @@ -246,15 +244,14 @@ def new_signal( - :class:`InvalidInterfaceNameError` - If ``interface`` is not a valid interface name. - :class:`InvalidMemberNameError` - If ``member`` is not a valid member name. """ - body = body if body else [] return Message( message_type=MessageType.SIGNAL, interface=interface, path=path, member=member, signature=signature, - body=body, - unix_fds=unix_fds, + body=body or [], + unix_fds=unix_fds or [], ) def _marshall(self, negotiate_unix_fd: bool) -> bytearray: