Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add additional typing #100

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down
23 changes: 14 additions & 9 deletions src/dbus_fast/errors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import Optional, Union


class SignatureBodyMismatchError(ValueError):
pass

Expand Down Expand Up @@ -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}")


Expand All @@ -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")

Expand All @@ -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)
21 changes: 9 additions & 12 deletions src/dbus_fast/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it was actually a bug

self.error_name = (
str(error_name.value) if type(error_name) is ErrorType else error_name
)
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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.

Expand All @@ -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:
Expand Down