diff --git a/src/core_codemods/literal_or_new_object_identity.py b/src/core_codemods/literal_or_new_object_identity.py index fe98b157..e6d9f73b 100644 --- a/src/core_codemods/literal_or_new_object_identity.py +++ b/src/core_codemods/literal_or_new_object_identity.py @@ -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 @@ -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): diff --git a/tests/codemods/test_literal_or_new_object_identity.py b/tests/codemods/test_literal_or_new_object_identity.py index 72f53cca..fd165f15 100644 --- a/tests/codemods/test_literal_or_new_object_identity.py +++ b/tests/codemods/test_literal_or_new_object_identity.py @@ -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