diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-01-18 05:47:48 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-18 05:47:48 (GMT) |
commit | 3bcbedc9f1471d957a30a90f9d1251516b422416 (patch) | |
tree | 8011052f704ebd4db1a1b77d04f9dc831bf35d2e /Lib | |
parent | e55cf024cae203f63b4f78f1b21c1375fe424441 (diff) | |
download | cpython-3bcbedc9f1471d957a30a90f9d1251516b422416.zip cpython-3bcbedc9f1471d957a30a90f9d1251516b422416.tar.gz cpython-3bcbedc9f1471d957a30a90f9d1251516b422416.tar.bz2 |
bpo-34850: Emit a warning for "is" and "is not" with a literal. (GH-9642)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_ast.py | 11 | ||||
-rw-r--r-- | Lib/test/test_grammar.py | 36 |
2 files changed, 36 insertions, 11 deletions
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index 2c8d8ab..897e705 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -1146,11 +1146,12 @@ class ASTValidatorTests(unittest.TestCase): tests = [fn for fn in os.listdir(stdlib) if fn.endswith(".py")] tests.extend(["test/test_grammar.py", "test/test_unpack_ex.py"]) for module in tests: - fn = os.path.join(stdlib, module) - with open(fn, "r", encoding="utf-8") as fp: - source = fp.read() - mod = ast.parse(source, fn) - compile(mod, fn, "exec") + with self.subTest(module): + fn = os.path.join(stdlib, module) + with open(fn, "r", encoding="utf-8") as fp: + source = fp.read() + mod = ast.parse(source, fn) + compile(mod, fn, "exec") class ConstantTests(unittest.TestCase): diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index 3d8b151..3ed19ff 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -1226,11 +1226,33 @@ class GrammarTests(unittest.TestCase): if 1 > 1: pass if 1 <= 1: pass if 1 >= 1: pass - if 1 is 1: pass - if 1 is not 1: pass + if x is x: pass + if x is not x: pass if 1 in (): pass if 1 not in (): pass - if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass + if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in x is x is not x: pass + + def test_comparison_is_literal(self): + def check(test, msg='"is" with a literal'): + with self.assertWarnsRegex(SyntaxWarning, msg): + compile(test, '<testcase>', 'exec') + with warnings.catch_warnings(): + warnings.filterwarnings('error', category=SyntaxWarning) + with self.assertRaisesRegex(SyntaxError, msg): + compile(test, '<testcase>', 'exec') + + check('x is 1') + check('x is "thing"') + check('1 is x') + check('x is y is 1') + check('x is not 1', '"is not" with a literal') + + with warnings.catch_warnings(): + warnings.filterwarnings('error', category=SyntaxWarning) + compile('x is None', '<testcase>', 'exec') + compile('x is False', '<testcase>', 'exec') + compile('x is True', '<testcase>', 'exec') + compile('x is ...', '<testcase>', 'exec') def test_binary_mask_ops(self): x = 1 & 1 @@ -1520,9 +1542,11 @@ class GrammarTests(unittest.TestCase): self.assertEqual(16 // (4 // 2), 8) self.assertEqual((16 // 4) // 2, 2) self.assertEqual(16 // 4 // 2, 2) - self.assertTrue(False is (2 is 3)) - self.assertFalse((False is 2) is 3) - self.assertFalse(False is 2 is 3) + x = 2 + y = 3 + self.assertTrue(False is (x is y)) + self.assertFalse((False is x) is y) + self.assertFalse(False is x is y) def test_matrix_mul(self): # This is not intended to be a comprehensive test, rather just to be few |