Skip to content

Commit ceb9584

Browse files
committed
Per review
1 parent 1c911c8 commit ceb9584

File tree

5 files changed

+48
-32
lines changed

5 files changed

+48
-32
lines changed

crates/ruff_linter/resources/test/fixtures/pylint/bad_str_strip_call.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@
8080
''.strip(r'\b\x09')
8181
''.strip('\\\x5C')
8282

83+
# Errors: Type inference
84+
b = b''
85+
b.strip(b'//')
86+
87+
# Errors: Type inference (preview)
88+
foo: str = ""; bar: bytes = b""
89+
foo.rstrip("//")
90+
bar.lstrip(b"//")
91+
92+
8393
# OK: Different types
8494
b"".strip("//")
8595
"".strip(b"//")
@@ -93,13 +103,8 @@
93103
"".rstrip()
94104

95105
# OK: Not literals
96-
foo: str = ""; bar: bytes = b""
97106
"".strip(foo)
98107
b"".strip(bar)
99108

100-
# False negative
101-
foo.rstrip("//")
102-
bar.lstrip(b"//")
103-
104109
# OK: Not `.[lr]?strip`
105110
"".mobius_strip("")

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ impl ValueKind {
8282
let binding_id = semantic.only_binding(name)?;
8383
let binding = semantic.binding(binding_id);
8484

85-
match () {
86-
() if typing::is_string(binding, semantic) => Some(Self::String),
87-
() if typing::is_bytes(binding, semantic) => Some(Self::Bytes),
88-
() => None,
85+
if typing::is_string(binding, semantic) {
86+
Some(Self::String)
87+
} else if typing::is_bytes(binding, semantic) {
88+
Some(Self::Bytes)
89+
} else {
90+
None
8991
}
9092
}
9193
_ => None,
@@ -183,7 +185,7 @@ pub(crate) fn bad_str_strip_call(checker: &Checker, call: &ast::ExprCall) {
183185
return;
184186
};
185187

186-
let value = value.as_ref();
188+
let value = &**value;
187189

188190
if checker.settings.preview.is_disabled()
189191
&& !matches!(value, Expr::StringLiteral(_) | Expr::BytesLiteral(_))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,5 @@ bad_str_strip_call.py:81:10: PLE1310 String `strip` call contains duplicate char
179179
81 | ''.strip('\\\x5C')
180180
| ^^^^^^^^ PLE1310
181181
82 |
182-
83 | # OK: Different types
182+
83 | # Errors: Type inference
183183
|

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

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -179,23 +179,32 @@ bad_str_strip_call.py:81:10: PLE1310 String `strip` call contains duplicate char
179179
81 | ''.strip('\\\x5C')
180180
| ^^^^^^^^ PLE1310
181181
82 |
182-
83 | # OK: Different types
183-
|
184-
185-
bad_str_strip_call.py:101:12: PLE1310 String `rstrip` call contains duplicate characters (did you mean `removesuffix`?)
186-
|
187-
100 | # False negative
188-
101 | foo.rstrip("//")
189-
| ^^^^ PLE1310
190-
102 | bar.lstrip(b"//")
191-
|
192-
193-
bad_str_strip_call.py:102:12: PLE1310 String `lstrip` call contains duplicate characters (did you mean `removeprefix`?)
194-
|
195-
100 | # False negative
196-
101 | foo.rstrip("//")
197-
102 | bar.lstrip(b"//")
198-
| ^^^^^ PLE1310
199-
103 |
200-
104 | # OK: Not `.[lr]?strip`
201-
|
182+
83 | # Errors: Type inference
183+
|
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_python_semantic/src/analyze/typing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ struct BytesChecker;
754754
impl BuiltinTypeChecker for BytesChecker {
755755
const BUILTIN_TYPE_NAME: &'static str = "bytes";
756756
const TYPING_NAME: Option<&'static str> = None;
757-
const EXPR_TYPE: PythonType = PythonType::String;
757+
const EXPR_TYPE: PythonType = PythonType::Bytes;
758758
}
759759

760760
struct TupleChecker;

0 commit comments

Comments
 (0)