Skip to content

Commit

Permalink
Per review
Browse files Browse the repository at this point in the history
  • Loading branch information
InSyncWithFoo committed Feb 9, 2025
1 parent 1c911c8 commit ceb9584
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@
''.strip(r'\b\x09')
''.strip('\\\x5C')

# Errors: Type inference
b = b''
b.strip(b'//')

# Errors: Type inference (preview)
foo: str = ""; bar: bytes = b""
foo.rstrip("//")
bar.lstrip(b"//")


# OK: Different types
b"".strip("//")
"".strip(b"//")
Expand All @@ -93,13 +103,8 @@
"".rstrip()

# OK: Not literals
foo: str = ""; bar: bytes = b""
"".strip(foo)
b"".strip(bar)

# False negative
foo.rstrip("//")
bar.lstrip(b"//")

# OK: Not `.[lr]?strip`
"".mobius_strip("")
12 changes: 7 additions & 5 deletions crates/ruff_linter/src/rules/pylint/rules/bad_str_strip_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ impl ValueKind {
let binding_id = semantic.only_binding(name)?;
let binding = semantic.binding(binding_id);

match () {
() if typing::is_string(binding, semantic) => Some(Self::String),
() if typing::is_bytes(binding, semantic) => Some(Self::Bytes),
() => None,
if typing::is_string(binding, semantic) {
Some(Self::String)
} else if typing::is_bytes(binding, semantic) {
Some(Self::Bytes)
} else {
None
}
}
_ => None,
Expand Down Expand Up @@ -183,7 +185,7 @@ pub(crate) fn bad_str_strip_call(checker: &Checker, call: &ast::ExprCall) {
return;
};

let value = value.as_ref();
let value = &**value;

if checker.settings.preview.is_disabled()
&& !matches!(value, Expr::StringLiteral(_) | Expr::BytesLiteral(_))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,5 @@ bad_str_strip_call.py:81:10: PLE1310 String `strip` call contains duplicate char
81 | ''.strip('\\\x5C')
| ^^^^^^^^ PLE1310
82 |
83 | # OK: Different types
83 | # Errors: Type inference
|
Original file line number Diff line number Diff line change
Expand Up @@ -179,23 +179,32 @@ bad_str_strip_call.py:81:10: PLE1310 String `strip` call contains duplicate char
81 | ''.strip('\\\x5C')
| ^^^^^^^^ PLE1310
82 |
83 | # OK: Different types
|

bad_str_strip_call.py:101:12: PLE1310 String `rstrip` call contains duplicate characters (did you mean `removesuffix`?)
|
100 | # False negative
101 | foo.rstrip("//")
| ^^^^ PLE1310
102 | bar.lstrip(b"//")
|

bad_str_strip_call.py:102:12: PLE1310 String `lstrip` call contains duplicate characters (did you mean `removeprefix`?)
|
100 | # False negative
101 | foo.rstrip("//")
102 | bar.lstrip(b"//")
| ^^^^^ PLE1310
103 |
104 | # OK: Not `.[lr]?strip`
|
83 | # Errors: Type inference
|

bad_str_strip_call.py:85:9: PLE1310 String `strip` call contains duplicate characters
|
83 | # Errors: Type inference
84 | b = b''
85 | b.strip(b'//')
| ^^^^^ PLE1310
86 |
87 | # Errors: Type inference (preview)
|

bad_str_strip_call.py:89:12: PLE1310 String `rstrip` call contains duplicate characters (did you mean `removesuffix`?)
|
87 | # Errors: Type inference (preview)
88 | foo: str = ""; bar: bytes = b""
89 | foo.rstrip("//")
| ^^^^ PLE1310
90 | bar.lstrip(b"//")
|

bad_str_strip_call.py:90:12: PLE1310 String `lstrip` call contains duplicate characters (did you mean `removeprefix`?)
|
88 | foo: str = ""; bar: bytes = b""
89 | foo.rstrip("//")
90 | bar.lstrip(b"//")
| ^^^^^ PLE1310
|
2 changes: 1 addition & 1 deletion crates/ruff_python_semantic/src/analyze/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ struct BytesChecker;
impl BuiltinTypeChecker for BytesChecker {
const BUILTIN_TYPE_NAME: &'static str = "bytes";
const TYPING_NAME: Option<&'static str> = None;
const EXPR_TYPE: PythonType = PythonType::String;
const EXPR_TYPE: PythonType = PythonType::Bytes;
}

struct TupleChecker;
Expand Down

0 comments on commit ceb9584

Please sign in to comment.