Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The `is` and `is not` operator will only return `True` when the expression have the same `id`. In other words, `a is b` is equivalent to `id(a) == id(b)`. New objects and literals have their own identities and thus shouldn't be compared with using the `is` or `is not` operators.
The `is` and `is not` operators only evaluate to `True` when the expressions on each side have the same `id`. In other words, `a is b` is equivalent to `id(a) == id(b)`. With few exceptions, objects and literals have unique identities and thus shouldn't generally be compared by using the `is` or `is not` operators.

Our changes look something like this:

Expand Down
5 changes: 2 additions & 3 deletions src/core_codemods/literal_or_new_object_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
class LiteralOrNewObjectIdentityTransformer(
LibcstResultTransformer, NameAndAncestorResolutionMixin
):

change_description = "Replaces is operator with =="
change_description = "Replace `is` operator with `==`"

def _is_object_creation_or_literal(self, node: cst.BaseExpression):
match node:
Expand Down Expand Up @@ -80,7 +79,7 @@ def leave_Comparison(
LiteralOrNewObjectIdentity = CoreCodemod(
metadata=Metadata(
name="literal-or-new-object-identity",
summary="Replaces is operator with == for literal or new object comparisons",
summary="Replace `is` with `==` for literal or new object comparisons",
review_guidance=ReviewGuidance.MERGE_WITHOUT_REVIEW,
references=[
Reference(
Expand Down