Skip to content

Commit b1fc362

Browse files
committed
document and fully test empty sequence comparison
1 parent 25e905c commit b1fc362

6 files changed

Lines changed: 45 additions & 3 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from core_codemods.fix_empty_sequence_comparison import FixEmptySequenceComparison
2+
from integration_tests.base_test import (
3+
BaseIntegrationTest,
4+
original_and_expected_from_code_path,
5+
)
6+
7+
8+
class TestFixEmptySequenceComparison(BaseIntegrationTest):
9+
codemod = FixEmptySequenceComparison
10+
code_path = "tests/samples/fix_empty_sequence_comparison.py"
11+
original_code, expected_new_code = original_and_expected_from_code_path(
12+
code_path, [(1, "if x:\n")]
13+
)
14+
expected_diff = (
15+
"--- \n+++ \n@@ -1,3 +1,3 @@\n x = [1]\n-if x != []:\n+if x:\n pass\n"
16+
)
17+
expected_line_change = "2"
18+
change_description = FixEmptySequenceComparison.CHANGE_DESCRIPTION

‎src/codemodder/scripts/generate_docs.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ class DocMetadata:
210210
importance="Medium",
211211
guidance_explained="We believe this change is safe and will not cause any issues.",
212212
),
213+
"fix-empty-sequence-comparison": DocMetadata(
214+
importance="Low",
215+
guidance_explained="Simplifying comparison expressions to empty sequences is safe.",
216+
),
213217
}
214218

215219

‎src/core_codemods/__init__.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@
4444
from .remove_debug_breakpoint import RemoveDebugBreakpoint
4545
from .combine_startswith_endswith import CombineStartswithEndswith
4646
from .fix_deprecated_logging_warn import FixDeprecatedLoggingWarn
47+
<<<<<<< HEAD
4748
from .flask_enable_csrf_protection import FlaskEnableCSRFProtection
4849
from .replace_flask_send_file import ReplaceFlaskSendFile
50+
=======
51+
from .fix_empty_sequence_comparison import FixEmptySequenceComparison
52+
>>>>>>> 00f4616 (document and fully test empty sequence comparison)
4953

5054
registry = CodemodCollection(
5155
origin="pixee",
@@ -98,5 +102,6 @@
98102
FixDeprecatedLoggingWarn,
99103
FlaskEnableCSRFProtection,
100104
ReplaceFlaskSendFile,
105+
FixEmptySequenceComparison,
101106
],
102107
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Empty sequences in a boolean comparison expression are considered falsy so you can use implicit boolean comparisons instead of
2+
comparing against empty sequences directly
3+
4+
Our changes look like the following:
5+
```diff
6+
x = [1]
7+
8+
- if x != []:
9+
+ if x:
10+
pass
11+
```

‎src/core_codemods/fix_empty_sequence_comparison.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import libcst as cst
2-
from typing import Union
32
from codemodder.codemods.api import BaseCodemod, ReviewGuidance
43
from codemodder.codemods.utils_mixin import NameResolutionMixin, AncestorPatternsMixin
54

@@ -8,9 +7,11 @@ class FixEmptySequenceComparison(
87
BaseCodemod, NameResolutionMixin, AncestorPatternsMixin
98
):
109
NAME = "fix-empty-sequence-comparison"
11-
SUMMARY = "TODO"
10+
SUMMARY = "Replace Comparisons to Empty Sequence with Implicit Boolean Comparison"
1211
REVIEW_GUIDANCE = ReviewGuidance.MERGE_WITHOUT_REVIEW
13-
DESCRIPTION = "TODO"
12+
DESCRIPTION = (
13+
"Replace comparisons to empty sequence with implicit boolean comparison."
14+
)
1415
REFERENCES: list = []
1516

1617
def leave_Comparison(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
x = [1]
2+
if x != []:
3+
pass

0 commit comments

Comments
 (0)