mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-17 09:00:26 +00:00
[pylint
] Avoid suggesting set rewrites for non-hashable types (#9956)
## Summary Ensures that `x in [y, z]` does not trigger in `x`, `y`, or `z` are known _not_ to be hashable. Closes https://github.com/astral-sh/ruff/issues/9928.
This commit is contained in:
parent
33ac2867b7
commit
ab2253db03
4 changed files with 98 additions and 15 deletions
|
@ -426,8 +426,16 @@ fn check_type<T: TypeChecker>(binding: &Binding, semantic: &SemanticModel) -> bo
|
|||
// ```
|
||||
//
|
||||
// The type checker might know how to infer the type based on `init_expr`.
|
||||
Some(Stmt::Assign(ast::StmtAssign { value, .. })) => {
|
||||
T::match_initializer(value.as_ref(), semantic)
|
||||
Some(Stmt::Assign(ast::StmtAssign { targets, value, .. })) => {
|
||||
// TODO(charlie): Replace this with `find_binding_value`, which matches the values.
|
||||
if targets
|
||||
.iter()
|
||||
.any(|target| target.range().contains_range(binding.range()))
|
||||
{
|
||||
T::match_initializer(value.as_ref(), semantic)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
// ```python
|
||||
|
@ -435,8 +443,15 @@ fn check_type<T: TypeChecker>(binding: &Binding, semantic: &SemanticModel) -> bo
|
|||
// ```
|
||||
//
|
||||
// In this situation, we check only the annotation.
|
||||
Some(Stmt::AnnAssign(ast::StmtAnnAssign { annotation, .. })) => {
|
||||
T::match_annotation(annotation.as_ref(), semantic)
|
||||
Some(Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||
target, annotation, ..
|
||||
})) => {
|
||||
// TODO(charlie): Replace this with `find_binding_value`, which matches the values.
|
||||
if target.range().contains_range(binding.range()) {
|
||||
T::match_annotation(annotation.as_ref(), semantic)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
_ => false,
|
||||
},
|
||||
|
@ -466,8 +481,15 @@ fn check_type<T: TypeChecker>(binding: &Binding, semantic: &SemanticModel) -> bo
|
|||
// ```
|
||||
//
|
||||
// It's a typed declaration, type annotation is the only source of information.
|
||||
Some(Stmt::AnnAssign(ast::StmtAnnAssign { annotation, .. })) => {
|
||||
T::match_annotation(annotation.as_ref(), semantic)
|
||||
Some(Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||
target, annotation, ..
|
||||
})) => {
|
||||
// TODO(charlie): Replace this with `find_binding_value`, which matches the values.
|
||||
if target.range().contains_range(binding.range()) {
|
||||
T::match_annotation(annotation.as_ref(), semantic)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
_ => false,
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue