Implement flynt's "join and concat to f-string" transforms
- Dominant language
- Rust
- Stars
- 49.6k
- Forks
- 2.4k
- Avg merge
- 2d 1h
- Merged PRs (30d)
- 445
Description
[`flynt`](https://github.com/ikamensh/flynt#readme) is a specialized linter for f-string usage.
[UP031 and UP032](https://github.com/charliermarsh/ruff/pull/1905) implement `flynt`'s core features, but the two extra transforms
```
-tc, --transform-concats
Replace string concatenations (defined as +
operations involving string literals) with
f-strings. Available only if flynt is
installed with 3.8+ interpreter.
-tj, --transform-joins
Replace static joins (where the joiner is a
string literal and the joinee is a static-
length list) with f-strings. Available only
if flynt is installed with 3.8+ interpreter.
```
i.e.
```diff
a = "Hello"
-msg = a + " World"
+msg = f"{a} World"
-msg2 = "Finally, " + a + " World"
+msg2 = "Finally, {a} World"
```
and
```diff
a = "Hello"
-msg1 = " ".join([a, " World"])
-msg2 = "".join(["Finally, ", a, " World"])
-msg3 = "x".join(("1", "2", "3"))
-msg4 = "x".join({"4", "5", "yee"})
-msg5 = "y".join([1, 2, 3]) # Should be transformed
+msg1 = f"{a} World"
+msg2 = f"Finally, {a} World"
+msg3 = "1x2x3"
+msg4 = "4x5xyee"
+msg5 = f"{1}y{2}y{3}" # Should be transformed
msg6 = a.join(["1", "2", "3"]) # Should not be transformed (not a static joiner)
msg7 = "a".join(a) # Should not be transformed (not a static joinee)
msg8 = "a".join([a, a, *a]) # Should not be transformed (not a static length)
```
respectively could be implemented in Ruff too. (I'd like to work on them! 😄) Should these be `FLY` series, or should they be `RUF`?
* [ ] `FLY001`: consider replacing string concatenation with f-string
* [x] `FLY002`: consider replacing static string joining with f-string (#4196)
Refs https://github.com/charliermarsh/ruff/issues/2097 (relating to f-strings)
Contributor guide
Research direction
Start by reading Ruff's existing UP031 and UP032 implementations, then inspect the FLY002 work referenced as #4196. Determine how a FLY001 rule should recognize string-literal concatenations without changing unsupported cases. Done means the examples gain the requested f-string transformations and the non-static join cases remain unchanged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, rust
- Domain
- devtools, tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100