Keep the literal type of the AdvertisingData legacy constants - #973
Merged
Conversation
AdvertisingData forwards each AdvertisingData.Type member as a class-level
constant for backward compatibility. Those constants are unannotated, so a type
checker widens them to AdvertisingData.Type, no Literal[Type...] overload of
get() or get_all() matches them, and the return type falls back to the
AdvertisingDataObject union:
ad.get(AdvertisingData.COMPLETE_LOCAL_NAME) -> the whole union
ad.get(AdvertisingData.Type.COMPLETE_LOCAL_NAME) -> str | None
The overloads arrived with the constant block in f321143, so they have never
been reachable through the forwarded names. Every in-tree call through a
forwarded name passes raw=True, which selects on the other parameter, so the
mypy run in CI never reached the broken path.
Declare the backward-compatibility block Final, which keeps the literal type of
each assignment and restores overload selection. pyright treats an all-caps
class variable as implicitly Final and already reports str | None for both
spellings; mypy, ty and zuban widen. The declaration is type-only and changes
nothing at run time.
`invoke project.check-types` runs mypy over tests/, so the assert_type calls
in the new test fail that gate if an overload stops resolving. mypy skips the
body of an unannotated function, hence the -> None.
Collaborator
zxzxwu
approved these changes
Sep 4, 2026
Contributor
Author
Yep, I think that the reason, as I mentioned, is that ALL_CAPS are implicitly considered |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Hi @zxzxwu,
I'm not sure how much you care about the legacy constants, but since they are still there, maybe the typing is still worth fixing. I didn't actually know about the new syntax, and that's why I ran into this issue when typechecking my project:
AdvertisingDataforwards eachAdvertisingData.Typemember as a class-level constant for backward compatibility. Those constants are unannotated, so a type checker widens them toAdvertisingData.Type, noLiteral[Type...]overload ofget()orget_all()matches them, and the return type falls back to the AdvertisingDataObject union:The overloads arrived with the constant block in f321143, so they have never been reachable through the forwarded names. Every in-tree call through a forwarded name passes raw=True, which selects on the other parameter, so the mypy run in CI never reached the broken path.
My patch declares the backward-compatibility block
Final, which keeps the literal type of each assignment and restores overload selection. pyright treats an all-caps class variable as implicitlyFinaland already reportsstr | Nonefor both spellings; mypy, ty and zuban widen.invoke project.check-typesruns mypy overtests/, so theassert_typecalls in the new test fail that gate if an overload stops resolving. mypy skips the body of an unannotated function, hence the-> None.