Ignore assert errors (S101) in TYPE_CHECKING blocks (#3960)

This commit is contained in:
Charlie Marsh
2023-04-13 14:20:44 -04:00
committed by GitHub
parent 032a84b167
commit ffac4f6ec3
3 changed files with 31 additions and 29 deletions

View File

@@ -1,11 +1,13 @@
# Error
assert True
assert True # S101
def fn():
x = 1
assert x == 1 # S101
assert x == 2 # S101
# Error
assert x == 1
# Error
assert x == 2
from typing import TYPE_CHECKING
if TYPE_CHECKING:
assert True # OK

View File

@@ -1603,16 +1603,18 @@ where
}
}
StmtKind::Assert { test, msg } => {
if !self.ctx.in_type_checking_block {
if self.settings.rules.enabled(Rule::Assert) {
self.diagnostics
.push(flake8_bandit::rules::assert_used(stmt));
}
}
if self.settings.rules.enabled(Rule::AssertTuple) {
pyflakes::rules::assert_tuple(self, stmt, test);
}
if self.settings.rules.enabled(Rule::AssertFalse) {
flake8_bugbear::rules::assert_false(self, stmt, test, msg.as_deref());
}
if self.settings.rules.enabled(Rule::Assert) {
self.diagnostics
.push(flake8_bandit::rules::assert_used(stmt));
}
if self.settings.rules.enabled(Rule::PytestAssertAlwaysFalse) {
if let Some(diagnostic) = flake8_pytest_style::rules::assert_falsy(stmt, test) {
self.diagnostics.push(diagnostic);

View File

@@ -1,29 +1,27 @@
---
source: crates/ruff/src/rules/flake8_bandit/mod.rs
---
S101.py:2:1: S101 Use of `assert` detected
S101.py:1:1: S101 Use of `assert` detected
|
2 | # Error
3 | assert True
1 | assert True # S101
| ^^^^^^ S101
4 |
5 | def fn():
|
S101.py:8:5: S101 Use of `assert` detected
|
8 | # Error
9 | assert x == 1
| ^^^^^^ S101
10 |
11 | # Error
|
S101.py:6:5: S101 Use of `assert` detected
|
6 | def fn():
7 | x = 1
8 | assert x == 1 # S101
| ^^^^^^ S101
9 | assert x == 2 # S101
|
S101.py:11:5: S101 Use of `assert` detected
|
11 | # Error
12 | assert x == 2
| ^^^^^^ S101
|
S101.py:7:5: S101 Use of `assert` detected
|
7 | x = 1
8 | assert x == 1 # S101
9 | assert x == 2 # S101
| ^^^^^^ S101
|