Fix uid: search to match by prefix instead of substring - #357
Open
harriiinnii wants to merge 2 commits into
Open
Fix uid: search to match by prefix instead of substring#357harriiinnii wants to merge 2 commits into
harriiinnii wants to merge 2 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.
| class AndQuery(Query): | ||
|
|
||
| """A query to combine multiple queries with "and".""" | ||
| """A query to combine multiple queries with \"and\".""" |
Owner
There was a problem hiding this comment.
The backslash is not needed inside triple quotes. Please remove it.
| class OrQuery(Query): | ||
|
|
||
| """A query to combine multiple queries with "or".""" | ||
| """A query to combine multiple queries with \"or\".""" |
| def __init__(self, value: str) -> None: | ||
| super().__init__("uid", value) | ||
|
|
||
| def _match_union(self, value: "str | datetime | list | dict[str, Any]" |
Owner
There was a problem hiding this comment.
Please remove the quotes around the type. It works without them as none of these types has to be lazy loaded. See the other _match_union implementations.
| actual = parse("uid:abc123") | ||
| expected = UidQuery("abc123") | ||
| self.assertEqual(actual, expected) | ||
| self.assertEqual(type(actual), UidQuery) |
Owner
There was a problem hiding this comment.
As you test for equality I think you do not need to test the type again afterwards.
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