Skip to content

Commit 64ba39a

Browse files
VascoSch92dylwil3
andauthored
[flynt] add fix safety section (FLY002) (#17496)
The PR add the fix safety section for rule `FLY002` (#15584 ) The motivation for the content of the fix safety section is given by the following example ```python foo = 1 bar = [2, 3] try: result_join = " ".join((foo, bar)) print(f"Join result: {result_join}") except TypeError as e: print(f"Join error: {e}") ``` which print `Join error: sequence item 0: expected str instance, int found` But after the fix is applied, we have ```python foo = 1 bar = [2, 3] try: result_join = f"{foo} {bar}" print(f"Join result: {result_join}") except TypeError as e: print(f"Join error: {e}") ``` which print `Join result: 1 [2, 3]` --------- Co-authored-by: dylwil3 <dylwil3@gmail.com>
1 parent a4e225e commit 64ba39a

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

crates/ruff_linter/src/rules/flynt/rules/static_join_to_fstring.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ use crate::rules::flynt::helpers;
2828
/// f"{foo} {bar}"
2929
/// ```
3030
///
31+
/// # Fix safety
32+
/// The fix is always marked unsafe because the evaluation of the f-string
33+
/// expressions will default to calling the `__format__` method of each
34+
/// object, whereas `str.join` expects each object to be an instance of
35+
/// `str` and uses the corresponding string. Therefore it is possible for
36+
/// the values of the resulting strings to differ, or for one expression
37+
/// to raise an exception while the other does not.
38+
///
3139
/// ## References
3240
/// - [Python documentation: f-strings](https://docs.python.org/3/reference/lexical_analysis.html#f-strings)
3341
#[derive(ViolationMetadata)]

0 commit comments

Comments
 (0)