Skip to content

chore: use explicit conversion flag in f-strings instead of str() (ruff RUF010)#156

Open
alexzhu0 wants to merge 1 commit intoFlowElement-ai:mainfrom
alexzhu0:chore/ruff-ruf010-explicit-conversion
Open

chore: use explicit conversion flag in f-strings instead of str() (ruff RUF010)#156
alexzhu0 wants to merge 1 commit intoFlowElement-ai:mainfrom
alexzhu0:chore/ruff-ruf010-explicit-conversion

Conversation

@alexzhu0
Copy link
Copy Markdown
Contributor

@alexzhu0 alexzhu0 commented May 2, 2026

Summary

Ruff rule RUF010 flags f"{str(x)}" — the str() call is redundant because f-string interpolation already invokes __format__, which for the default format-spec defers to __str__. 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.

 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) and format(x, 's') both call x.__str__(). The !s conversion flag is specified by the format-string grammar (PEP 3101) to do exactly that:

The conversion fields cause a type coercion before formatting. Two conversion flags are currently supported: '!s' which calls str() on the value, and '!r' which calls repr().

For every exception object e, f"{str(e)}" and f"{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.

File Count
m_flow-mcp/src/server.py 7
m_flow/api/v1/manual/manual.py 5
m_flow/memory/episodic/episode_size_check.py 3
m_flow/api/v1/maintenance/routers/get_maintenance_router.py 2
m_flow/cli/commands/memorize_command.py 2
m_flow/tests/unit/interfaces/graph/extract_graph_unit_test.py 2
m_flow/api/v1/memorize/routers/get_memorize_router.py 1
m_flow/retrieval/episodic/memory_fragment.py 1
m_flow/retrieval/utils/fine_grained_triplet_search.py 1
m_flow/retrieval/utils/procedural_memory_fragment.py 1
mflow_workers/workers/memory_node_saving_worker.py 1

11 files, +25 / −25.

Verification

$ ruff check . --exclude 'm_flow/baml_client' --exclude 'm_flow-frontend' --select RUF010
All checks passed!

$ for f in <each touched file>; do uv run python -c "import ast; ast.parse(open('$f').read())"; done
# clean across all 11 files

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

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant