From: Guido van Rossum Date: Mon, 28 Jan 2019 01:16:47 +0000 (-0800) Subject: Add some negative tests X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cb301f7fcbfbbe160a60b9839b3df218dab44c98;p=thirdparty%2FPython%2Fcpython.git Add some negative tests --- 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()