-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-131927: Prevent emitting optimizer warnings twice in the REPL #131993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2bd0582
08eab52
2049da3
a922e16
742a4b1
bbefbf8
d047dd3
c4a27bc
7f8d1af
c15635b
97d028e
a8fa07f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import contextlib | ||
import io | ||
import unittest | ||
import warnings | ||
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("default") | ||
console.runsource(code) | ||
|
||
count = sum("'return' in a 'finally' block" in str(w.message) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use "'return' in a 'finally' block" may be an error in future versions. Maybe use some more long living warnings, like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hmm I don't think I can, unless I want to match the warning message exactly?
I can't if I want to test the new repl. The PEP 765 warning is the only one emitted from the ast optimizer which is needed to trigger this behaviour in the repl. However this does change the behaviour of all warnings that use |
||
for w in caught) | ||
self.assertEqual(count, 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if set it to "always"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It emits twice, I added a test to
test_compile
for it