[ruff] Ignore str() when not used for simple conversion (RUF065)
#21330
+32
−2
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
Fixed RUF065 (
logging-eager-conversion) to only flagstr()calls when they perform a simple conversion that can be safely removed. The rule now ignoresstr()calls with no arguments, multiple arguments, starred arguments, or keyword unpacking, preventing false positives.Fixes #21315
Problem Analysis
The RUF065 rule was incorrectly flagging all
str()calls in logging statements, even whenstr()was performing actual conversion work beyond simple type coercion. Specifically, the rule flagged:str()with no arguments - which returns an empty stringstr(b"data", "utf-8")with multiple arguments - which performs encoding conversionstr(*args)with starred arguments - which unpacks argumentsstr(**kwargs)with keyword unpacking - which passes keyword argumentsThese cases cannot be safely removed because
str()is doing meaningful work (encoding conversion, argument unpacking, etc.), not just redundant type conversion.The root cause was that the rule only checked if the function was
str()without validating the call signature. It didn't distinguish between simplestr(value)conversions (which can be removed) and more complexstr()calls that perform actual work.Approach
The fix adds validation to the
str()detection logic inlogging_eager_conversion.rs:str()calls with exactly one positional argument (str_call_args.args.len() == 1)!str_call_args.args[0].is_starred_expr())str_call_args.keywords.is_empty())This ensures the rule only flags cases like
str(value)wherestr()is truly redundant and can be removed, while ignoring cases wherestr()performs actual conversion work.The fix maintains backward compatibility - all existing valid test cases continue to be flagged correctly, while the new edge cases are properly ignored.