Treat `if 0:` and `if False:` as type-checking blocks (#2485)

This commit is contained in:
Charlie Marsh 2023-02-02 12:35:59 -05:00 committed by GitHub
parent a0df78cb7d
commit ec6054edce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 21 deletions

View File

@ -4,15 +4,22 @@ if TYPE_CHECKING:
pass # TCH005 pass # TCH005
if False:
pass # TCH005
if 0:
pass # TCH005
def example(): def example():
if TYPE_CHECKING: if TYPE_CHECKING:
pass # TYP005 pass # TCH005
return return
class Test: class Test:
if TYPE_CHECKING: if TYPE_CHECKING:
pass # TYP005 pass # TCH005
x = 2 x = 2
@ -23,3 +30,10 @@ if TYPE_CHECKING:
if TYPE_CHECKING: if TYPE_CHECKING:
x: List x: List
if False:
x: List
if 0:
x: List

View File

@ -1944,7 +1944,7 @@ where
if flake8_type_checking::helpers::is_type_checking_block(self, test) { if flake8_type_checking::helpers::is_type_checking_block(self, test) {
if self.settings.rules.enabled(&Rule::EmptyTypeCheckingBlock) { 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; let prev_in_type_checking_block = self.in_type_checking_block;

View File

@ -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::ast::types::{Binding, BindingKind, ExecutionContext};
use crate::checkers::ast::Checker; 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 { 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"] call_path.as_slice() == ["typing", "TYPE_CHECKING"]
}) }) {
return true;
}
false
} }
pub fn is_valid_runtime_import(binding: &Binding) -> bool { pub fn is_valid_runtime_import(binding: &Binding) -> bool {

View File

@ -1,5 +1,6 @@
use rustpython_ast::{Stmt, StmtKind};
use ruff_macros::derive_message_formats; use ruff_macros::derive_message_formats;
use rustpython_ast::{Expr, Stmt, StmtKind};
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
@ -18,10 +19,7 @@ impl Violation for EmptyTypeCheckingBlock {
} }
/// TCH005 /// TCH005
pub fn empty_type_checking_block(checker: &mut Checker, test: &Expr, body: &[Stmt]) { pub fn empty_type_checking_block(checker: &mut Checker, 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) { if body.len() == 1 && matches!(body[0].node, StmtKind::Pass) {
checker.diagnostics.push(Diagnostic::new( checker.diagnostics.push(Diagnostic::new(
EmptyTypeCheckingBlock, EmptyTypeCheckingBlock,
@ -29,4 +27,3 @@ pub fn empty_type_checking_block(checker: &mut Checker, test: &Expr, body: &[Stm
)); ));
} }
} }
}

View File

@ -15,20 +15,40 @@ expression: diagnostics
- kind: - kind:
EmptyTypeCheckingBlock: ~ EmptyTypeCheckingBlock: ~
location: 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 column: 8
end_location: end_location:
row: 9 row: 16
column: 12 column: 12
fix: ~ fix: ~
parent: ~ parent: ~
- kind: - kind:
EmptyTypeCheckingBlock: ~ EmptyTypeCheckingBlock: ~
location: location:
row: 15 row: 22
column: 8 column: 8
end_location: end_location:
row: 15 row: 22
column: 12 column: 12
fix: ~ fix: ~
parent: ~ parent: ~