mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:35 +00:00
Avoid false-positive on PLR1701 for multi-type isinstance calls (#1063)
This commit is contained in:
parent
fb2c457a9b
commit
e695f6eb25
2 changed files with 16 additions and 13 deletions
|
@ -34,4 +34,5 @@ def isinstances():
|
|||
result = isinstance(var[7], int) or not isinstance(var[7], float)
|
||||
result = isinstance(var[6], int) or isinstance(var[7], float)
|
||||
result = isinstance(var[6], int) or isinstance(var[7], int)
|
||||
result = isinstance(var[6], (float, int)) or False
|
||||
return result
|
||||
|
|
|
@ -18,29 +18,31 @@ pub fn consider_merging_isinstance(
|
|||
return;
|
||||
}
|
||||
|
||||
let mut obj_to_types: FxHashMap<String, FxHashSet<String>> = FxHashMap::default();
|
||||
let mut obj_to_types: FxHashMap<String, (usize, FxHashSet<String>)> = FxHashMap::default();
|
||||
for value in values {
|
||||
if let ExprKind::Call { func, args, .. } = &value.node {
|
||||
if matches!(&func.node, ExprKind::Name { id, .. } if id == "isinstance") {
|
||||
if let [obj, types] = &args[..] {
|
||||
obj_to_types
|
||||
let (num_calls, matches) = obj_to_types
|
||||
.entry(obj.to_string())
|
||||
.or_insert_with(FxHashSet::default)
|
||||
.extend(match &types.node {
|
||||
ExprKind::Tuple { elts, .. } => {
|
||||
elts.iter().map(std::string::ToString::to_string).collect()
|
||||
}
|
||||
_ => {
|
||||
vec![types.to_string()]
|
||||
}
|
||||
});
|
||||
.or_insert_with(|| (0, FxHashSet::default()));
|
||||
|
||||
*num_calls += 1;
|
||||
matches.extend(match &types.node {
|
||||
ExprKind::Tuple { elts, .. } => {
|
||||
elts.iter().map(std::string::ToString::to_string).collect()
|
||||
}
|
||||
_ => {
|
||||
vec![types.to_string()]
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (obj, types) in obj_to_types {
|
||||
if types.len() > 1 {
|
||||
for (obj, (num_calls, types)) in obj_to_types {
|
||||
if num_calls > 1 && types.len() > 1 {
|
||||
checker.add_check(Check::new(
|
||||
CheckKind::ConsiderMergingIsinstance(obj, types.into_iter().sorted().collect()),
|
||||
Range::from_located(expr),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue