-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request: Raise error on any printf style formatting for direct string interpolation #6796
Comments
I think this is a reasonable change. I'd love to get more opinions on it. |
This likely now relates to #3863/#7769 (fixable groups/fix applicability). You may still want to split it in a different rule to separately configure "fixable w/o type information" vs "unfixable (bad argument count)/risky fix". Idk. In my case I'm looking to refactor out print-f style formatting. And prevent using it in new code across my projects. The following risky fix is acceptable during the refactoring phase (under the understanding it is a risky fix and needs review, but can be fixed later if missed during testing): world = ("World",)
print("Hello %s" % world)
>>> Hello World
print(f"Hello {world}")
>>> Hello ('World',) |
Hey @Avasam the applicability (safety level) is attached to the fix not the rule so we could generate an always applicable fix when type information is around but a sometimes applicable fix when not. Does that address your case? |
As far as the original post goes. I really just want to prevent printf formatting and raise location in CI . Autofix or not. As far as autofixing goes. It'd be a nice to have for me if printf-style strings are updated even if it causes the constructed string to change. My last comment is just because I think those two features are likely related. |
I'd be happy to review a pull request that changes this rule to raise a violation when type information cannot be inferred. We should gate this behavior behind preview mode. We can explore an unsafe fix after. |
Ruff 0.8 now flags all format string usages, see #12461 |
As explained here: #3549 (comment) , Ruff cannot safely autofix all usages of printf-style-formatting (UP031).
I would still like cases like
"%s" % may_be_a_single_element_tuple
to be raised, as they can be manually fixed.I don't mind whether this is done through UP031 or as another rule (could be in FLY, see #2102 (comment)).
The text was updated successfully, but these errors were encountered: