chore: use explicit conversion flag in f-strings instead of str() (ruff RUF010)#156
Open
alexzhu0 wants to merge 1 commit intoFlowElement-ai:mainfrom
Open
chore: use explicit conversion flag in f-strings instead of str() (ruff RUF010)#156alexzhu0 wants to merge 1 commit intoFlowElement-ai:mainfrom
str() (ruff RUF010)#156alexzhu0 wants to merge 1 commit intoFlowElement-ai:mainfrom
Conversation
…ruff RUF010)
Ruff rule RUF010 flags `f"{str(x)}"` — the `str()` call is redundant
because f-string interpolation already invokes `__format__` (which
defers to `__str__` in the default case). Using the explicit conversion
flag `!s` makes the intent clear at the call site and drops one
function call from the hot path of every format.
`f"{str(e)}"` → `f"{e!s}"`
Scope: 26 sites across 11 files. Almost all are error-handling paths
(`except Exception as e: ... f"failed: {str(e)}"`) in the MCP server,
API routers, CLI commands, memorize/retrieval utilities, and one test.
Semantics are identical: `str(e)` and `format(e, 's')` both call
`e.__str__()` on an `Exception` instance (the formatter short-circuits
the format-spec branch when the flag is `s`).
Verification:
- `ruff check . --exclude 'm_flow/baml_client' --select RUF010` 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 RUF010 flags
f"{str(x)}"— thestr()call is redundant because f-string interpolation already invokes__format__, which for the default format-spec defers to__str__. The explicit conversion flag!smakes the intent clear at the call site and drops one function call from the hot path of every format.except Exception as e: _log.error("搜索失败: %s", e) - return [types.TextContent(type="text", text=f"❌ 搜索失败: {str(e)}")] + return [types.TextContent(type="text", text=f"❌ 搜索失败: {e!s}")]Semantic equivalence
str(x)andformat(x, 's')both callx.__str__(). The!sconversion flag is specified by the format-string grammar (PEP 3101) to do exactly that:For every exception object
e,f"{str(e)}"andf"{e!s}"produce byte-for-byte identical output.Scope
26 sites across 11 files. Almost all are error-handling paths — the common
except Exception as e: ... f"failed: {str(e)}"pattern.m_flow-mcp/src/server.pym_flow/api/v1/manual/manual.pym_flow/memory/episodic/episode_size_check.pym_flow/api/v1/maintenance/routers/get_maintenance_router.pym_flow/cli/commands/memorize_command.pym_flow/tests/unit/interfaces/graph/extract_graph_unit_test.pym_flow/api/v1/memorize/routers/get_memorize_router.pym_flow/retrieval/episodic/memory_fragment.pym_flow/retrieval/utils/fine_grained_triplet_search.pym_flow/retrieval/utils/procedural_memory_fragment.pymflow_workers/workers/memory_node_saving_worker.py11 files, +25 / −25.
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