mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-07 21:25:08 +00:00
[pylint
] Also report when the object isn't a literal (PLE1310
) (#15985)
## Summary Follow-up to #15984. Previously, `PLE1310` would only report when the object is a literal: ```python 'a'.strip('//') # error foo = '' foo.strip('//') # no error ``` After this change, objects whose type can be inferred to be either `str` or `bytes` will also be reported in preview. ## Test Plan `cargo nextest run` and `cargo insta test`.
This commit is contained in:
parent
c08989692b
commit
07cf8852a3
6 changed files with 272 additions and 8 deletions
|
@ -741,6 +741,22 @@ impl BuiltinTypeChecker for SetChecker {
|
|||
const EXPR_TYPE: PythonType = PythonType::Set;
|
||||
}
|
||||
|
||||
struct StringChecker;
|
||||
|
||||
impl BuiltinTypeChecker for StringChecker {
|
||||
const BUILTIN_TYPE_NAME: &'static str = "str";
|
||||
const TYPING_NAME: Option<&'static str> = None;
|
||||
const EXPR_TYPE: PythonType = PythonType::String;
|
||||
}
|
||||
|
||||
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::Bytes;
|
||||
}
|
||||
|
||||
struct TupleChecker;
|
||||
|
||||
impl BuiltinTypeChecker for TupleChecker {
|
||||
|
@ -984,6 +1000,16 @@ pub fn is_float(binding: &Binding, semantic: &SemanticModel) -> bool {
|
|||
check_type::<FloatChecker>(binding, semantic)
|
||||
}
|
||||
|
||||
/// Test whether the given binding can be considered an instance of `str`.
|
||||
pub fn is_string(binding: &Binding, semantic: &SemanticModel) -> bool {
|
||||
check_type::<StringChecker>(binding, semantic)
|
||||
}
|
||||
|
||||
/// Test whether the given binding can be considered an instance of `bytes`.
|
||||
pub fn is_bytes(binding: &Binding, semantic: &SemanticModel) -> bool {
|
||||
check_type::<BytesChecker>(binding, semantic)
|
||||
}
|
||||
|
||||
/// Test whether the given binding can be considered a set.
|
||||
///
|
||||
/// For this, we check what value might be associated with it through it's initialization and
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue