Skip to content

Commit

Permalink
Treat if 0: and if False: as type-checking blocks (#2485)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Feb 2, 2023
1 parent a0df78c commit ec6054e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 21 deletions.
18 changes: 16 additions & 2 deletions resources/test/fixtures/flake8_type_checking/TCH005.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@
pass # TCH005


if False:
pass # TCH005

if 0:
pass # TCH005


def example():
if TYPE_CHECKING:
pass # TYP005
pass # TCH005
return


class Test:
if TYPE_CHECKING:
pass # TYP005
pass # TCH005
x = 2


Expand All @@ -23,3 +30,10 @@ class Test:

if TYPE_CHECKING:
x: List


if False:
x: List

if 0:
x: List
2 changes: 1 addition & 1 deletion src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1944,7 +1944,7 @@ where

if flake8_type_checking::helpers::is_type_checking_block(self, test) {
if self.settings.rules.enabled(&Rule::EmptyTypeCheckingBlock) {
flake8_type_checking::rules::empty_type_checking_block(self, test, body);
flake8_type_checking::rules::empty_type_checking_block(self, body);
}

let prev_in_type_checking_block = self.in_type_checking_block;
Expand Down
35 changes: 32 additions & 3 deletions src/rules/flake8_type_checking/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
use rustpython_ast::Expr;
use num_traits::Zero;
use rustpython_ast::{Constant, Expr, ExprKind};

use crate::ast::types::{Binding, BindingKind, ExecutionContext};
use crate::checkers::ast::Checker;

/// Return `true` if [`Expr`] is a guard for a type-checking block.
pub fn is_type_checking_block(checker: &Checker, test: &Expr) -> bool {
checker.resolve_call_path(test).map_or(false, |call_path| {
// Ex) `if False:`
if matches!(
test.node,
ExprKind::Constant {
value: Constant::Bool(false),
..
}
) {
return true;
}

// Ex) `if 0:`
if let ExprKind::Constant {
value: Constant::Int(value),
..
} = &test.node
{
if value.is_zero() {
return true;
}
}

// Ex) `if typing.TYPE_CHECKING:`
if checker.resolve_call_path(test).map_or(false, |call_path| {
call_path.as_slice() == ["typing", "TYPE_CHECKING"]
})
}) {
return true;
}

false
}

pub fn is_valid_runtime_import(binding: &Binding) -> bool {
Expand Down
19 changes: 8 additions & 11 deletions src/rules/flake8_type_checking/rules/empty_type_checking_block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rustpython_ast::{Stmt, StmtKind};

use ruff_macros::derive_message_formats;
use rustpython_ast::{Expr, Stmt, StmtKind};

use crate::ast::types::Range;
use crate::checkers::ast::Checker;
Expand All @@ -18,15 +19,11 @@ impl Violation for EmptyTypeCheckingBlock {
}

/// TCH005
pub fn empty_type_checking_block(checker: &mut Checker, test: &Expr, body: &[Stmt]) {
if checker.resolve_call_path(test).map_or(false, |call_path| {
call_path.as_slice() == ["typing", "TYPE_CHECKING"]
}) {
if body.len() == 1 && matches!(body[0].node, StmtKind::Pass) {
checker.diagnostics.push(Diagnostic::new(
EmptyTypeCheckingBlock,
Range::from_located(&body[0]),
));
}
pub fn empty_type_checking_block(checker: &mut Checker, body: &[Stmt]) {
if body.len() == 1 && matches!(body[0].node, StmtKind::Pass) {
checker.diagnostics.push(Diagnostic::new(
EmptyTypeCheckingBlock,
Range::from_located(&body[0]),
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,40 @@ expression: diagnostics
- kind:
EmptyTypeCheckingBlock: ~
location:
row: 9
row: 8
column: 4
end_location:
row: 8
column: 8
fix: ~
parent: ~
- kind:
EmptyTypeCheckingBlock: ~
location:
row: 11
column: 4
end_location:
row: 11
column: 8
fix: ~
parent: ~
- kind:
EmptyTypeCheckingBlock: ~
location:
row: 16
column: 8
end_location:
row: 9
row: 16
column: 12
fix: ~
parent: ~
- kind:
EmptyTypeCheckingBlock: ~
location:
row: 15
row: 22
column: 8
end_location:
row: 15
row: 22
column: 12
fix: ~
parent: ~
Expand Down

0 comments on commit ec6054e

Please sign in to comment.