Skip to content
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

[pyupgrade] Show violations without auto-fix for UP031 #11229

Merged
merged 6 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyupgrade/UP031_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,33 @@

print("%2X" % 1)
print("%02X" % 1)

# UP031 (no longer false negatives, but offer no fix because of more complex syntax)

"%d.%d" % (a, b)

"%*s" % (5, "hi")

"%d" % (flt,)

"%c" % (some_string,)

"%.2r" % (1.25)

"%.*s" % (5, "hi")

"%i" % (flt,)

"%()s" % {"": "empty"}

"%s" % {"k": "v"}

"%()s" % {"": "bar"}

"%(1)s" % {"1": "bar"}

"%(a)s" % {"a": 1, "a": 2}

"%(1)s" % {1: 2, "1": 2}

"%(and)s" % {"and": 2}
26 changes: 0 additions & 26 deletions crates/ruff_linter/resources/test/fixtures/pyupgrade/UP031_1.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
# OK
b"%s" % (b"bytestring",)

"%*s" % (5, "hi")

"%d" % (flt,)

"%c" % (some_string,)

"%4%" % ()

"%.2r" % (1.25)

i % 3

"%.*s" % (5, "hi")

"%i" % (flt,)

"%()s" % {"": "empty"}

"%s" % {"k": "v"}

"%(1)s" % {"1": "bar"}

"%(a)s" % {"a": 1, "a": 2}

pytest.param('"%8s" % (None,)', id="unsafe width-string conversion"),

"%()s" % {"": "bar"}

"%(1)s" % {1: 2, "1": 2}

"%(and)s" % {"and": 2}
15 changes: 14 additions & 1 deletion crates/ruff_linter/src/rules/pyupgrade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod tests {

use crate::registry::Rule;
use crate::rules::pyupgrade;
use crate::settings::types::PythonVersion;
use crate::settings::types::{PreviewMode, PythonVersion};
use crate::test::test_path;
use crate::{assert_messages, settings};

Expand Down Expand Up @@ -100,6 +100,19 @@ mod tests {
Ok(())
}

#[test_case(Rule::PrintfStringFormatting, Path::new("UP031_0.py"))]
fn preview(rule_code: Rule, path: &Path) -> Result<()> {
let diagnostics = test_path(
Path::new("pyupgrade").join(path),
&settings::LinterSettings {
preview: PreviewMode::Enabled,
..settings::LinterSettings::for_rule(rule_code)
},
)?;
assert_messages!(diagnostics);
Ok(())
}

#[test]
fn async_timeout_error_alias_not_applied_py310() -> Result<()> {
let diagnostics = test_path(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,29 @@ use crate::rules::pyupgrade::helpers::curly_escape;
/// formatting.
///
/// ## Example
///
/// ```python
/// "%s, %s" % ("Hello", "World") # "Hello, World"
/// ```
///
/// Use instead:
///
/// ```python
/// "{}, {}".format("Hello", "World") # "Hello, World"
/// ```
///
/// ```python
/// f"{'Hello'}, {'World'}" # "Hello, World"
/// ```
///
/// ## Fix safety
///
/// In cases where the format string contains a single generic format specifier
/// (e.g. `%s`), and the right-hand side is an ambiguous expression,
/// we cannot offer a safe fix.
///
/// For example, given:
///
/// ```python
/// "%s" % val
/// ```
Expand Down Expand Up @@ -379,6 +387,11 @@ pub(crate) fn printf_string_formatting(
return;
};
if !convertible(&format_string, right) {
if checker.settings.preview.is_enabled() {
checker
.diagnostics
.push(Diagnostic::new(PrintfStringFormatting, string_expr.range()));
}
return;
}

Expand Down Expand Up @@ -437,6 +450,11 @@ pub(crate) fn printf_string_formatting(
let Some(params_string) =
clean_params_dictionary(right, checker.locator(), checker.stylist())
else {
if checker.settings.preview.is_enabled() {
checker
.diagnostics
.push(Diagnostic::new(PrintfStringFormatting, string_expr.range()));
}
return;
};
Cow::Owned(params_string)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1142,12 +1142,16 @@ UP031_0.py:140:7: UP031 [*] Use format specifiers instead of percent format
140 |-print("%2X" % 1)
140 |+print("{:2X}".format(1))
141 141 | print("%02X" % 1)
142 142 |
143 143 | # UP031 (no longer false negatives, but offer no fix because of more complex syntax)

UP031_0.py:141:7: UP031 [*] Use format specifiers instead of percent format
|
140 | print("%2X" % 1)
141 | print("%02X" % 1)
| ^^^^^^^^^^ UP031
142 |
143 | # UP031 (no longer false negatives, but offer no fix because of more complex syntax)
|
= help: Replace with format specifiers

Expand All @@ -1157,3 +1161,6 @@ UP031_0.py:141:7: UP031 [*] Use format specifiers instead of percent format
140 140 | print("%2X" % 1)
141 |-print("%02X" % 1)
141 |+print("{:02X}".format(1))
142 142 |
143 143 | # UP031 (no longer false negatives, but offer no fix because of more complex syntax)
144 144 |
Loading
Loading