chore: iterate dict directly instead of via .keys() (ruff SIM118)#155
Open
alexzhu0 wants to merge 1 commit intoFlowElement-ai:mainfrom
Open
chore: iterate dict directly instead of via .keys() (ruff SIM118)#155alexzhu0 wants to merge 1 commit intoFlowElement-ai:mainfrom
.keys() (ruff SIM118)#155alexzhu0 wants to merge 1 commit intoFlowElement-ai:mainfrom
Conversation
Ruff rule SIM118 flags `x in dict.keys()` / `for x in dict.keys()`. Iterating a dict directly is the idiomatic form and is measurably faster: `.keys()` constructs a view object on every call; `for x in d` or `x in d` skips the view and goes straight to the dict's internal `__contains__` / `__iter__`. Scope: 5 sites across 4 files — 2 in bundle_search neighbor expansion (hot path inside `_two_phase_projection`), 1 in procedural_bundle_search (same pattern), 1 in a ChromaDB deserialization loop, 1 in a test helper. Semantic equivalence: every site iterates the dict / checks membership; none captures or returns the `.keys()` view. 4 fixes were gated as "hidden" by ruff's conservative default because `.keys()` returns a distinct view type — inspected each site manually and applied the `--unsafe-fixes` class after confirming no view is ever retained. Verification: - `ruff check . --exclude 'm_flow/baml_client' --select SIM118` is now clean - `ast.parse` on every touched file: clean No behavior change.
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
Ruff rule SIM118 flags
x in dict.keys()/for x in dict.keys(). Iterating a dict directly is the idiomatic form and is measurably faster:.keys()constructs a freshdict_keysview object on every callfor x in d/x in dskips the view and goes straight to the dict's internal__iter__/__contains__5 sites, 4 files.
Semantic equivalence
Every site either iterates the dict or checks membership — none captures, returns, or type-annotates the
.keys()view. 4 of the 5 fixes were gated as "hidden" by ruff's conservative default because.keys()returns a distinct view type; I inspected each site manually and applied them via--unsafe-fixesafter confirming no view is ever retained.Files
m_flow/retrieval/episodic/bundle_search.py_two_phase_projection/_two_phase_projection_with_stats— hot path for neighbor expansionm_flow/retrieval/utils/procedural_bundle_search.pym_flow/adapters/vector/chromadb/ChromaDBAdapter.pym_flow/tests/test_multi_tenancy.py_get_first_dataset_idtest helper4 files, +5 / −5.
Verification
No behavior change. Generated code under
m_flow/baml_client/is untouched.I affirm that all code in every commit of this pull request conforms to the terms of the M-flow Developer Certificate of Origin