[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:
InSync 2025-02-10 15:31:27 +07:00 committed by GitHub
parent c08989692b
commit 07cf8852a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 272 additions and 8 deletions

View file

@ -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