Skip to content

backends: make timeout and disconnected_callback explicit kwargs#1994

Open
JPHutchins wants to merge 1 commit into
hbldh:developfrom
JPHutchins:pairing-explicit-kwargs
Open

backends: make timeout and disconnected_callback explicit kwargs#1994
JPHutchins wants to merge 1 commit into
hbldh:developfrom
JPHutchins:pairing-explicit-kwargs

Conversation

@JPHutchins
Copy link
Copy Markdown
Contributor

Split out of #1990.

What

Makes the previously-implicit timeout / disconnected_callback contract explicit on the backend clients instead of threading them through **kwargs:

  • BaseBleakClient.__init__ takes timeout and disconnected_callback as explicit keyword-only parameters.
  • The WinRT, BlueZ, CoreBluetooth, and p4android backends accept and forward them explicitly.
  • Corrects the disconnected_callback docstring: the backend-level callable takes no arguments (signature is Callable[[], None]), matching how backends invoke it — the previous "must take one argument (the client)" text described the public facade, not this interface.

This makes the constructor signatures self-documenting and is the groundwork for adding pairing_callbacks as a sibling explicit parameter in a follow-up.

Note for review

This also drops the branches: [master, develop] filter from the pull_request: trigger in build_and_test.yml and format_and_lint.yml so CI runs on every PR (it was needed to run CI on the stacked split PRs). If you'd rather keep that filter on upstream, I'm happy to pull those two lines back out of this PR.

Testing

  • mypy + pyright (strict) clean, native and Linux-simulated.
  • No behavior change; existing connect/disconnect coverage exercises the forwarded parameters.

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings June 1, 2026 20:17
@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 1, 2026

Codecov Report

❌ Patch coverage is 80.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 52.45%. Comparing base (ae2f589) to head (4560596).

Files with missing lines Patch % Lines
bleak/backends/p4android/client.py 0.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1994      +/-   ##
===========================================
- Coverage    52.45%   52.45%   -0.01%     
===========================================
  Files           43       43              
  Lines         4097     4099       +2     
  Branches       504      504              
===========================================
+ Hits          2149     2150       +1     
- Misses        1817     1818       +1     
  Partials       131      131              
Flag Coverage Δ
bluez-integration-py310 39.13% <50.00%> (-0.02%) ⬇️
bluez-integration-py311 39.13% <50.00%> (-0.02%) ⬇️
bluez-integration-py312 39.13% <50.00%> (-0.02%) ⬇️
bluez-integration-py313 39.13% <50.00%> (-0.02%) ⬇️
bluez-integration-py314 37.62% <50.00%> (-0.02%) ⬇️
macos-latest-py310 20.05% <60.00%> (+0.01%) ⬆️
macos-latest-py311 20.05% <60.00%> (+0.01%) ⬆️
macos-latest-py312 20.05% <60.00%> (+0.01%) ⬆️
macos-latest-py313 20.05% <60.00%> (+0.01%) ⬆️
macos-latest-py314 19.86% <60.00%> (+0.01%) ⬆️
ubuntu-latest-py310 23.93% <50.00%> (-0.02%) ⬇️
ubuntu-latest-py311 23.93% <50.00%> (-0.02%) ⬇️
ubuntu-latest-py312 23.93% <50.00%> (-0.02%) ⬇️
ubuntu-latest-py313 23.93% <50.00%> (-0.02%) ⬇️
ubuntu-latest-py314 22.04% <50.00%> (-0.02%) ⬇️
windows-latest-py310 18.71% <50.00%> (-0.01%) ⬇️
windows-latest-py311 18.71% <50.00%> (-0.01%) ⬇️
windows-latest-py312 18.71% <50.00%> (-0.01%) ⬇️
windows-latest-py313 18.71% <50.00%> (-0.01%) ⬇️
windows-latest-py314 18.43% <50.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR refactors backend client constructors to pass timeout and disconnected_callback explicitly into BaseBleakClient, and loosens GitHub Actions PR trigger branch filtering.

Changes:

  • Update BaseBleakClient.__init__ to take keyword-only timeout and disconnected_callback parameters instead of pulling them from **kwargs.
  • Update multiple backend client constructors to accept/pass through the new parameters.
  • Remove pull_request.branches restrictions in CI workflows so PR workflows trigger for all target branches.

Reviewed changes

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

Show a summary per file
File Description
bleak/backends/client.py Makes timeout and disconnected_callback explicit keyword-only args and updates callback contract docs.
bleak/backends/winrt/client.py Updates WinRT backend constructor to accept/pass through timeout and disconnected_callback.
bleak/backends/bluezdbus/client.py Updates BlueZ D-Bus backend constructor to accept/pass through timeout and disconnected_callback.
bleak/backends/corebluetooth/client.py Updates CoreBluetooth backend constructor to accept/pass through timeout and disconnected_callback; adds Callable import.
bleak/backends/p4android/client.py Updates Android backend constructor to accept/pass through timeout and disconnected_callback; adds Callable import.
.github/workflows/format_and_lint.yml Removes PR branch filtering for format/lint workflow triggers.
.github/workflows/build_and_test.yml Removes PR branch filtering for build/test workflow triggers.
Comments suppressed due to low confidence (1)

