Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid TCH005 for if stmt with elif/else block #11376

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Loading