Skip to content

gh-130080: fix warnings in tests #131400

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

Merged
merged 6 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2415,7 +2415,9 @@ def compile_snippet(i):
script = """def func():\n""" + i * snippet
if async_:
script = "async " + script
code = compile(script, "<script>", "exec")
with warnings.catch_warnings():
warnings.simplefilter('ignore', SyntaxWarning)
code = compile(script, "<script>", "exec")
exec(code, ns, ns)
return ns['func'].__code__

Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3011,7 +3011,8 @@ def cls_context_manager(cls, arg: int) -> str:
try:
yield str(arg)
finally:
return 'Done'
pass
return 'Done'

@classmethod_friendly_decorator
@classmethod
Expand All @@ -3027,7 +3028,8 @@ def cls_context_manager(cls, arg: int) -> str:
try:
yield str(arg)
finally:
return 'Done'
pass
return 'Done'

@functools.singledispatchmethod
@classmethod_friendly_decorator
Expand Down
87 changes: 45 additions & 42 deletions Lib/test/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,56 +917,59 @@ def g3():
check_syntax_error(self, "class foo:return 1")

def test_break_in_finally(self):
count = 0
while count < 2:
count += 1
try:
pass
finally:
break
self.assertEqual(count, 1)
with warnings.catch_warnings():
warnings.simplefilter('ignore', SyntaxWarning)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this fix work? The SyntaxWarning is issued when the file is imported so this block hasn't run yet

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I'll refix this.


count = 0
while count < 2:
count += 1
try:
continue
finally:
break
self.assertEqual(count, 1)
count = 0
while count < 2:
count += 1
try:
pass
finally:
break
self.assertEqual(count, 1)

count = 0
while count < 2:
count += 1
try:
1/0
finally:
break
self.assertEqual(count, 1)
count = 0
while count < 2:
count += 1
try:
continue
finally:
break
self.assertEqual(count, 1)

for count in [0, 1]:
count = 0
while count < 2:
count += 1
try:
1/0
finally:
break
self.assertEqual(count, 1)

for count in [0, 1]:
self.assertEqual(count, 0)
try:
pass
finally:
break
self.assertEqual(count, 0)
try:
pass
finally:
break
self.assertEqual(count, 0)

for count in [0, 1]:
for count in [0, 1]:
self.assertEqual(count, 0)
try:
continue
finally:
break
self.assertEqual(count, 0)
try:
continue
finally:
break
self.assertEqual(count, 0)

for count in [0, 1]:
for count in [0, 1]:
self.assertEqual(count, 0)
try:
1/0
finally:
break
self.assertEqual(count, 0)
try:
1/0
finally:
break
self.assertEqual(count, 0)

def test_continue_in_finally(self):
count = 0
Expand Down
41 changes: 17 additions & 24 deletions Lib/test/test_sys_settrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,20 +920,28 @@ def test_try_except_with_wrong_type(self):

def func():
try:
2/0
except IndexError:
4
finally:
return 6
try:
2/0
except IndexError:
5
finally:
7
except:
pass
return 10

self.run_and_compare(func,
[(0, 'call'),
(1, 'line'),
(2, 'line'),
(2, 'exception'),
(3, 'line'),
(6, 'line'),
(6, 'return')])
(3, 'exception'),
(4, 'line'),
(7, 'line'),
(8, 'line'),
(9, 'line'),
(10, 'line'),
(10, 'return')])

def test_finally_with_conditional(self):

Expand Down Expand Up @@ -2228,21 +2236,6 @@ def test_jump_in_nested_finally_3(output):
output.append(11)
output.append(12)

@jump_test(5, 11, [2, 4], (ValueError, 'comes after the current code block'))
def test_no_jump_over_return_try_finally_in_finally_block(output):
try:
output.append(2)
finally:
output.append(4)
output.append(5)
return
try:
output.append(8)
finally:
output.append(10)
pass
output.append(12)

@jump_test(3, 4, [1], (ValueError, 'after'))
def test_no_jump_infinite_while_loop(output):
output.append(1)
Expand Down Expand Up @@ -2766,7 +2759,7 @@ def test_no_jump_over_return_out_of_finally_block(output):
finally:
output.append(4)
output.append(5)
return
return
output.append(7)

@jump_test(7, 4, [1, 6], (ValueError, 'into'))
Expand Down
16 changes: 10 additions & 6 deletions Lib/test/test_unparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pathlib
import random
import tokenize
import warnings
import ast
from test.support.ast_helper import ASTTestMixin

Expand Down Expand Up @@ -839,13 +840,16 @@ def files_to_test(cls):
return items

def test_files(self):
for item in self.files_to_test():
if test.support.verbose:
print(f"Testing {item.absolute()}")
with warnings.catch_warnings():
warnings.simplefilter('ignore', SyntaxWarning)

with self.subTest(filename=item):
source = read_pyfile(item)
self.check_ast_roundtrip(source)
for item in self.files_to_test():
if test.support.verbose:
print(f"Testing {item.absolute()}")

with self.subTest(filename=item):
source = read_pyfile(item)
self.check_ast_roundtrip(source)


if __name__ == "__main__":
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_yield_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -1520,8 +1520,9 @@ def inner():
try:
yield yielded_first
yield yielded_second
finally:
return returned
except:
pass
return returned

def outer():
return (yield from inner())
Expand Down
Loading