gh-131927: Do not emit PEP 765 warnings in ast.parse() (GH-139642)

ast.parse() no longer emits syntax warnings for
return/break/continue in finally (see PEP-765) -- they are only
emitted during compilation.
This commit is contained in:
Serhiy Storchaka 2025-10-30 13:00:42 +02:00 committed by GitHub
parent 2a904263aa
commit ad0a3f733b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 98 additions and 61 deletions

View file

@ -1745,6 +1745,66 @@ def test_compile_warning_in_finally(self):
self.assertEqual(wm.category, SyntaxWarning)
self.assertIn("\"is\" with 'int' literal", str(wm.message))
@support.subTests('src', [
textwrap.dedent("""
def f():
try:
pass
finally:
return 42
"""),
textwrap.dedent("""
for x in y:
try:
pass
finally:
break
"""),
textwrap.dedent("""
for x in y:
try:
pass
finally:
continue
"""),
])
def test_pep_765_warnings(self, src):
with self.assertWarnsRegex(SyntaxWarning, 'finally'):
compile(src, '<string>', 'exec')
with warnings.catch_warnings():
warnings.simplefilter("error")
tree = ast.parse(src)
with self.assertWarnsRegex(SyntaxWarning, 'finally'):
compile(tree, '<string>', 'exec')
@support.subTests('src', [
textwrap.dedent("""
try:
pass
finally:
def f():
return 42
"""),
textwrap.dedent("""
try:
pass
finally:
for x in y:
break
"""),
textwrap.dedent("""
try:
pass
finally:
for x in y:
continue
"""),
])
def test_pep_765_no_warnings(self, src):
with warnings.catch_warnings():
warnings.simplefilter("error")
compile(src, '<string>', 'exec')
class TestBooleanExpression(unittest.TestCase):
class Value: