Skip to content

Commit

Permalink
Avoid PEP 604 panic with empty tuple (#3526)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 14, 2023
1 parent a36139a commit 58353a4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP038.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
issubclass("yes", int) # OK
isinstance(1, int | float) # OK
issubclass("yes", int | str) # OK
isinstance(1, ()) # OK
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fn union(elts: &[Expr]) -> Expr {
}
}

/// UP038
pub fn use_pep604_isinstance(checker: &mut Checker, expr: &Expr, func: &Expr, args: &[Expr]) {
if let ExprKind::Name { id, .. } = &func.node {
let Some(kind) = CallKind::from_name(id) else {
Expand All @@ -78,6 +79,9 @@ pub fn use_pep604_isinstance(checker: &mut Checker, expr: &Expr, func: &Expr, ar
};
if let Some(types) = args.get(1) {
if let ExprKind::Tuple { elts, .. } = &types.node {
if elts.is_empty() {
return;
}
let mut diagnostic =
Diagnostic::new(IsinstanceWithTuple { kind }, Range::from(expr));
if checker.patch(diagnostic.kind.rule()) {
Expand Down

0 comments on commit 58353a4

Please sign in to comment.