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

@ -1,5 +1,6 @@
import contextlib
import io
import warnings
import unittest
from unittest.mock import patch
from textwrap import dedent
@ -273,3 +274,28 @@ def test_incomplete_statement(self):
code = "if foo:"
console = InteractiveColoredConsole(namespace, filename="<stdin>")
self.assertTrue(_more_lines(console, code))
class TestWarnings(unittest.TestCase):
def test_pep_765_warning(self):
"""
Test that a SyntaxWarning emitted from the
AST optimizer is only shown once in the REPL.
"""
# gh-131927
console = InteractiveColoredConsole()
code = dedent("""\
def f():
try:
return 1
finally:
return 2
""")
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
console.runsource(code)
count = sum("'return' in a 'finally' block" in str(w.message)
for w in caught)
self.assertEqual(count, 1)