Fix uid: search to match by prefix instead of substring - #357
Open
harriiinnii wants to merge 3 commits into
Open
Fix uid: search to match by prefix instead of substring#357harriiinnii wants to merge 3 commits into
harriiinnii wants to merge 3 commits into
Conversation
khard list displays a short unique UID prefix for each contact, but uid:xxx searched by substring so uid:a would match both "aaabbbb" and "bbbaaaa". Introduce UidQuery which uses startswith so the displayed prefix can be used directly to select a contact unambiguously. Fixes lucc#327
UidQuery is a subclass of FieldQuery so isinstance always returns True; use type() equality to assert that parse() returns a UidQuery specifically.
lucc
requested changes
Aug 17, 2026
lucc
left a comment
Owner
There was a problem hiding this comment.
Looks good, I just have some nitpicks.
- Remove unnecessary backslash escapes from AndQuery/OrQuery docstrings - Remove forward-reference quotes from UidQuery._match_union type hint (str | datetime | list | dict[str, Any] works unquoted on Python 3.10+, consistent with the rest of the file) - Drop redundant type assertion in test_uid_field_creates_uid_query_instance; assertEqual(actual, expected) already verifies both value and type via UidQuery.__eq__
lucc
requested changes
Aug 28, 2026
lucc
left a comment
Owner
There was a problem hiding this comment.
I activated the ci pipeline so you can see that your changes break some other tests. Please run all test locally to speed up the feedback cycle.
The breakage is not severe, its just that some uid queries are being compared and their type now obviously changed. Please update the breaking tests as well.
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.
Summary
khard listdisplays a short unique UID prefix for each contact (e.g.afor a contact whose UID starts withaand is the only one with that prefix). However,uid:amatched by substring, so it would match bothaaabbbbandbbbaaaainstead of onlyaaabbbb. This made the displayed prefix unusable as a selector.Problem
parse("uid:xxx")returned a plainFieldQuery("uid", "xxx"), which inheritsTermQuery.matchand doesterm in value.lower()— a substring check. The displayed prefix is a prefix, not a substring, so the two behaviours are inconsistent.Solution
Add a
UidQuerysubclass ofFieldQuerythat overrides_match_unionto usestr.startswithfor string values. Updateparse()to return aUidQuerywhen the field is"uid".The change is intentionally minimal: only the UID field is affected, and the rest of the query machinery (
AndQuery,OrQuery, equality, hashing,__str__) works as before.Testing
TestUidQueryintest/test_query.pycovering: prefix match, substring-only non-match, exact match, case insensitivity, empty term with and without uid set, and disambiguation of two contacts sharing a common prefix.test_uid_field_creates_uid_query_instancetoTestParserverifying thatparse("uid:xxx")returns aUidQuery.Fixes #327