From cb301f7fcbfbbe160a60b9839b3df218dab44c98 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sun, 27 Jan 2019 17:16:47 -0800 Subject: [PATCH] Add some negative tests --- Lib/test/test_type_comments.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Lib/test/test_type_comments.py b/Lib/test/test_type_comments.py index d1b032097e55..a50f976fed07 100644 --- a/Lib/test/test_type_comments.py +++ b/Lib/test/test_type_comments.py @@ -198,6 +198,27 @@ class TypeCommentTests(unittest.TestCase): self.assertEqual(arg.type_comment, arg.arg.upper()) assert not todo + def test_inappropriate_type_comments(self): + """Tests for inappropriately-placed type comments. + + These should be silently ignored with type comments off, + but raise SyntaxError with type comments on. + + This is not meant to be exhaustive. + """ + + def check_both_ways(source): + ast.parse(source, type_comments=False) + with self.assertRaises(SyntaxError): + ast.parse(source, type_comments=True) + + check_both_ways("pass # type: int\n") + check_both_ways("x += 1 # type: int\n") + check_both_ways("while True: # type: int\n continue\n") + check_both_ways("while True:\n continue # type: int\n") + check_both_ways("try: # type: int\n pass\nfinally:\n pass\n") + check_both_ways("try:\n pass\nfinally: # type: int\n pass\n") + if __name__ == '__main__': unittest.main() -- 2.47.3