diff options
author | Yury Selivanov <yury@magic.io> | 2016-09-15 16:50:23 (GMT) |
---|---|---|
committer | Yury Selivanov <yury@magic.io> | 2016-09-15 16:50:23 (GMT) |
commit | 8987c9d219f0efb438f5d707a63d0a0a0f72b3ef (patch) | |
tree | 409543083b30dda1c786a619ab9012b0c9e0483f /Lib/test | |
parent | 67752315979e1501b7088273b5a1aecc90b6fe67 (diff) | |
download | cpython-8987c9d219f0efb438f5d707a63d0a0a0f72b3ef.zip cpython-8987c9d219f0efb438f5d707a63d0a0a0f72b3ef.tar.gz cpython-8987c9d219f0efb438f5d707a63d0a0a0f72b3ef.tar.bz2 |
Issue #26182: Raise DeprecationWarning for improper use of async/await keywords
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_coroutines.py | 137 | ||||
-rw-r--r-- | Lib/test/test_grammar.py | 12 |
2 files changed, 95 insertions, 54 deletions
diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index 154ce7f..f2839a7 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -358,57 +358,110 @@ class AsyncBadSyntaxTest(unittest.TestCase): with self.subTest(code=code), self.assertRaises(SyntaxError): compile(code, "<test>", "exec") - def test_goodsyntax_1(self): - # Tests for issue 24619 + def test_badsyntax_2(self): + samples = [ + """def foo(): + await = 1 + """, + + """class Bar: + def async(): pass + """, - def foo(await): - async def foo(): pass - async def foo(): + """class Bar: + async = 1 + """, + + """class async: pass - return await + 1 - self.assertEqual(foo(10), 11) + """, - def foo(await): - async def foo(): pass - async def foo(): pass - return await + 2 - self.assertEqual(foo(20), 22) + """class await: + pass + """, - def foo(await): + """import math as await""", - async def foo(): pass + """def async(): + pass""", - async def foo(): pass + """def foo(*, await=1): + pass""" - return await + 2 - self.assertEqual(foo(20), 22) + """async = 1""", - def foo(await): - """spam""" - async def foo(): \ - pass - # 123 - async def foo(): pass - # 456 - return await + 2 - self.assertEqual(foo(20), 22) - - def foo(await): - def foo(): pass - def foo(): pass - async def bar(): return await_ - await_ = await - try: - bar().send(None) - except StopIteration as ex: - return ex.args[0] - self.assertEqual(foo(42), 42) + """print(await=1)""" + ] - async def f(): - async def g(): pass - await z - await = 1 - self.assertTrue(inspect.iscoroutinefunction(f)) + for code in samples: + with self.subTest(code=code), self.assertWarnsRegex( + DeprecationWarning, + "'await' will become reserved keywords"): + compile(code, "<test>", "exec") + + def test_badsyntax_3(self): + with self.assertRaises(DeprecationWarning): + with warnings.catch_warnings(): + warnings.simplefilter("error") + compile("async = 1", "<test>", "exec") + + def test_goodsyntax_1(self): + # Tests for issue 24619 + + samples = [ + '''def foo(await): + async def foo(): pass + async def foo(): + pass + return await + 1 + ''', + + '''def foo(await): + async def foo(): pass + async def foo(): pass + return await + 1 + ''', + + '''def foo(await): + + async def foo(): pass + + async def foo(): pass + + return await + 1 + ''', + + '''def foo(await): + """spam""" + async def foo(): \ + pass + # 123 + async def foo(): pass + # 456 + return await + 1 + ''', + + '''def foo(await): + def foo(): pass + def foo(): pass + async def bar(): return await_ + await_ = await + try: + bar().send(None) + except StopIteration as ex: + return ex.args[0] + 1 + ''' + ] + + for code in samples: + with self.subTest(code=code): + loc = {} + + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + exec(code, loc, loc) + + self.assertEqual(loc['foo'](10), 11) class TokenizerRegrTest(unittest.TestCase): diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index 67a61d4..03f2c84 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -1345,18 +1345,6 @@ class GrammarTests(unittest.TestCase): self.assertEqual(m.other, 42) def test_async_await(self): - async = 1 - await = 2 - self.assertEqual(async, 1) - - def async(): - nonlocal await - await = 10 - async() - self.assertEqual(await, 10) - - self.assertFalse(bool(async.__code__.co_flags & inspect.CO_COROUTINE)) - async def test(): def sum(): pass |