Avoid PEP 604 isinstance errors for starred tuples (#3527)

This commit is contained in:
Charlie Marsh 2023-03-14 18:08:43 -04:00 committed by GitHub
parent 58353a4bf4
commit 2545869797
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View file

@ -6,3 +6,4 @@ issubclass("yes", int) # OK
isinstance(1, int | float) # OK isinstance(1, int | float) # OK
issubclass("yes", int | str) # OK issubclass("yes", int | str) # OK
isinstance(1, ()) # OK isinstance(1, ()) # OK
isinstance(1, (int, *(str, bytes))) # OK

View file

@ -79,9 +79,19 @@ pub fn use_pep604_isinstance(checker: &mut Checker, expr: &Expr, func: &Expr, ar
}; };
if let Some(types) = args.get(1) { if let Some(types) = args.get(1) {
if let ExprKind::Tuple { elts, .. } = &types.node { if let ExprKind::Tuple { elts, .. } = &types.node {
// Ex) `()`
if elts.is_empty() { if elts.is_empty() {
return; return;
} }
// Ex) `(*args,)`
if elts
.iter()
.any(|elt| matches!(elt.node, ExprKind::Starred { .. }))
{
return;
}
let mut diagnostic = let mut diagnostic =
Diagnostic::new(IsinstanceWithTuple { kind }, Range::from(expr)); Diagnostic::new(IsinstanceWithTuple { kind }, Range::from(expr));
if checker.patch(diagnostic.kind.rule()) { if checker.patch(diagnostic.kind.rule()) {