mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:10:09 +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[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], float)
|
||||||
result = isinstance(var[6], int) or isinstance(var[7], int)
|
result = isinstance(var[6], int) or isinstance(var[7], int)
|
||||||
|
result = isinstance(var[6], (float, int)) or False
|
||||||
return result
|
return result
|
||||||
|
|
|
@ -18,15 +18,17 @@ pub fn consider_merging_isinstance(
|
||||||
return;
|
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 {
|
for value in values {
|
||||||
if let ExprKind::Call { func, args, .. } = &value.node {
|
if let ExprKind::Call { func, args, .. } = &value.node {
|
||||||
if matches!(&func.node, ExprKind::Name { id, .. } if id == "isinstance") {
|
if matches!(&func.node, ExprKind::Name { id, .. } if id == "isinstance") {
|
||||||
if let [obj, types] = &args[..] {
|
if let [obj, types] = &args[..] {
|
||||||
obj_to_types
|
let (num_calls, matches) = obj_to_types
|
||||||
.entry(obj.to_string())
|
.entry(obj.to_string())
|
||||||
.or_insert_with(FxHashSet::default)
|
.or_insert_with(|| (0, FxHashSet::default()));
|
||||||
.extend(match &types.node {
|
|
||||||
|
*num_calls += 1;
|
||||||
|
matches.extend(match &types.node {
|
||||||
ExprKind::Tuple { elts, .. } => {
|
ExprKind::Tuple { elts, .. } => {
|
||||||
elts.iter().map(std::string::ToString::to_string).collect()
|
elts.iter().map(std::string::ToString::to_string).collect()
|
||||||
}
|
}
|
||||||
|
@ -39,8 +41,8 @@ pub fn consider_merging_isinstance(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (obj, types) in obj_to_types {
|
for (obj, (num_calls, types)) in obj_to_types {
|
||||||
if types.len() > 1 {
|
if num_calls > 1 && types.len() > 1 {
|
||||||
checker.add_check(Check::new(
|
checker.add_check(Check::new(
|
||||||
CheckKind::ConsiderMergingIsinstance(obj, types.into_iter().sorted().collect()),
|
CheckKind::ConsiderMergingIsinstance(obj, types.into_iter().sorted().collect()),
|
||||||
Range::from_located(expr),
|
Range::from_located(expr),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue