Skip to content

refactor: drop redundant Windows guard on dbus re-exports#273

Draft
bluetoothbot wants to merge 2 commits into
Bluetooth-Devices:mainfrom
bluetoothbot:koan/dbus-init-windows-import-consistency
Draft

refactor: drop redundant Windows guard on dbus re-exports#273
bluetoothbot wants to merge 2 commits into
Bluetooth-Devices:mainfrom
bluetoothbot:koan/dbus-init-windows-import-consistency

Conversation

@bluetoothbot

@bluetoothbot bluetoothbot commented May 26, 2026

Copy link
Copy Markdown
Contributor

What

Drop the if system() != "Windows": guard around the .dbus re-exports in bluetooth_adapters/__init__.py. Always import BlueZDBusObjects, get_bluetooth_adapter_details, get_bluetooth_adapters, get_dbus_managed_objects at the top level.

Why

The guard was both inconsistent and redundant:

  • Inconsistent with macOSdbus-fast is platform_system == "Linux" in pyproject.toml, so macOS also lacks it at runtime. But the macOS path imported the names anyway, so non-Linux platforms had asymmetric contracts for no functional reason.
  • Redundantdbus.py already wraps the dbus_fast import in try/except ImportError, setting MessageBus / BusType / AuthError / Message / MessageType / unpack_variants to None when absent. The module imports cleanly on any platform; only calls would fail when dbus-fast isn't installed.

The result was that __all__ listed those names unconditionally, so from bluetooth_adapters import * (or any direct attribute access) on Windows raised NameError / AttributeError even though the module itself loaded fine.

How

  • __init__.py: remove the platform.system() check and the now-unused from platform import system import; move .dbus re-exports next to the other unconditional imports.
  • tests/test_init.py: drop the mirroring if system() != "Windows": guard and the now-unused system import. The per-test skipif(MessageType is None ...) decorators already handle dbus-fast-missing runs.

Testing

  • poetry run pytest tests/ — 51 passed, coverage still 98%.
  • poetry run ruff check src/ tests/ — clean.
  • CI matrix covers macOS + Windows on 3.10–3.14, which is the actual exposure surface.

Quality Report

Changes: 2 files changed, 9 insertions(+), 23 deletions(-)

Code scan: clean

Tests: passed (1
PASSED)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

@codecov

codecov Bot commented May 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.24%. Comparing base (ece711c) to head (1467941).

Files with missing lines Patch % Lines
src/bluetooth_adapters/dbus.py 71.42% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #273      +/-   ##
==========================================
- Coverage   99.61%   99.24%   -0.38%     
==========================================
  Files          14       14              
  Lines         522      527       +5     
  Branches       48       48              
==========================================
+ Hits          520      523       +3     
- Misses          0        1       +1     
- Partials        2        3       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR removes the Windows-only conditional around DBus-related re-exports so that bluetooth_adapters exposes a consistent top-level API across platforms, aligning behavior with the existing “import succeeds; usage depends on optional deps” pattern in dbus.py.

Changes:

  • Unconditionally re-export DBus helpers (BlueZDBusObjects, get_bluetooth_adapter_details, get_bluetooth_adapters, get_dbus_managed_objects) from bluetooth_adapters.__init__.
  • Simplify tests/test_init.py by removing the mirrored Windows guard and importing the DBus helpers unconditionally.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/bluetooth_adapters/__init__.py Removes the Windows platform gate and always re-exports DBus helpers at package top-level.
tests/test_init.py Removes Windows-specific conditional imports and relies on existing skip markers for dbus-fast availability.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +12 to +17
from .dbus import (
BlueZDBusObjects,
get_bluetooth_adapter_details,
get_bluetooth_adapters,
get_dbus_managed_objects,
)
@bdraco

bdraco commented May 27, 2026

Copy link
Copy Markdown
Member

@bluetoothbot rr

@bluetoothbot

Copy link
Copy Markdown
Contributor Author

PR Review — refactor: drop redundant Windows guard on dbus re-exports

Clean, minimal refactor that does exactly what the description says: drops an asymmetric Windows-only guard so the package exposes the same top-level surface on all platforms, matching the existing macOS contract. The dbus.py module already has its own dbus_fast ImportError fallback (setting MessageBus/BusType/etc. to None), so the package import remains safe on non-Linux; only calls fail when the optional dep is missing, which is unchanged. Test changes mirror the source change correctly — the dropped conditional is replaced by alphabetically-correct unconditional imports, and the per-test skipif(MessageType is None ...) decorators continue to gate dbus-fast-requiring assertions. Coverage holds at 99.42%. Copilot's note about non-actionable errors when calling these helpers without dbus_fast is valid but pre-existing (macOS already had this contract) and out of scope for a guard-removal refactor.



