Skip to content

Commit d8159e8

Browse files
committed
[pylint] Better inference for str.strip (PLE310) (#16671)
## Summary This PR stabilizes the behavior introduced in #15985 The new behavior improves the inference of `str.strip` calls: * before: The rule only considered calls on string or byte literals (`"abcd".strip`) * now: The rule also catches calls to `strip` on object where the type is known to be a `str` or `bytes` (e.g. `a = "abc"; a.strip("//")`) The new behavior shipped as part of Ruff 0.9.6 on the 10th of Feb which is a little more than a month ago. There have been now new issues or PRs related to the new behavior.
1 parent 04ad562 commit d8159e8

4 files changed

+28
-235
lines changed

crates/ruff_linter/src/rules/pylint/mod.rs

+1-19
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ mod tests {
1616
use crate::registry::Rule;
1717
use crate::rules::{flake8_tidy_imports, pylint};
1818

19+
use crate::assert_messages;
1920
use crate::settings::types::PreviewMode;
2021
use crate::settings::LinterSettings;
2122
use crate::test::test_path;
22-
use crate::{assert_messages, settings};
2323

2424
#[test_case(Rule::SingledispatchMethod, Path::new("singledispatch_method.py"))]
2525
#[test_case(
@@ -440,22 +440,4 @@ mod tests {
440440
assert_messages!(diagnostics);
441441
Ok(())
442442
}
443-
444-
#[test_case(Rule::BadStrStripCall, Path::new("bad_str_strip_call.py"))]
445-
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
446-
let snapshot = format!(
447-
"preview__{}_{}",
448-
rule_code.noqa_code(),
449-
path.to_string_lossy()
450-
);
451-
let diagnostics = test_path(
452-
Path::new("pylint").join(path).as_path(),
453-
&settings::LinterSettings {
454-
preview: PreviewMode::Enabled,
455-
..settings::LinterSettings::for_rule(rule_code)
456-
},
457-
)?;
458-
assert_messages!(snapshot, diagnostics);
459-
Ok(())
460-
}
461443
}

crates/ruff_linter/src/rules/pylint/rules/bad_str_strip_call.rs

-6
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,6 @@ pub(crate) fn bad_str_strip_call(checker: &Checker, call: &ast::ExprCall) {
187187

188188
let value = &**value;
189189

190-
if checker.settings.preview.is_disabled()
191-
&& !matches!(value, Expr::StringLiteral(_) | Expr::BytesLiteral(_))
192-
{
193-
return;
194-
}
195-
196190
let Some(value_kind) = ValueKind::from(value, checker.semantic()) else {
197191
return;
198192
};

crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLE1310_bad_str_strip_call.py.snap

+27
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,30 @@ bad_str_strip_call.py:81:10: PLE1310 String `strip` call contains duplicate char
181181
82 |
182182
83 | # Errors: Type inference
183183
|
184+
185+
bad_str_strip_call.py:85:9: PLE1310 String `strip` call contains duplicate characters
186+
|
187+
83 | # Errors: Type inference
188+
84 | b = b''
189+
85 | b.strip(b'//')
190+
| ^^^^^ PLE1310
191+
86 |
192+
87 | # Errors: Type inference (preview)
193+
|
194+
195+
bad_str_strip_call.py:89:12: PLE1310 String `rstrip` call contains duplicate characters (did you mean `removesuffix`?)
196+
|
197+
87 | # Errors: Type inference (preview)
198+
88 | foo: str = ""; bar: bytes = b""
199+
89 | foo.rstrip("//")
200+
| ^^^^ PLE1310
201+
90 | bar.lstrip(b"//")
202+
|
203+
204+
bad_str_strip_call.py:90:12: PLE1310 String `lstrip` call contains duplicate characters (did you mean `removeprefix`?)
205+
|
206+
88 | foo: str = ""; bar: bytes = b""
207+
89 | foo.rstrip("//")
208+
90 | bar.lstrip(b"//")
209+
| ^^^^^ PLE1310
210+
|

crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__preview__PLE1310_bad_str_strip_call.py.snap

-210
This file was deleted.

0 commit comments

Comments
 (0)