Skip to content

Commit

Permalink
test empty sequence for exclude
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Jan 18, 2024
1 parent 3cfaa93 commit 8a535c4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion integration_tests/test_fix_empty_sequence_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ class TestFixEmptySequenceComparison(BaseIntegrationTest):
"--- \n+++ \n@@ -1,3 +1,3 @@\n x = [1]\n-if x != []:\n+if x:\n pass\n"
)
expected_line_change = "2"
change_description = FixEmptySequenceComparison.CHANGE_DESCRIPTION
change_description = FixEmptySequenceComparison.change_description
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Empty sequences in a boolean comparison expression are considered falsy so you can use implicit boolean comparisons instead of
comparing against empty sequences directly
Empty sequences in Python always evaluate to `False`. This means that comparison expressions that use empty sequences can sometimes be simplified. In these cases no explicit comparison is required: instead we can rely on the [truth value](https://docs.python.org/3/library/stdtypes.html#truth-value-testing) of the object under comparison. This is sometimes referred to as "implicit" comparison. Using implicit boolean comparison expressions is considered best practice and can lead to better code.

Our changes look like the following:
```diff
Expand Down
14 changes: 14 additions & 0 deletions tests/codemods/test_fix_empty_sequence_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,20 @@ def test_change_tuple(self, tmpdir, input_code, expected_output):
def test_no_change(self, tmpdir, code):
self.run_and_assert(tmpdir, code, code)

def test_exclude_line(self, tmpdir):
input_code = expected = """\
x = [1]
if x != []:
pass
"""
lines_to_exclude = [2]
self.run_and_assert(
tmpdir,
input_code,
expected,
lines_to_exclude=lines_to_exclude,
)


class TestFixEmptySequenceComparisonAssertStatements(BaseCodemodTest):
codemod = FixEmptySequenceComparison
Expand Down

0 comments on commit 8a535c4

Please sign in to comment.