ruff icon indicating copy to clipboard operation
ruff copied to clipboard

Implement `flake8-use-fstring`

Open Secrus opened this issue 2 years ago • 3 comments

https://github.com/MichaelKim0407/flake8-use-fstring

  • FS001: % formatting is used.

  • FS002: .format formatting is used.

  • FS003: f-string missing prefix (ignored by default).

Config options: --percent-greedy, --format-greedy

  • Level 0 (default): only report error if the value before % or .format is a string literal.

  • Level 1: report error if a string literal appears before % or .format anywhere in the statement.

  • Level 2: report any usage of % or .format.

Secrus avatar Jan 22 '23 22:01 Secrus

I believe we support FS001 and FS002, at least partially, as UP031 and UP032, which also automatically upgrade % strings to .format strings, and .format strings to f-strings.

(I say "partially", because it doesn't flag every usage of % and .format strings -- it omits some cases depending on formatting.)

charliermarsh avatar Jan 23 '23 00:01 charliermarsh

We could change UP031 and UP032 to always flag those string formatting methods, even if they can't fix them. That'd be one way to close out this issue. But it's a bit of a behavior change for those rules.

charliermarsh avatar Jan 23 '23 01:01 charliermarsh

Let me test how it works right now, looking at the rules description, it might actually be enough.

Secrus avatar Jan 25 '23 20:01 Secrus

@charliermarsh Sorry for necro'ing a closed issue - just want to mention that in my recent usage, UP032 did indeed left some .format()s, which are a bit more complex then just already assigned variable(s).
I'm thinking an opt-in option in settings for it to flag them, even though auto-fix is not available could be nice?
Might include comment, like "consider adding helper variable for complex formatting before using it in f-string"

martinhoyer avatar Apr 24 '23 14:04 martinhoyer