From f73f09a3e08a572bf6acbfd9cae74cf7f03389d5 Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Sun, 23 Aug 2026 22:02:09 +0200 Subject: [PATCH] refac: drop the redundant .keys() from two dict membership tests (#28859) `x in d` and `x in d.keys()` are identical for a plain dict, so the `.keys()` call builds a throwaway view and reads as if it were doing something. Both sites operate on a plain dict: `combined` in `merge_and_sort_query_results` is a local `dict()`, and `ui_settings` comes from `UserSettings.model_dump()` where `ui` is annotated `dict | None` and is already guarded against None on the preceding line. No behaviour change, and no measurable speedup either, so this is a readability cleanup rather than a performance one. Sites where `.keys()` is load-bearing are left alone: the `list(d.keys())` snapshots taken before mutating during iteration, and the places where `.keys()` is the iteration or comprehension source rather than a membership test. --- backend/open_webui/retrieval/utils.py | 2 +- backend/open_webui/routers/users.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/open_webui/retrieval/utils.py b/backend/open_webui/retrieval/utils.py index 371a2313eb..6d7aec94f7 100644 --- a/backend/open_webui/retrieval/utils.py +++ b/backend/open_webui/retrieval/utils.py @@ -641,7 +641,7 @@ def merge_and_sort_query_results(query_results: list[dict], k: int) -> dict: if isinstance(document, str): doc_hash = (metadata or {}).get(CHUNK_HASH_KEY) or _content_hash(document) - if doc_hash not in combined.keys(): + if doc_hash not in combined: combined[doc_hash] = (distance, document, metadata) continue # if doc is new, no further comparison is needed diff --git a/backend/open_webui/routers/users.py b/backend/open_webui/routers/users.py index 2078ebc0c4..d736c038bc 100644 --- a/backend/open_webui/routers/users.py +++ b/backend/open_webui/routers/users.py @@ -515,7 +515,7 @@ async def update_user_settings_by_session_user( if ( user.role != 'admin' and ui_settings is not None - and 'toolServers' in ui_settings.keys() + and 'toolServers' in ui_settings and not await has_permission( user.id, 'features.direct_tool_servers',