From 78c4256c14a75dd54117edea638f9a851b9ed921 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 9 Oct 2022 09:05:41 -1000 Subject: [PATCH 1/3] feat: add additional typing --- src/dbus_fast/constants.py | 4 +-- src/dbus_fast/signature.py | 67 ++++++++++++++++++------------------- src/dbus_fast/validators.py | 10 +++--- 3 files changed, 39 insertions(+), 42 deletions(-) diff --git a/src/dbus_fast/constants.py b/src/dbus_fast/constants.py index 9aa62fdc..1a368c30 100644 --- a/src/dbus_fast/constants.py +++ b/src/dbus_fast/constants.py @@ -69,11 +69,11 @@ class PropertyAccess(Enum): WRITE = "write" #: The property is writeonly. READWRITE = "readwrite" #: The property can be read or written to. - def readable(self): + def readable(self) -> bool: """Get whether the property can be read.""" return self == PropertyAccess.READ or self == PropertyAccess.READWRITE - def writable(self): + def writable(self) -> bool: """Get whether the property can be written to.""" return self == PropertyAccess.WRITE or self == PropertyAccess.READWRITE diff --git a/src/dbus_fast/signature.py b/src/dbus_fast/signature.py index f5546026..f62ffd89 100644 --- a/src/dbus_fast/signature.py +++ b/src/dbus_fast/signature.py @@ -1,5 +1,5 @@ from functools import lru_cache -from typing import Any, List, Union +from typing import Any, Callable, Iterable, List, Optional, Tuple, Union from .errors import InvalidSignatureError, SignatureBodyMismatchError from .validators import is_object_path_valid @@ -26,15 +26,14 @@ class to parse signatures. def __init__(self, token: str) -> None: self.token = token self.children: List[SignatureType] = [] - self._signature = None + self._signature: Optional[str] = None - def __eq__(self, other): + def __eq__(self, other: Any) -> bool: if type(other) is SignatureType: return self.signature == other.signature - else: - return super().__eq__(other) + return super().__eq__(other) - def _collapse(self): + def _collapse(self) -> str: if self.token not in "a({": return self.token @@ -58,9 +57,9 @@ def signature(self) -> str: return self._signature @staticmethod - def _parse_next(signature): + def _parse_next(signature: str) -> Tuple["SignatureType", str]: if not signature: - return (None, "") + raise InvalidSignatureError("Cannot parse an empty signature") token = signature[0] @@ -103,7 +102,7 @@ def _parse_next(signature): # basic type return (SignatureType(token), signature[1:]) - def _verify_byte(self, body): + def _verify_byte(self, body: int) -> None: BYTE_MIN = 0x00 BYTE_MAX = 0xFF if not isinstance(body, int): @@ -115,13 +114,13 @@ def _verify_byte(self, body): f"DBus BYTE type must be between {BYTE_MIN} and {BYTE_MAX}" ) - def _verify_boolean(self, body): + def _verify_boolean(self, body: bool) -> None: if not isinstance(body, bool): raise SignatureBodyMismatchError( f'DBus BOOLEAN type "b" must be Python type "bool", got {type(body)}' ) - def _verify_int16(self, body): + def _verify_int16(self, body: int) -> None: INT16_MIN = -0x7FFF - 1 INT16_MAX = 0x7FFF if not isinstance(body, int): @@ -133,7 +132,7 @@ def _verify_int16(self, body): f'DBus INT16 type "n" must be between {INT16_MIN} and {INT16_MAX}' ) - def _verify_uint16(self, body): + def _verify_uint16(self, body: int) -> None: UINT16_MIN = 0 UINT16_MAX = 0xFFFF if not isinstance(body, int): @@ -145,7 +144,7 @@ def _verify_uint16(self, body): f'DBus UINT16 type "q" must be between {UINT16_MIN} and {UINT16_MAX}' ) - def _verify_int32(self, body): + def _verify_int32(self, body: int) -> None: INT32_MIN = -0x7FFFFFFF - 1 INT32_MAX = 0x7FFFFFFF if not isinstance(body, int): @@ -157,7 +156,7 @@ def _verify_int32(self, body): f'DBus INT32 type "i" must be between {INT32_MIN} and {INT32_MAX}' ) - def _verify_uint32(self, body): + def _verify_uint32(self, body: int) -> None: UINT32_MIN = 0 UINT32_MAX = 0xFFFFFFFF if not isinstance(body, int): @@ -169,7 +168,7 @@ def _verify_uint32(self, body): f'DBus UINT32 type "u" must be between {UINT32_MIN} and {UINT32_MAX}' ) - def _verify_int64(self, body): + def _verify_int64(self, body: int) -> None: INT64_MAX = 9223372036854775807 INT64_MIN = -INT64_MAX - 1 if not isinstance(body, int): @@ -181,7 +180,7 @@ def _verify_int64(self, body): f'DBus INT64 type "x" must be between {INT64_MIN} and {INT64_MAX}' ) - def _verify_uint64(self, body): + def _verify_uint64(self, body: int) -> None: UINT64_MIN = 0 UINT64_MAX = 18446744073709551615 if not isinstance(body, int): @@ -193,13 +192,13 @@ def _verify_uint64(self, body): f'DBus UINT64 type "t" must be between {UINT64_MIN} and {UINT64_MAX}' ) - def _verify_double(self, body): - if not isinstance(body, float) and not isinstance(body, int): + def _verify_double(self, body: Union[float, int]) -> None: + if not isinstance(body, (float, int)): raise SignatureBodyMismatchError( f'DBus DOUBLE type "d" must be Python type "float" or "int", got {type(body)}' ) - def _verify_unix_fd(self, body): + def _verify_unix_fd(self, body: int) -> None: try: self._verify_uint32(body) except SignatureBodyMismatchError: @@ -207,19 +206,19 @@ def _verify_unix_fd(self, body): 'DBus UNIX_FD type "h" must be a valid UINT32' ) - def _verify_object_path(self, body): + def _verify_object_path(self, body: str) -> None: if not is_object_path_valid(body): raise SignatureBodyMismatchError( 'DBus OBJECT_PATH type "o" must be a valid object path' ) - def _verify_string(self, body): + def _verify_string(self, body: str) -> None: if not isinstance(body, str): raise SignatureBodyMismatchError( f'DBus STRING type "s" must be Python type "str", got {type(body)}' ) - def _verify_signature(self, body): + def _verify_signature(self, body: str) -> None: # I guess we could run it through the SignatureTree parser instead if not isinstance(body, str): raise SignatureBodyMismatchError( @@ -230,7 +229,7 @@ def _verify_signature(self, body): 'DBus SIGNATURE type "g" must be less than 256 bytes' ) - def _verify_array(self, body): + def _verify_array(self, body: Any) -> None: child_type = self.children[0] if child_type.token == "{": @@ -255,7 +254,7 @@ def _verify_array(self, body): for member in body: child_type.verify(member) - def _verify_struct(self, body): + def _verify_struct(self, body: Iterable[Any]) -> None: # TODO allow tuples if not isinstance(body, list): raise SignatureBodyMismatchError( @@ -270,7 +269,7 @@ def _verify_struct(self, body): for i, member in enumerate(body): self.children[i].verify(member) - def _verify_variant(self, body): + def _verify_variant(self, body: "Variant") -> None: # a variant signature and value is valid by construction if not isinstance(body, Variant): raise SignatureBodyMismatchError( @@ -294,7 +293,7 @@ def verify(self, body: Any) -> bool: return True - validators = { + validators: dict[str, Callable[["SignatureType", Any], None]] = { "y": _verify_byte, "b": _verify_boolean, "n": _verify_int16, @@ -332,7 +331,7 @@ class SignatureTree: __slots__ = ("signature", "types") - def __init__(self, signature: str = ""): + def __init__(self, signature: str = "") -> None: self.signature = signature self.types: List[SignatureType] = [] @@ -344,13 +343,12 @@ def __init__(self, signature: str = ""): (type_, signature) = SignatureType._parse_next(signature) self.types.append(type_) - def __eq__(self, other): + def __eq__(self, other: Any) -> bool: if type(other) is SignatureTree: return self.signature == other.signature - else: - return super().__eq__(other) + return super().__eq__(other) - def verify(self, body: List[Any]): + def verify(self, body: List[Any]) -> bool: """Verifies that the give body matches this signature tree :param body: the body to verify for this tree @@ -433,13 +431,12 @@ def __init__( self.signature = signature_str self.value = value - def __eq__(self, other): + def __eq__(self, other: Any) -> bool: if type(other) is Variant: return self.signature == other.signature and self.value == other.value - else: - return super().__eq__(other) + return super().__eq__(other) - def __repr__(self): + def __repr__(self) -> str: return "".format( self.type.signature, self.value ) diff --git a/src/dbus_fast/validators.py b/src/dbus_fast/validators.py index b17e6621..594310e9 100644 --- a/src/dbus_fast/validators.py +++ b/src/dbus_fast/validators.py @@ -27,7 +27,7 @@ def is_bus_name_valid(name: str) -> bool: :rtype: bool """ if not isinstance(name, str): - return False + return False # type: ignore[unreachable] if not name or len(name) > 255: return False @@ -62,7 +62,7 @@ def is_object_path_valid(path: str) -> bool: :rtype: bool """ if not isinstance(path, str): - return False + return False # type: ignore[unreachable] if not path: return False @@ -93,7 +93,7 @@ def is_interface_name_valid(name: str) -> bool: :rtype: bool """ if not isinstance(name, str): - return False + return False # type: ignore[unreachable] if not name or len(name) > 255: return False @@ -124,7 +124,7 @@ def is_member_name_valid(member: str) -> bool: :rtype: bool """ if not isinstance(member, str): - return False + return False # type: ignore[unreachable] if not member or len(member) > 255: return False @@ -180,7 +180,7 @@ def assert_interface_name_valid(name: str) -> None: raise InvalidInterfaceNameError(name) -def assert_member_name_valid(member) -> None: +def assert_member_name_valid(member: str) -> None: """Raise an error if this is not a valid member name. .. seealso:: https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-member From 49795d819c02779d0170284835a58d9ff86b8307 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 9 Oct 2022 09:13:10 -1000 Subject: [PATCH 2/3] feat: add additional typing --- src/dbus_fast/signature.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/dbus_fast/signature.py b/src/dbus_fast/signature.py index f62ffd89..08a37af8 100644 --- a/src/dbus_fast/signature.py +++ b/src/dbus_fast/signature.py @@ -102,7 +102,7 @@ def _parse_next(signature: str) -> Tuple["SignatureType", str]: # basic type return (SignatureType(token), signature[1:]) - def _verify_byte(self, body: int) -> None: + def _verify_byte(self, body: Any) -> None: BYTE_MIN = 0x00 BYTE_MAX = 0xFF if not isinstance(body, int): @@ -114,13 +114,13 @@ def _verify_byte(self, body: int) -> None: f"DBus BYTE type must be between {BYTE_MIN} and {BYTE_MAX}" ) - def _verify_boolean(self, body: bool) -> None: + def _verify_boolean(self, body: Any) -> None: if not isinstance(body, bool): raise SignatureBodyMismatchError( f'DBus BOOLEAN type "b" must be Python type "bool", got {type(body)}' ) - def _verify_int16(self, body: int) -> None: + def _verify_int16(self, body: Any) -> None: INT16_MIN = -0x7FFF - 1 INT16_MAX = 0x7FFF if not isinstance(body, int): @@ -132,7 +132,7 @@ def _verify_int16(self, body: int) -> None: f'DBus INT16 type "n" must be between {INT16_MIN} and {INT16_MAX}' ) - def _verify_uint16(self, body: int) -> None: + def _verify_uint16(self, body: Any) -> None: UINT16_MIN = 0 UINT16_MAX = 0xFFFF if not isinstance(body, int): @@ -156,7 +156,7 @@ def _verify_int32(self, body: int) -> None: f'DBus INT32 type "i" must be between {INT32_MIN} and {INT32_MAX}' ) - def _verify_uint32(self, body: int) -> None: + def _verify_uint32(self, body: Any) -> None: UINT32_MIN = 0 UINT32_MAX = 0xFFFFFFFF if not isinstance(body, int): @@ -168,7 +168,7 @@ def _verify_uint32(self, body: int) -> None: f'DBus UINT32 type "u" must be between {UINT32_MIN} and {UINT32_MAX}' ) - def _verify_int64(self, body: int) -> None: + def _verify_int64(self, body: Any) -> None: INT64_MAX = 9223372036854775807 INT64_MIN = -INT64_MAX - 1 if not isinstance(body, int): @@ -180,7 +180,7 @@ def _verify_int64(self, body: int) -> None: f'DBus INT64 type "x" must be between {INT64_MIN} and {INT64_MAX}' ) - def _verify_uint64(self, body: int) -> None: + def _verify_uint64(self, body: Any) -> None: UINT64_MIN = 0 UINT64_MAX = 18446744073709551615 if not isinstance(body, int): @@ -192,13 +192,13 @@ def _verify_uint64(self, body: int) -> None: f'DBus UINT64 type "t" must be between {UINT64_MIN} and {UINT64_MAX}' ) - def _verify_double(self, body: Union[float, int]) -> None: + def _verify_double(self, body: Any) -> None: if not isinstance(body, (float, int)): raise SignatureBodyMismatchError( f'DBus DOUBLE type "d" must be Python type "float" or "int", got {type(body)}' ) - def _verify_unix_fd(self, body: int) -> None: + def _verify_unix_fd(self, body: Any) -> None: try: self._verify_uint32(body) except SignatureBodyMismatchError: @@ -206,19 +206,19 @@ def _verify_unix_fd(self, body: int) -> None: 'DBus UNIX_FD type "h" must be a valid UINT32' ) - def _verify_object_path(self, body: str) -> None: + def _verify_object_path(self, body: Any) -> None: if not is_object_path_valid(body): raise SignatureBodyMismatchError( 'DBus OBJECT_PATH type "o" must be a valid object path' ) - def _verify_string(self, body: str) -> None: + def _verify_string(self, body: Any) -> None: if not isinstance(body, str): raise SignatureBodyMismatchError( f'DBus STRING type "s" must be Python type "str", got {type(body)}' ) - def _verify_signature(self, body: str) -> None: + def _verify_signature(self, body: Any) -> None: # I guess we could run it through the SignatureTree parser instead if not isinstance(body, str): raise SignatureBodyMismatchError( @@ -254,7 +254,7 @@ def _verify_array(self, body: Any) -> None: for member in body: child_type.verify(member) - def _verify_struct(self, body: Iterable[Any]) -> None: + def _verify_struct(self, body: Any) -> None: # TODO allow tuples if not isinstance(body, list): raise SignatureBodyMismatchError( @@ -269,7 +269,7 @@ def _verify_struct(self, body: Iterable[Any]) -> None: for i, member in enumerate(body): self.children[i].verify(member) - def _verify_variant(self, body: "Variant") -> None: + def _verify_variant(self, body: Any) -> None: # a variant signature and value is valid by construction if not isinstance(body, Variant): raise SignatureBodyMismatchError( From 7760cfe89aaf53fd418fb743d52dde0e1413caae Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 9 Oct 2022 09:14:51 -1000 Subject: [PATCH 3/3] feat: add additional typing --- src/dbus_fast/signature.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dbus_fast/signature.py b/src/dbus_fast/signature.py index 08a37af8..f63f3060 100644 --- a/src/dbus_fast/signature.py +++ b/src/dbus_fast/signature.py @@ -1,5 +1,5 @@ from functools import lru_cache -from typing import Any, Callable, Iterable, List, Optional, Tuple, Union +from typing import Any, Callable, Dict, List, Optional, Tuple, Union from .errors import InvalidSignatureError, SignatureBodyMismatchError from .validators import is_object_path_valid @@ -293,7 +293,7 @@ def verify(self, body: Any) -> bool: return True - validators: dict[str, Callable[["SignatureType", Any], None]] = { + validators: Dict[str, Callable[["SignatureType", Any], None]] = { "y": _verify_byte, "b": _verify_boolean, "n": _verify_int16,