Checklist

  • Import surface is consistent across platforms
  • Tests updated to match unconditional import
  • No new untested branches introduced
  • No regressions in all / public API
  • No hardcoded secrets / unsafe ops

Summary

Clean, minimal refactor that does exactly what the description says: drops an asymmetric Windows-only guard so the package exposes the same top-level surface on all platforms, matching the existing macOS contract. The dbus.py module already has its own dbus_fast ImportError fallback (setting MessageBus/BusType/etc. to None), so the package import remains safe on non-Linux; only calls fail when the optional dep is missing, which is unchanged. Test changes mirror the source change correctly — the dropped conditional is replaced by alphabetically-correct unconditional imports, and the per-test skipif(MessageType is None ...) decorators continue to gate dbus-fast-requiring assertions. Coverage holds at 99.42%. Copilot's note about non-actionable errors when calling these helpers without dbus_fast is valid but pre-existing (macOS already had this contract) and out of scope for a guard-removal refactor.


Automated review by Kōane2e18a2

`__init__.py` previously gated the `.dbus` re-exports behind
`if system() != "Windows":`. This had two problems:

- Inconsistent with macOS — `dbus-fast` is also unavailable there
  (poetry marker is `platform_system == "Linux"`), but the macOS path
  imported the names fine, so the contract differed between non-Linux
  platforms with no functional reason.
- Redundant — `dbus.py` already has its own `try/except ImportError`
  on `dbus_fast` that sets `MessageBus`/`BusType`/`Message`/etc. to
  `None`. The module imports cleanly on any platform; only calls
  would fail when `dbus-fast` is missing.

`__all__` already listed `BlueZDBusObjects`, `get_bluetooth_adapters`,
`get_bluetooth_adapter_details`, `get_dbus_managed_objects` unconditionally,
so `from bluetooth_adapters import *` raised NameError on Windows.
The fix aligns runtime exports with `__all__` on all platforms.

`tests/test_init.py` had the same Windows guard for symmetry — also
dropped; the per-test `skipif(MessageType is None ...)` guards already
skip dbus-fast-dependent tests on non-Linux runners.
@bluetoothbot

Copy link
Copy Markdown
Contributor Author

Rebase with requested adjustments

Branch koan/dbus-init-windows-import-consistency was rebased onto main and review feedback was applied.

Changes applied

  • Changes done.
  • Summary

  • Addressed Copilot review comment on src/bluetooth_adapters/__init__.py:17 requesting actionable errors instead of NoneType failures when dbus_fast is unavailable.
  • src/bluetooth_adapters/dbus.py: Added module-level _ensure_dbus_fast() guard that raises RuntimeError with a clear, actionable message ("dbus_fast is required ... but is not installed (it is Linux-only)") when MessageBus is None.
  • Invoked guard at entry of public surface: BlueZDBusObjects.__init__, get_bluetooth_adapters(), get_bluetooth_adapter_details(), get_dbus_managed_objects(). Windows/macOS users now get a descriptive RuntimeError rather than a low-signal TypeError from None(...) calls, per reviewer request.

Stats

3 files changed, 23 insertions(+), 23 deletions(-)
Actions performed
  • Already-solved check: negative (confidence=high, reasoning=Windows guard still present at line 15 of init.py on main; PR refactor not yet applied.)
  • Rebased koan/dbus-init-windows-import-consistency onto upstream/main
  • Applied review feedback
  • Pre-push CI check: previous run passed
  • Force-pushed koan/dbus-init-windows-import-consistency to origin
  • CI check enqueued in ## CI (async)

CI status

CI will be checked asynchronously.


Automated by Kōan

@bluetoothbot bluetoothbot force-pushed the koan/dbus-init-windows-import-consistency branch from e2e18a2 to 1467941 Compare May 27, 2026 03:50
@bdraco bdraco requested a review from Copilot May 27, 2026 03:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment on lines +31 to +38
def _ensure_dbus_fast() -> None:
"""Raise a clear error if dbus_fast is not available on this platform."""
if MessageBus is None:
raise RuntimeError(
"dbus_fast is required for DBus-based bluetooth adapter access "
"but is not installed (it is Linux-only). Install bluetooth-adapters "
"on a Linux host, or avoid calling DBus helpers on this platform."
)
@@ -98,6 +111,7 @@ async def get_bluetooth_adapter_details() -> dict[str, dict[str, Any]]:

async def get_dbus_managed_objects() -> dict[str, Any]:
"""Return a list of bluetooth adapter with details."""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants