Skip to content

Commit

Permalink
Direct assert rule to ignore asserts in test functions
Browse files Browse the repository at this point in the history
For several Python test frameworks, the assert builtin is
required to validate the test. Precli should avoid showing
results for this condition since this isn't the use case
the rule is trying to find.

Fixes: #638

Signed-off-by: Eric Brown <[email protected]>
  • Loading branch information
ericwb committed Oct 14, 2024
1 parent 91021fb commit 0405ab1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
11 changes: 6 additions & 5 deletions precli/rules/python/stdlib/assert.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ def __init__(self, id: str):
)

def analyze_assert(self, context: dict) -> Optional[Result]:
return Result(
rule_id=self.id,
artifact=context["artifact"],
location=Location(context["node"]),
)
if not context["symtab"].name().startswith("test_"):
return Result(
rule_id=self.id,
artifact=context["artifact"],
location=Location(context["node"]),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# level: NONE
def test_foobar(a: str = None):
assert a is not None
return f"Hello {a}"


foobar(None)
1 change: 1 addition & 0 deletions tests/unit/rules/python/stdlib/assert/test_assert.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def test_rule_meta(self):
"filename",
[
"assert.py",
"assert_test_func.py",
],
)
def test(self, filename):
Expand Down

0 comments on commit 0405ab1

Please sign in to comment.