From 7e4cab6778e8cfb2894e638a7d47eff1cc191bff Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 13 Mar 2025 13:12:13 -1000 Subject: [PATCH] fix: upgrade to ruff 0.1.0 and fix violations (#422) --- .pre-commit-config.yaml | 2 +- build_ext.py | 2 +- examples/glib-list-names.py | 3 +-- pyproject.toml | 11 +++++------ src/dbus_fast/_private/address.py | 8 ++++---- src/dbus_fast/_private/marshaller.py | 4 +++- src/dbus_fast/_private/unmarshaller.py | 8 +++++--- src/dbus_fast/constants.py | 12 +++++++++--- src/dbus_fast/message_bus.py | 3 +-- src/dbus_fast/proxy_object.py | 8 +++++--- src/dbus_fast/service.py | 11 ++++------- src/dbus_fast/signature.py | 4 ++-- tests/benchmarks/test_marshall.py | 3 +-- tests/benchmarks/test_unmarshall.py | 3 +-- tests/client/test_aio.py | 1 - tests/client/test_methods.py | 4 ++-- tests/client/test_properties.py | 2 +- tests/client/test_signals.py | 1 - tests/service/test_export.py | 1 - tests/service/test_methods.py | 1 - tests/service/test_properties.py | 1 - tests/service/test_signals.py | 1 - tests/service/test_standard_interfaces.py | 1 - tests/test_address_parser.py | 1 - tests/test_aio_low_level.py | 1 - tests/test_aio_multi_flags.py | 1 - tests/test_auth.py | 1 - tests/test_big_message.py | 2 +- tests/test_disconnect.py | 1 - tests/test_fd_passing.py | 1 - tests/test_glib_low_level.py | 2 +- tests/test_marshaller.py | 7 +++---- tests/test_request_name.py | 2 +- tests/test_send_reply.py | 1 - tests/test_signature.py | 1 - tests/test_tcp_address.py | 1 - tests/test_unpack_variants.py | 1 - tests/test_validators.py | 18 +++++++++--------- 38 files changed, 61 insertions(+), 75 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f6c9eca..612e038 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -38,7 +38,7 @@ repos: - id: pyupgrade args: [--py39-plus] - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.9.10 + rev: v0.1.0 hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] diff --git a/build_ext.py b/build_ext.py index a62265a..ebfc2d2 100644 --- a/build_ext.py +++ b/build_ext.py @@ -44,7 +44,7 @@ class BuildExt(build_ext): def build(setup_kwargs): - if os.environ.get("SKIP_CYTHON", False): + if os.environ.get("SKIP_CYTHON"): return try: from Cython.Build import cythonize diff --git a/examples/glib-list-names.py b/examples/glib-list-names.py index 649926c..0f2209f 100755 --- a/examples/glib-list-names.py +++ b/examples/glib-list-names.py @@ -7,10 +7,9 @@ sys.path.append(os.path.abspath(os.path.dirname(__file__) + "/..")) import json import signal -from gi.repository import GLib - from dbus_fast import Message from dbus_fast.glib import MessageBus +from gi.repository import GLib main = GLib.MainLoop() bus = MessageBus().connect_sync() diff --git a/pyproject.toml b/pyproject.toml index 57cbb50..ddd3da7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -120,7 +120,6 @@ ignore = [ "SIM109", # intended for cython "SIM102", # intended for cython "E501", # too many to fix, - "TC001", # too many to fix right now "TID252", # intended "PLC0414", # intended "TRY003", # too many to fix @@ -128,7 +127,7 @@ ignore = [ "PLR0911", # api is public "PYI036", # too many to fix "PYI034", # too many to fix - "TC003", # too many to fix + "E721", # many are as intended ## should fix these sooner "B007", # too many to fix -- should fix sooner "SIM103", # too many to fix -- should fix sooner @@ -138,7 +137,6 @@ ignore = [ "B904", # too many to fix -- should fix sooner "PERF401", # too many to fix -- should fix sooner "B904", # too many to fix -- should fix sooner - "PLR1704", # too many to fix -- should fix sooner "B006", # too many to fix -- should fix sooner "G004", # too many to fix -- should fix sooner "PT015", # too many to fix -- should fix sooner @@ -189,7 +187,6 @@ select = [ "SLOT", # flake8-slots "T100", # Trace found: {name} used "T20", # flake8-print - "TC", # flake8-type-checking "TID", # Tidy imports "TRY", # tryceratops ] @@ -227,8 +224,6 @@ select = [ "PLR0913", # skip this one "SIM102" , # too many to fix right now "SIM108", # too many to fix right now - "TC003", # too many to fix right now - "TC002", # too many to fix right now "T201", # too many to fix right now ] "bench/**/*" = [ @@ -238,4 +233,8 @@ select = [ "T201", # intended "RSE102", # too many to fix "C408", # too many to fix + "E402", # as intended +] +"docs/**/*" = [ + "E402", # as intended ] diff --git a/src/dbus_fast/_private/address.py b/src/dbus_fast/_private/address.py index 9c3eb63..7095d8f 100644 --- a/src/dbus_fast/_private/address.py +++ b/src/dbus_fast/_private/address.py @@ -73,10 +73,10 @@ def get_session_bus_address() -> str: display = os.environ["DISPLAY"] try: display = display_re.search(display).group(1) - except Exception: + except Exception as ex: raise InvalidAddressError( f"DBUS_SESSION_BUS_ADDRESS not set and could not parse DISPLAY environment variable to get bus address: {display}" - ) + ) from ex # XXX: this will block but they're very small files and fs operations # should be fairly reliable. fix this by passing in an async func to read @@ -90,10 +90,10 @@ def get_session_bus_address() -> str: try: with open(dbus_info_file_name) as f: dbus_info = f.read().rstrip() - except Exception: + except Exception as ex: raise InvalidAddressError( f"could not open dbus info file: {dbus_info_file_name}" - ) + ) from ex for line in dbus_info.split("\n"): if line.strip().startswith("DBUS_SESSION_BUS_ADDRESS="): diff --git a/src/dbus_fast/_private/marshaller.py b/src/dbus_fast/_private/marshaller.py index 324c28d..f25a472 100644 --- a/src/dbus_fast/_private/marshaller.py +++ b/src/dbus_fast/_private/marshaller.py @@ -186,7 +186,9 @@ class Marshaller: try: return self._construct_buffer() except KeyError as ex: - raise NotImplementedError(f'type is not implemented yet: "{ex.args}"') + raise NotImplementedError( + f'type is not implemented yet: "{ex.args}"' + ) from ex except error: self.signature_tree.verify(self.body) raise RuntimeError("should not reach here") diff --git a/src/dbus_fast/_private/unmarshaller.py b/src/dbus_fast/_private/unmarshaller.py index 005626a..b156c9b 100644 --- a/src/dbus_fast/_private/unmarshaller.py +++ b/src/dbus_fast/_private/unmarshaller.py @@ -738,9 +738,11 @@ class Unmarshaller: self._serial = _ustr_uint32(self._buf_ustr, 8, endian) self._header_len = _ustr_uint32(self._buf_ustr, 12, endian) elif endian == LITTLE_ENDIAN: - self._body_len, self._serial, self._header_len = ( - UNPACK_HEADER_LITTLE_ENDIAN(self._buf, 4) - ) + ( + self._body_len, + self._serial, + self._header_len, + ) = UNPACK_HEADER_LITTLE_ENDIAN(self._buf, 4) self._uint32_unpack = UINT32_UNPACK_LITTLE_ENDIAN self._int16_unpack = INT16_UNPACK_LITTLE_ENDIAN self._uint16_unpack = UINT16_UNPACK_LITTLE_ENDIAN diff --git a/src/dbus_fast/constants.py b/src/dbus_fast/constants.py index ff8691b..29280f6 100644 --- a/src/dbus_fast/constants.py +++ b/src/dbus_fast/constants.py @@ -108,9 +108,15 @@ class ErrorType(str, Enum): :seealso: http://man7.org/linux/man-pages/man3/sd-bus-errors.3.html """ - SERVICE_ERROR = "com.dubstepdish.dbus.next.ServiceError" #: A custom error to indicate an exported service threw an exception. - INTERNAL_ERROR = "com.dubstepdish.dbus.next.InternalError" #: A custom error to indicate something went wrong with the library. - CLIENT_ERROR = "com.dubstepdish.dbus.next.ClientError" #: A custom error to indicate something went wrong with the client. + SERVICE_ERROR = ( + "com.dubstepdish.dbus.next.ServiceError" + ) #: A custom error to indicate an exported service threw an exception. + INTERNAL_ERROR = ( + "com.dubstepdish.dbus.next.InternalError" + ) #: A custom error to indicate something went wrong with the library. + CLIENT_ERROR = ( + "com.dubstepdish.dbus.next.ClientError" + ) #: A custom error to indicate something went wrong with the client. FAILED = "org.freedesktop.DBus.Error.Failed" NO_MEMORY = "org.freedesktop.DBus.Error.NoMemory" diff --git a/src/dbus_fast/message_bus.py b/src/dbus_fast/message_bus.py index ed17ea9..e54728d 100644 --- a/src/dbus_fast/message_bus.py +++ b/src/dbus_fast/message_bus.py @@ -1033,8 +1033,7 @@ class BaseMessageBus: # first build up the result object to know when it's complete result: dict[str, dict[str, Any]] = { - node: {interface: None for interface in self._path_exports[node]} - for node in nodes + node: dict.fromkeys(self._path_exports[node]) for node in nodes } if is_result_complete(): diff --git a/src/dbus_fast/proxy_object.py b/src/dbus_fast/proxy_object.py index 8757984..a1e6750 100644 --- a/src/dbus_fast/proxy_object.py +++ b/src/dbus_fast/proxy_object.py @@ -121,7 +121,7 @@ class BaseProxyInterface: return match = [s for s in self.introspection.signals if s.name == msg.member] - if not len(match): + if not match: return intr_signal = match[0] if intr_signal.signature != msg.signature: @@ -302,8 +302,10 @@ class BaseProxyObject: intr_interface = next( i for i in self.introspection.interfaces if i.name == name ) - except StopIteration: - raise InterfaceNotFoundError(f"interface not found on this object: {name}") + except StopIteration as ex: + raise InterfaceNotFoundError( + f"interface not found on this object: {name}" + ) from ex interface = self.ProxyInterface( self.bus_name, self.path, intr_interface, self.bus diff --git a/src/dbus_fast/service.py b/src/dbus_fast/service.py index 803d3b1..4a1b3cf 100644 --- a/src/dbus_fast/service.py +++ b/src/dbus_fast/service.py @@ -33,7 +33,8 @@ HandlerType = Callable[[Message, SendReply], None] class _MethodCallbackProtocol(Protocol): - def __call__(self, interface: ServiceInterface, *args: Any) -> Any: ... + def __call__(self, interface: ServiceInterface, *args: Any) -> Any: + ... class _Method: @@ -206,7 +207,7 @@ def signal(name: str | None = None, disabled: bool = False) -> Callable: class _Property(property): - def set_options(self, options): + def set_options(self, options: dict[str, Any]) -> None: self.options = getattr(self, "options", {}) for k, v in options.items(): self.options[k] = v @@ -221,11 +222,7 @@ class _Property(property): else: self.access = PropertyAccess.READWRITE - if "disabled" in options: - self.disabled = options["disabled"] - else: - self.disabled = False - + self.disabled = options.get("disabled", False) self.introspection = intr.Property(self.name, self.signature, self.access) self.__dict__["__DBUS_PROPERTY"] = True diff --git a/src/dbus_fast/signature.py b/src/dbus_fast/signature.py index bfc0829..ee264c4 100644 --- a/src/dbus_fast/signature.py +++ b/src/dbus_fast/signature.py @@ -205,10 +205,10 @@ class SignatureType: def _verify_unix_fd(self, body: Any) -> None: try: self._verify_uint32(body) - except SignatureBodyMismatchError: + except SignatureBodyMismatchError as ex: raise SignatureBodyMismatchError( 'DBus UNIX_FD type "h" must be a valid UINT32' - ) + ) from ex def _verify_object_path(self, body: Any) -> None: if not is_object_path_valid(body): diff --git a/tests/benchmarks/test_marshall.py b/tests/benchmarks/test_marshall.py index 3aa6f4c..17d333f 100644 --- a/tests/benchmarks/test_marshall.py +++ b/tests/benchmarks/test_marshall.py @@ -1,6 +1,5 @@ -from pytest_codspeed import BenchmarkFixture - from dbus_fast import Message +from pytest_codspeed import BenchmarkFixture message = Message( destination="org.bluez", diff --git a/tests/benchmarks/test_unmarshall.py b/tests/benchmarks/test_unmarshall.py index 45f785c..7c65e7c 100644 --- a/tests/benchmarks/test_unmarshall.py +++ b/tests/benchmarks/test_unmarshall.py @@ -1,8 +1,7 @@ import io -from pytest_codspeed import BenchmarkFixture - from dbus_fast._private.unmarshaller import Unmarshaller +from pytest_codspeed import BenchmarkFixture def test_unmarshall_bluez_rssi_message(benchmark: BenchmarkFixture) -> None: diff --git a/tests/client/test_aio.py b/tests/client/test_aio.py index 5e4d5e5..49e0d15 100644 --- a/tests/client/test_aio.py +++ b/tests/client/test_aio.py @@ -1,5 +1,4 @@ import pytest - from dbus_fast import aio from dbus_fast.service import ServiceInterface diff --git a/tests/client/test_methods.py b/tests/client/test_methods.py index 23dda4a..7222ea4 100644 --- a/tests/client/test_methods.py +++ b/tests/client/test_methods.py @@ -3,13 +3,13 @@ import sys from logging.handlers import QueueHandler from queue import SimpleQueue -import pytest - import dbus_fast.introspection as intr +import pytest from dbus_fast import DBusError, aio, glib from dbus_fast.message import MessageFlag from dbus_fast.service import ServiceInterface, method from dbus_fast.signature import Variant + from tests.util import check_gi_repository, skip_reason_no_gi has_gi = check_gi_repository() diff --git a/tests/client/test_properties.py b/tests/client/test_properties.py index eab9073..e92efc7 100644 --- a/tests/client/test_properties.py +++ b/tests/client/test_properties.py @@ -1,10 +1,10 @@ import sys import pytest - from dbus_fast import DBusError, Message, aio, glib from dbus_fast.service import PropertyAccess, ServiceInterface, dbus_property from dbus_fast.signature import Variant + from tests.util import check_gi_repository, skip_reason_no_gi has_gi = check_gi_repository() diff --git a/tests/client/test_signals.py b/tests/client/test_signals.py index 65299a2..9f8acbd 100644 --- a/tests/client/test_signals.py +++ b/tests/client/test_signals.py @@ -1,7 +1,6 @@ import asyncio import pytest - from dbus_fast import Message from dbus_fast.aio import MessageBus from dbus_fast.constants import RequestNameReply diff --git a/tests/service/test_export.py b/tests/service/test_export.py index 74bcb4d..232e9a0 100644 --- a/tests/service/test_export.py +++ b/tests/service/test_export.py @@ -1,5 +1,4 @@ import pytest - from dbus_fast import Message, MessageType from dbus_fast import introspection as intr from dbus_fast.aio import MessageBus diff --git a/tests/service/test_methods.py b/tests/service/test_methods.py index 9f7d719..b15c8b9 100644 --- a/tests/service/test_methods.py +++ b/tests/service/test_methods.py @@ -1,5 +1,4 @@ import pytest - from dbus_fast import ( DBusError, ErrorType, diff --git a/tests/service/test_properties.py b/tests/service/test_properties.py index f587cb3..cd826e6 100644 --- a/tests/service/test_properties.py +++ b/tests/service/test_properties.py @@ -1,7 +1,6 @@ import asyncio import pytest - from dbus_fast import ( DBusError, ErrorType, diff --git a/tests/service/test_signals.py b/tests/service/test_signals.py index 8485d24..595ec34 100644 --- a/tests/service/test_signals.py +++ b/tests/service/test_signals.py @@ -1,7 +1,6 @@ import asyncio import pytest - from dbus_fast import Message, MessageType from dbus_fast.aio import MessageBus from dbus_fast.constants import PropertyAccess diff --git a/tests/service/test_standard_interfaces.py b/tests/service/test_standard_interfaces.py index 37e338e..8e5e19a 100644 --- a/tests/service/test_standard_interfaces.py +++ b/tests/service/test_standard_interfaces.py @@ -1,5 +1,4 @@ import pytest - from dbus_fast import Message, MessageType from dbus_fast import introspection as intr from dbus_fast.aio import MessageBus diff --git a/tests/test_address_parser.py b/tests/test_address_parser.py index c162ca2..bf1c7db 100644 --- a/tests/test_address_parser.py +++ b/tests/test_address_parser.py @@ -2,7 +2,6 @@ import os from unittest.mock import patch import pytest - from dbus_fast._private.address import ( get_bus_address, get_session_bus_address, diff --git a/tests/test_aio_low_level.py b/tests/test_aio_low_level.py index 3335202..507211a 100644 --- a/tests/test_aio_low_level.py +++ b/tests/test_aio_low_level.py @@ -1,5 +1,4 @@ import pytest - from dbus_fast import Message, MessageFlag, MessageType from dbus_fast.aio import MessageBus diff --git a/tests/test_aio_multi_flags.py b/tests/test_aio_multi_flags.py index eeaba09..adeea46 100644 --- a/tests/test_aio_multi_flags.py +++ b/tests/test_aio_multi_flags.py @@ -1,5 +1,4 @@ import pytest - from dbus_fast.aio import MessageBus from dbus_fast.service import ServiceInterface, method diff --git a/tests/test_auth.py b/tests/test_auth.py index 81f3a90..676dc96 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -1,7 +1,6 @@ """This tests setting a hardcoded UID in AuthExternal""" import pytest - from dbus_fast.auth import ( UID_NOT_SPECIFIED, AuthAnnonymous, diff --git a/tests/test_big_message.py b/tests/test_big_message.py index 9dea47a..b9639d0 100644 --- a/tests/test_big_message.py +++ b/tests/test_big_message.py @@ -1,9 +1,9 @@ import sys import pytest - from dbus_fast import Message, MessageType, aio, glib from dbus_fast.service import ServiceInterface, method + from tests.util import check_gi_repository, skip_reason_no_gi has_gi = check_gi_repository() diff --git a/tests/test_disconnect.py b/tests/test_disconnect.py index e4bd9bc..5bf32fb 100644 --- a/tests/test_disconnect.py +++ b/tests/test_disconnect.py @@ -2,7 +2,6 @@ import asyncio from unittest.mock import patch import pytest - from dbus_fast import Message from dbus_fast.aio import MessageBus diff --git a/tests/test_fd_passing.py b/tests/test_fd_passing.py index 79f113f..a3ee36f 100644 --- a/tests/test_fd_passing.py +++ b/tests/test_fd_passing.py @@ -3,7 +3,6 @@ import os import pytest - from dbus_fast import Message, MessageType from dbus_fast.aio import MessageBus from dbus_fast.service import ServiceInterface, dbus_property, method, signal diff --git a/tests/test_glib_low_level.py b/tests/test_glib_low_level.py index 4121133..ae230fd 100644 --- a/tests/test_glib_low_level.py +++ b/tests/test_glib_low_level.py @@ -1,9 +1,9 @@ import sys import pytest - from dbus_fast import Message, MessageFlag, MessageType from dbus_fast.glib import MessageBus + from tests.util import check_gi_repository, skip_reason_no_gi has_gi = check_gi_repository() diff --git a/tests/test_marshaller.py b/tests/test_marshaller.py index 7c68866..e1e28ad 100644 --- a/tests/test_marshaller.py +++ b/tests/test_marshaller.py @@ -5,7 +5,6 @@ from enum import Enum from typing import Any import pytest - from dbus_fast import Message, MessageFlag, MessageType, SignatureTree, Variant from dbus_fast._private._cython_compat import FakeCython from dbus_fast._private.constants import BIG_ENDIAN, LITTLE_ENDIAN @@ -234,9 +233,9 @@ def test_unmarshalling_with_table(unmarshall_table): "flags", "serial", ]: - assert getattr(unmarshaller.message, attr) == getattr(message, attr), ( - f"attr doesnt match: {attr}" - ) + assert getattr(unmarshaller.message, attr) == getattr( + message, attr + ), f"attr doesnt match: {attr}" def test_unmarshall_can_resume(): diff --git a/tests/test_request_name.py b/tests/test_request_name.py index 57758f8..16430f0 100644 --- a/tests/test_request_name.py +++ b/tests/test_request_name.py @@ -1,7 +1,6 @@ import sys import pytest - from dbus_fast import ( Message, MessageType, @@ -11,6 +10,7 @@ from dbus_fast import ( aio, glib, ) + from tests.util import check_gi_repository, skip_reason_no_gi has_gi = check_gi_repository() diff --git a/tests/test_send_reply.py b/tests/test_send_reply.py index 616ff8f..6d44123 100644 --- a/tests/test_send_reply.py +++ b/tests/test_send_reply.py @@ -2,7 +2,6 @@ import os from unittest.mock import patch import pytest - from dbus_fast.constants import ErrorType, MessageType from dbus_fast.errors import DBusError from dbus_fast.message import Message diff --git a/tests/test_signature.py b/tests/test_signature.py index 61090a5..f040404 100644 --- a/tests/test_signature.py +++ b/tests/test_signature.py @@ -1,5 +1,4 @@ import pytest - from dbus_fast import SignatureBodyMismatchError, SignatureTree, Variant from dbus_fast._private.util import signature_contains_type diff --git a/tests/test_tcp_address.py b/tests/test_tcp_address.py index 2d7dd83..8e36bf6 100644 --- a/tests/test_tcp_address.py +++ b/tests/test_tcp_address.py @@ -3,7 +3,6 @@ import os from contextlib import suppress import pytest - from dbus_fast import Message from dbus_fast._private.address import parse_address from dbus_fast.aio import MessageBus diff --git a/tests/test_unpack_variants.py b/tests/test_unpack_variants.py index 7f8da7a..8868584 100644 --- a/tests/test_unpack_variants.py +++ b/tests/test_unpack_variants.py @@ -1,7 +1,6 @@ """Test unpack variants.""" import pytest - from dbus_fast.signature import Variant from dbus_fast.unpack import unpack_variants diff --git a/tests/test_validators.py b/tests/test_validators.py index f67cf80..f7bfbbf 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -66,13 +66,13 @@ def test_interface_name_validator(): ] for name in valid_names: - assert is_interface_name_valid(name), ( - f'interface name should be valid: "{name}"' - ) + assert is_interface_name_valid( + name + ), f'interface name should be valid: "{name}"' for name in invalid_names: - assert not is_interface_name_valid(name), ( - f'interface name should be invalid: "{name}"' - ) + assert not is_interface_name_valid( + name + ), f'interface name should be invalid: "{name}"' def test_member_name_validator(): @@ -82,6 +82,6 @@ def test_member_name_validator(): for member in valid_members: assert is_member_name_valid(member), f'member name should be valid: "{member}"' for member in invalid_members: - assert not is_member_name_valid(member), ( - f'member name should be invalid: "{member}"' - ) + assert not is_member_name_valid( + member + ), f'member name should be invalid: "{member}"'