Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added expression propagation for literal-or-new-object-identity codemod #200

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/codemodder/codemods/utils_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def find_global_scope(self):
def find_single_assignment(
self,
node: Union[cst.Name, cst.Attribute, cst.Call, cst.Subscript, cst.Decorator],
) -> Optional[Assignment]:
) -> Optional[BaseAssignment]:
"""
Given a MetadataWrapper and a CSTNode representing an access, find if there is a single assignment that it refers to.
"""
Expand Down Expand Up @@ -395,7 +395,7 @@ def resolve_expression(self, node: cst.BaseExpression) -> cst.BaseExpression:

def _resolve_name_transitive(self, node: cst.Name) -> Optional[cst.BaseExpression]:
maybe_assignment = self.find_single_assignment(node)
if maybe_assignment:
if maybe_assignment and isinstance(maybe_assignment, Assignment):
if maybe_target_assignment := self.is_target_of_assignment(
maybe_assignment.node
):
Expand Down
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
Loading