Repository: asottile/pyupgrade

Issue:
Fix `is (int, float, bytes, str)` automatically
Similar to #47, let's automatically fix this as it becomes a `SyntaxWarning` in python3.8

```pycon
$ python3.8 -Werror
Python 3.8.0a3 (default, Mar 27 2019, 03:46:44) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 5
>>> x is 5
  File "<stdin>", line 1
SyntaxError: "is" with a literal. Did you mean "=="?
>>> 
```

Comments:

Patch:
diff --git a/README.md b/README.md
index d7d5e9c..dff27e7 100644
--- a/README.md
+++ b/README.md
@@ -90,6 +90,17 @@ u'\d'   # u'\\d'
 # but in python3.x, that's our friend ☃
 ```
 
+### `is` / `is not` comparison to constant literals
+
+In python3.8+, comparison to literals becomes a `SyntaxWarning` as the success
+of those comparisons is implementation specific (due to common object caching).
+
+```python
+x is 5      # x == 5
+x is not 5  # x != 5
+x is 'foo'  # x == foo
+```
+
 ### `ur` string literals
 
 `ur'...'` literals are not valid in python 3.x
diff --git a/pyupgrade.py b/pyupgrade.py
index 221c87f..b67cbf7 100644
--- a/pyupgrade.py
+++ b/pyupgrade.py
@@ -324,13 +324,34 @@ def _process_dict_comp(tokens, start, arg):
     tokens[start:start + 2] = [Token('OP', '{')]
 
 
-class FindDictsSetsVisitor(ast.NodeVisitor):
-    def __init__(self):
+def _process_is_literal(tokens, i, compare):
+    while tokens[i].src != 'is':
+        i -= 1
+    if isinstance(compare, ast.Is):
+        tokens[i] = tokens[i]._replace(src='==')
+    else:
+        tokens[i] = tokens[i]._replace(src='!=')
+        # since we iterate backward, the dummy tokens keep the same length
+        i += 1
+        while tokens[i].src != 'not':
+            tokens[i] = Token('DUMMY', '')
+            i += 1
+        tokens[i] = Token('DUMMY', '')
+
+
+LITERAL_TYPES = (ast.Str, ast.Num)
+if sys.version_info >= (3,):  # pragma: no cover (py3+)
+    LITERAL_TYPES += (ast.Bytes,)
+
+
+class Py2CompatibleVisitor(ast.NodeVisitor):
+    def __init__(self):  # type: () -> None
         self.dicts = {}
         self.sets = {}
         self.set_empty_literals = {}
+        self.is_literal = {}
 
-    def visit_Call(self, node):
+    def visit_Call(self, node):  # type: (ast.AST) -> None
         if (
                 isinstance(node.func, ast.Name) and
                 node.func.id == 'set' and
@@ -357,15 +378,35 @@ class FindDictsSetsVisitor(ast.NodeVisitor):
             self.dicts[_ast_to_offset(node.func)] = arg
         self.generic_visit(node)
 
+    def visit_Compare(self, node):  # type: (ast.AST) -> None
+        left = node.left
+        for op, right in zip(node.ops, node.comparators):
+            if (
+                    isinstance(op, (ast.Is, ast.IsNot)) and
+                    (
+                        isinstance(left, LITERAL_TYPES) or
+                        isinstance(right, LITERAL_TYPES)
+                    )
+            ):
+                self.is_literal[_ast_to_offset(right)] = op
+            left = right
 
-def _fix_dict_set(contents_text):
+        self.generic_visit(node)
+
+
+def _fix_py2_compatible(contents_text):
     try:
         ast_obj = ast_parse(contents_text)
     except SyntaxError:
         return contents_text
-    visitor = FindDictsSetsVisitor()
+    visitor = Py2CompatibleVisitor()
     visitor.visit(ast_obj)
-    if not any((visitor.dicts, visitor.sets, visitor.set_empty_literals)):
+    if not any((
+            visitor.dicts,
+            visitor.sets,
+            visitor.set_empty_literals,
+            visitor.is_literal,
+    )):
         return contents_text
 
     tokens = src_to_tokens(contents_text)
@@ -376,6 +417,8 @@ def _fix_dict_set(contents_text):
             _process_set_empty_literal(tokens, i)
         elif token.offset in visitor.sets:
             _process_set_literal(tokens, i, visitor.sets[token.offset])
+        elif token.offset in visitor.is_literal:
+            _process_is_literal(tokens, i, visitor.is_literal[token.offset])
     return tokens_to_src(tokens)
 
 
@@ -1401,7 +1444,7 @@ def fix_file(filename, args):
         print('{} is non-utf-8 (not supported)'.format(filename))
         return 1
 
-    contents_text = _fix_dict_set(contents_text)
+    contents_text = _fix_py2_compatible(contents_text)
     contents_text = _fix_format_literals(contents_text)
     contents_text = _fix_tokens(contents_text, args.py3_plus)
     if not args.keep_percent_format:


Test patch:
diff --git a/tests/pyupgrade_test.py b/tests/pyupgrade_test.py
index 8282b62..9ab97d3 100644
--- a/tests/pyupgrade_test.py
+++ b/tests/pyupgrade_test.py
@@ -7,10 +7,10 @@ import sys
 
 import pytest
 
-from pyupgrade import _fix_dict_set
 from pyupgrade import _fix_format_literals
 from pyupgrade import _fix_fstrings
 from pyupgrade import _fix_percent_format
+from pyupgrade import _fix_py2_compatible
 from pyupgrade import _fix_six_and_classes
 from pyupgrade import _fix_super
 from pyupgrade import _fix_tokens
@@ -120,7 +120,7 @@ def test_intentionally_not_round_trip(s, expected):
     ),
 )
 def test_sets(s, expected):
-    ret = _fix_dict_set(s)
+    ret = _fix_py2_compatible(s)
     assert ret == expected
 
 
@@ -139,8 +139,8 @@ def test_sets(s, expected):
         ),
     ),
 )
-def test_sets_generators_trailing_comas(s, expected):
-    ret = _fix_dict_set(s)
+def test_sets_generators_trailing_commas(s, expected):
+    ret = _fix_py2_compatible(s)
     assert ret == expected
 
 
@@ -216,7 +216,69 @@ def test_sets_generators_trailing_comas(s, expected):
     ),
 )
 def test_dictcomps(s, expected):
-    ret = _fix_dict_set(s)
+    ret = _fix_py2_compatible(s)
+    assert ret == expected
+
+
+@pytest.mark.parametrize(
+    's',
+    (
+        'x is True',
+        'x is False',
+        'x is None',
+        'x is (not 5)',
+        'x is 5 + 5',
+        # pyupgrade is timid about containers since the original can be
+        # always-False, but the rewritten code could be `True`.
+        'x is ()',
+        'x is []',
+        'x is {}',
+        'x is {1}',
+    ),
+)
+def test_fix_is_compare_to_literal_noop(s):
+    assert _fix_py2_compatible(s) == s
+
+
+@pytest.mark.parametrize(
+    ('s', 'expected'),
+    (
+        pytest.param('x is 5', 'x == 5', id='`is`'),
+        pytest.param('x is not 5', 'x != 5', id='`is not`'),
+        pytest.param('x is ""', 'x == ""', id='string'),
+        pytest.param('x is u""', 'x == u""', id='unicode string'),
+        pytest.param('x is b""', 'x == b""', id='bytes'),
+        pytest.param('x is 1.5', 'x == 1.5', id='float'),
+        pytest.param('x == 5 is 5', 'x == 5 == 5', id='compound compare'),
+        pytest.param(
+            'if (\n'
+            '    x is\n'
+            '    5\n'
+            '): pass\n',
+
+            'if (\n'
+            '    x ==\n'
+            '    5\n'
+            '): pass\n',
+
+            id='multi-line `is`',
+        ),
+        pytest.param(
+            'if (\n'
+            '    x is\n'
+            '    not 5\n'
+            '): pass\n',
+
+            'if (\n'
+            '    x != 5\n'
+            '): pass\n',
+
+            id='multi-line `is not`',
+        ),
+    ),
+)
+def test_fix_is_compare_to_literal(s, expected):
+    ret = _fix_py2_compatible(s)
     assert ret == expected