Skip to content

Commit

Permalink
Avoid TCH005 for if stmt with elif/else block (#11376)
Browse files Browse the repository at this point in the history
## Summary

This PR fixes a bug where the auto-fix for `TCH005` would delete the
entire `if` statement.

The fix in this PR is to not consider it a violation if there are any
`elif`/`else` blocks. This also matches the behavior of the original
plugin.

fixes: #11368 

## Test plan

Add test cases.
  • Loading branch information
dhruvmanila authored May 13, 2024
1 parent d7f093e commit d835b3e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ TCH005.py:45:5: TCH005 [*] Found empty type-checking block
44 | if TYPE_CHECKING:
45 | pass # TCH005
| ^^^^ TCH005
46 |
47 | # https://github.com/astral-sh/ruff/issues/11368
|
= help: Delete empty type-checking block

Expand All @@ -110,5 +112,6 @@ TCH005.py:45:5: TCH005 [*] Found empty type-checking block
43 43 |
44 |-if TYPE_CHECKING:
45 |- pass # TCH005


46 44 |
47 45 | # https://github.com/astral-sh/ruff/issues/11368
48 46 | if TYPE_CHECKING:

0 comments on commit d835b3e

Please sign in to comment.