bleak/backends/client.py:1

  • Given the callback’s argument contract has changed, it would be good to add/adjust a unit test that exercises a disconnect path and asserts the callback invocation matches the documented signature. This helps catch regressions where a backend (or shared code) still calls the callback with the client argument.
# Created on 2018-04-23 by hbldh <henrik.blidh@nedomkull.com>

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

Comment thread bleak/backends/client.py
Comment on lines 60 to +72
self,
address_or_ble_device: Union[BLEDevice, str],
services: Optional[set[str]] = None,
*,
timeout: float,
disconnected_callback: Callable[[], None] | None = None,
**kwargs: Any,
):
super().__init__(address_or_ble_device, **kwargs)
super().__init__(
address_or_ble_device,
timeout=timeout,
disconnected_callback=disconnected_callback,
)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Extra keyword arguments were already discarded before this PR: BaseBleakClient.__init__ itself takes **kwargs: Any and uses nothing beyond its named parameters, so forwarding them from the backend would not change the outcome. This PR's scope is just promoting timeout/disconnected_callback out of **kwargs; tightening the remaining catch-all is a separate cleanup.

Comment on lines 7 to 8
pull_request:
branches: [master, develop]
paths: ["bleak/**", "tests/**", ".github/workflows/build_and_test.yml", "pyproject.toml"]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Generally there's no reason to gate on open source, depending on volume of CI runs.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is an unrelated change so doesn't belong in this PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed

Comment on lines 7 to 8
pull_request:
branches: [master, develop]
paths: ["bleak/**", "tests/**", ".github/workflows/build_and_test.yml", "pyproject.toml"]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is an unrelated change so doesn't belong in this PR.

push:
branches: [master, develop]
pull_request:
branches: [master, develop]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ditto

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed

Comment thread bleak/backends/client.py Outdated
address_or_ble_device: BLEDevice | str,
*,
timeout: float,
disconnected_callback: Callable[[], None] | None = None,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
disconnected_callback: Callable[[], None] | None = None,
disconnected_callback: Callable[[], None] | None,

Not much use in giving a default since we should always be passing it from the subclass's __init__ (in fact it could hide a bug if a new backend forgot to pass it along).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed

Comment thread bleak/backends/client.py
Comment thread bleak/backends/bluezdbus/client.py Outdated
Comment on lines +70 to +71
timeout: float,
disconnected_callback: Optional[Callable[[], None]] = None,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's put these in the same order as bleak.BleakClient to keep things consistent.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, it is always timeout, disconnected_callback, unless I'm missing one someplace!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Here these are after bluez when they should be before.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ah, looks like it is fixed.

Replace the kwargs["timeout"] / kwargs.get("disconnected_callback") access
in BaseBleakClient with explicit keyword-only parameters and forward them
explicitly from each backend __init__ instead of threading **kwargs through.

disconnected_callback is required (no default) on BaseBleakClient so that a
backend which forgets to forward it fails loudly rather than silently
passing None. The backend constructors list the new parameters in the same
order as bleak.BleakClient and annotate disconnected_callback consistently.

No behavior change for the built-in backends or the public BleakClient,
which always pass timeout and disconnected_callback explicitly.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@JPHutchins JPHutchins force-pushed the pairing-explicit-kwargs branch from ce44cda to 4560596 Compare June 3, 2026 01:04
address_or_ble_device: Union[BLEDevice, str],
services: Optional[set[str]] = None,
*,
disconnected_callback: Callable[[], None] | None = None,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
disconnected_callback: Callable[[], None] | None = None,
disconnected_callback: Callable[[], None] | None,

address_or_ble_device: Union[BLEDevice, str],
services: Optional[set[str]] = None,
*,
disconnected_callback: Callable[[], None] | None = None,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
disconnected_callback: Callable[[], None] | None = None,
disconnected_callback: Callable[[], None] | None,

services: Optional[set[uuid.UUID]],
**kwargs,
*,
disconnected_callback: Callable[[], None] | None = None,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
disconnected_callback: Callable[[], None] | None = None,
disconnected_callback: Callable[[], None] | None,

Comment on lines +152 to +153
disconnected_callback: Callable[[], None] | None = None,
timeout: float,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
disconnected_callback: Callable[[], None] | None = None,
timeout: float,
timeout: float,
disconnected_callback: Callable[[], None] | None,

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