Skip to content

Make sdp.Server.Service a type alias - #974

Merged
zxzxwu merged 1 commit into
google:mainfrom
zyv:sdp-service-alias
Sep 4, 2026
Merged

Make sdp.Server.Service a type alias#974
zxzxwu merged 1 commit into
google:mainfrom
zyv:sdp-service-alias

Conversation

@zyv

@zyv zyv commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

In my project, I must construct service records by hand and hit the following typing error in the process:

device.sdp_service_records = {
    handle: rfcomm.make_service_sdp_records(handle, channel, core.UUID(uuid))
    for uuid, handle in _SDP_SERVICE_HANDLES.items()
}
classic.py:536: error: Value expression in dictionary comprehension has incompatible type
"list[ServiceAttribute]"; expected type "Service"  [misc]

sdp.Server.Service is a NewType over list[ServiceAttribute], and a NewType is satisfied only by an explicit constructor call. Device.sdp_service_records is unannotated for that reason: annotating it with the NewType in-place causes mypy errors:

apps/rfcomm_bridge.py:112: error: Dict entry 0 has incompatible type
"int": "list[ServiceAttribute]"; expected "int": "Service"

I suggest to fix it as follows as the alternative to keep the nominal type and change every producer to return Server.Service breaks every downstream user who assigns a list they built themselves:

Make Service a type alias and give Device.sdp_service_records the annotation the NewType ruled out: sdp_records() helpers are already annotated with dict[int, list[ServiceAttribute]] by hand.

sdp.Server.Service is a NewType over list[ServiceAttribute], and a NewType is
satisfied only by an explicit constructor call. Device.sdp_service_records is
unannotated for that reason: annotating it with the NewType in place causes
mypy errors:

    apps/rfcomm_bridge.py:112: error: Dict entry 0 has incompatible type
    "int": "list[ServiceAttribute]"; expected "int": "Service"

Make Service a type alias and give Device.sdp_service_records the annotation
the NewType ruled out: sdp_records() helpers are already annotated with
dict[int, list[ServiceAttribute]] by hand.
@zxzxwu
zxzxwu merged commit 2e61973 into google:main Sep 4, 2026
36 checks passed
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.

2 participants