From b6aa4bcd8249bc1e2d2ecdae6d83927a370e8062 Mon Sep 17 00:00:00 2001 From: sunmy2019 <59365878+sunmy2019@users.noreply.github.com> Date: Thu, 4 May 2023 01:12:00 +0800 Subject: [PATCH 1/4] catch DeprecationWarning in `test_fstring` --- Lib/test/test_fstring.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index be71fde5aaba54..264c4650bb84d3 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -16,6 +16,7 @@ from test import support from test.support.os_helper import temp_cwd from test.support.script_helper import assert_python_failure +from test.support.warnings_helper import check_warnings a_global = 'global variable' @@ -33,6 +34,11 @@ def assertAllRaise(self, exception_type, regex, error_strings): with self.assertRaisesRegex(exception_type, regex): eval(str) + def assertEqualWithDeprecationWarning(self, string, expected): + with check_warnings(('', DeprecationWarning), quiet=False): + s = eval(string) + self.assertEqual(s, expected) + def test__format__lookup(self): # Make sure __format__ is looked up on the type, not the instance. class X: @@ -980,11 +986,11 @@ def test_roundtrip_raw_quotes(self): self.assertEqual(fr'\"\'\"\'', '\\"\\\'\\"\\\'') def test_fstring_backslash_before_double_bracket(self): - self.assertEqual(f'\{{\}}', '\\{\\}') - self.assertEqual(f'\{{', '\\{') - self.assertEqual(f'\{{{1+1}', '\\{2') - self.assertEqual(f'\}}{1+1}', '\\}2') - self.assertEqual(f'{1+1}\}}', '2\\}') + self.assertEqualWithDeprecationWarning(r"f'\{{\}}'", '\\{\\}') + self.assertEqualWithDeprecationWarning(r"f'\{{'", '\\{') + self.assertEqualWithDeprecationWarning(r"f'\{{{1+1}'", '\\{2') + self.assertEqualWithDeprecationWarning(r"f'\}}{1+1}'", '\\}2') + self.assertEqualWithDeprecationWarning(r"f'{1+1}\}}'", '2\\}') self.assertEqual(fr'\{{\}}', '\\{\\}') self.assertEqual(fr'\{{', '\\{') self.assertEqual(fr'\{{{1+1}', '\\{2') From a06b18287e05a542ab8239b63c407168245e2164 Mon Sep 17 00:00:00 2001 From: sunmy2019 <59365878+sunmy2019@users.noreply.github.com> Date: Thu, 4 May 2023 14:18:49 +0800 Subject: [PATCH 2/4] Update Lib/test/test_fstring.py Co-authored-by: Alex Waygood --- Lib/test/test_fstring.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index 264c4650bb84d3..6d894016f10e13 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -986,11 +986,18 @@ def test_roundtrip_raw_quotes(self): self.assertEqual(fr'\"\'\"\'', '\\"\\\'\\"\\\'') def test_fstring_backslash_before_double_bracket(self): - self.assertEqualWithDeprecationWarning(r"f'\{{\}}'", '\\{\\}') - self.assertEqualWithDeprecationWarning(r"f'\{{'", '\\{') - self.assertEqualWithDeprecationWarning(r"f'\{{{1+1}'", '\\{2') - self.assertEqualWithDeprecationWarning(r"f'\}}{1+1}'", '\\}2') - self.assertEqualWithDeprecationWarning(r"f'{1+1}\}}'", '2\\}') + deprecated_cases = [ + (r"f'\{{\}}'", '\\{\\}'), + (r"f'\{{'", '\\{'), + (r"f'\{{{1+1}'", '\\{2'), + (r"f'\}}{1+1}'", '\\}2'), + (r"f'{1+1}\}}'", '2\\}') + ] + for case, expected_result in deprecated_cases: + with self.subTest(case=case, expected_result=expected_result): + with self.assertWarns(DeprecationWarning): + result = eval(case) + self.assertEqual(result, expected_result) self.assertEqual(fr'\{{\}}', '\\{\\}') self.assertEqual(fr'\{{', '\\{') self.assertEqual(fr'\{{{1+1}', '\\{2') From cc232eb4800e27c2b174f837516e9055dd7305e0 Mon Sep 17 00:00:00 2001 From: sunmy2019 <59365878+sunmy2019@users.noreply.github.com> Date: Thu, 4 May 2023 14:20:15 +0800 Subject: [PATCH 3/4] remove unused method --- Lib/test/test_fstring.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index 6d894016f10e13..6ccd0f9e93a318 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -34,11 +34,6 @@ def assertAllRaise(self, exception_type, regex, error_strings): with self.assertRaisesRegex(exception_type, regex): eval(str) - def assertEqualWithDeprecationWarning(self, string, expected): - with check_warnings(('', DeprecationWarning), quiet=False): - s = eval(string) - self.assertEqual(s, expected) - def test__format__lookup(self): # Make sure __format__ is looked up on the type, not the instance. class X: From 426b94d222ef18883589281c1bde59af66d87a54 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 4 May 2023 07:31:09 +0100 Subject: [PATCH 4/4] Unused import --- Lib/test/test_fstring.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index 6ccd0f9e93a318..58e2550715cecf 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -16,7 +16,6 @@ from test import support from test.support.os_helper import temp_cwd from test.support.script_helper import assert_python_failure -from test.support.warnings_helper import check_warnings a_global = 'global variable'