Skip to content

Commit

Permalink
Documentation for literal-or-new-object-identity
Browse files Browse the repository at this point in the history
  • Loading branch information
andrecsilva committed Dec 21, 2023
1 parent 210ecc1 commit 7450c26
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/codemodder/scripts/generate_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ class DocMetadata:
importance="Low",
guidance_explained="Removing future imports is safe and will not cause any issues.",
),
"literal-or-new-object-identity": DocMetadata(
importance="Low",
guidance_explained="Since literals and new objects have their own identities, comparisons against them using `is` operators are most likely a bug and thus we deem the change safe.",
),
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The `is` and `is not` operator will only return `True` when the expression have the same `id`, that is, `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.

Our changes look something like this:

```diff
def foo(l):
- return l is [1,2,3]
+ return l == [1,2,3]
```
2 changes: 1 addition & 1 deletion src/core_codemods/literal_or_new_object_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class LiteralOrNewObjectIdentity(BaseCodemod, NameResolutionMixin):
DESCRIPTION = SUMMARY
REFERENCES = [
{
"url": "https://docs.python.org/3/library/operator.html#operator.is_",
"url": "https://docs.python.org/3/library/stdtypes.html#comparisons",
"description": "",
},
]
Expand Down

0 comments on commit 7450c26

Please sign in to comment.