Skip to content

Commit

Permalink
Added expression propagation for literal-or-new-object-identity
Browse files Browse the repository at this point in the history
  • Loading branch information
andrecsilva committed Jan 2, 2024
1 parent efc0926 commit 89d9e65
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/core_codemods/literal_or_new_object_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from codemodder.codemods.api import BaseCodemod
from codemodder.codemods.base_codemod import ReviewGuidance

from codemodder.codemods.utils_mixin import NameResolutionMixin
from codemodder.codemods.utils_mixin import NameAndAncestorResolutionMixin


class LiteralOrNewObjectIdentity(BaseCodemod, NameResolutionMixin):
class LiteralOrNewObjectIdentity(BaseCodemod, NameAndAncestorResolutionMixin):
NAME = "literal-or-new-object-identity"
SUMMARY = "Replaces is operator with == for literal or new object comparisons"
REVIEW_GUIDANCE = ReviewGuidance.MERGE_WITHOUT_REVIEW
Expand Down Expand Up @@ -40,7 +40,8 @@ def leave_Comparison(
left=left, comparisons=[cst.ComparisonTarget() as target]
):
if isinstance(target.operator, cst.Is | cst.IsNot):
right = target.comparator
left = self.resolve_expression(left)
right = self.resolve_expression(target.comparator)
if self._is_object_creation_or_literal(
left
) or self._is_object_creation_or_literal(right):
Expand Down
12 changes: 12 additions & 0 deletions tests/codemods/test_literal_or_new_object_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ def test_list(self, tmpdir):
self.run_and_assert(tmpdir, dedent(input_code), dedent(expected))
assert len(self.file_context.codemod_changes) == 1

def test_list_indirect(self, tmpdir):
input_code = """\
some_list = [1,2,3]
l is some_list
"""
expected = """\
some_list = [1,2,3]
l == some_list
"""
self.run_and_assert(tmpdir, dedent(input_code), dedent(expected))
assert len(self.file_context.codemod_changes) == 1

def test_list_lhs(self, tmpdir):
input_code = """\
[1,2,3] is l
Expand Down

0 comments on commit 89d9e65

Please sign in to comment.