Skip to content

Commit

Permalink
Avoid TCH005 for if stmt with elif/else block
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed May 12, 2024
1 parent f79c980 commit 1c9cb37
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@ class Test:

if TYPE_CHECKING:
pass # TCH005

# https://github.com/astral-sh/ruff/issues/11368
if TYPE_CHECKING:
pass
else:
pass
if TYPE_CHECKING:
pass
elif test:
pass
5 changes: 1 addition & 4 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use ruff_diagnostics::Diagnostic;
use ruff_python_ast::helpers;
use ruff_python_ast::types::Node;
use ruff_python_ast::{self as ast, Expr, Stmt};
use ruff_python_semantic::analyze::typing;
use ruff_python_semantic::ScopeKind;
use ruff_text_size::Ranged;

Expand Down Expand Up @@ -1098,9 +1097,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
pylint::rules::too_many_nested_blocks(checker, stmt);
}
if checker.enabled(Rule::EmptyTypeCheckingBlock) {
if typing::is_type_checking_block(if_, &checker.semantic) {
flake8_type_checking::rules::empty_type_checking_block(checker, if_);
}
flake8_type_checking::rules::empty_type_checking_block(checker, if_);
}
if checker.enabled(Rule::IfTuple) {
pyflakes::rules::if_tuple(checker, if_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use ruff_python_ast as ast;

use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_semantic::analyze::typing;
use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -47,6 +48,14 @@ impl AlwaysFixableViolation for EmptyTypeCheckingBlock {

/// TCH005
pub(crate) fn empty_type_checking_block(checker: &mut Checker, stmt: &ast::StmtIf) {
if !typing::is_type_checking_block(stmt, checker.semantic()) {
return;
}

if !stmt.elif_else_clauses.is_empty() {
return;
}

let [stmt] = stmt.body.as_slice() else {
return;
};
Expand Down

0 comments on commit 1c9cb37

Please sign in to comment.