From 49a5de404bf561a419e77d9b1a486b66d274d7f9 Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Thu, 2 Jan 2025 02:01:53 +0200 Subject: [PATCH] Remove unnecessary sys.version_info checks from tests Our minimum supported Python version 3.9 and these things were added way before it was releases, so there is no need to keep them. --- astor/code_gen.py | 4 +--- astor/rtrip.py | 12 +++--------- tests/test_code_gen.py | 27 +-------------------------- tests/test_misc.py | 2 -- 4 files changed, 5 insertions(+), 40 deletions(-) diff --git a/astor/code_gen.py b/astor/code_gen.py index 39b6114..1a01b86 100644 --- a/astor/code_gen.py +++ b/astor/code_gen.py @@ -687,8 +687,6 @@ def _handle_string_constant(self, node, value, is_joined=False): current_line[0] = current_line[0][str_index:] current_line = ''.join(current_line) - has_ast_constant = sys.version_info >= (3, 6) - if is_joined: # Handle new f-strings. This is a bit complicated, because # the tree can contain subnodes that recurse back to JoinedStr @@ -708,7 +706,7 @@ def recurse(node): if value.format_spec is not None: self.write(':') recurse(value.format_spec) - elif has_ast_constant and isinstance(value, ast.Constant): + elif isinstance(value, ast.Constant): self.write(value.value) else: kind = type(value).__name__ diff --git a/astor/rtrip.py b/astor/rtrip.py index 8b108e7..a1eb286 100755 --- a/astor/rtrip.py +++ b/astor/rtrip.py @@ -22,12 +22,6 @@ dsttree = 'tmp_rtrip' -# TODO: Remove this workaround once we remove version 2 support - - -def out_prep(s, pre_encoded=(sys.version_info[0] == 2)): - return s if pre_encoded else s.encode('utf-8') - def convert(srctree, dsttree=dsttree, readonly=False, dumpall=False, ignore_exceptions=False, fullcomp=False): @@ -90,7 +84,7 @@ def convert(srctree, dsttree=dsttree, readonly=False, dumpall=False, dstfname = os.path.join(dstpath, fname) try: with open(dstfname, 'wb') as f: - f.write(out_prep(dsttxt)) + f.write(dsttxt.encode('utf-8')) except UnicodeEncodeError: badfiles.add(dstfname) @@ -117,12 +111,12 @@ def convert(srctree, dsttree=dsttree, readonly=False, dumpall=False, if not readonly: try: with open(dstfname[:-3] + '.srcdmp', 'wb') as f: - f.write(out_prep(srcdump)) + f.write(srcdump.encode('utf-8')) except UnicodeEncodeError: badfiles.add(dstfname[:-3] + '.srcdmp') try: with open(dstfname[:-3] + '.dstdmp', 'wb') as f: - f.write(out_prep(dstdump)) + f.write(dstdump.encode('utf-8')) except UnicodeEncodeError: badfiles.add(dstfname[:-3] + '.dstdmp') elif dumpall: diff --git a/tests/test_code_gen.py b/tests/test_code_gen.py index 0461b90..21c1a5d 100644 --- a/tests/test_code_gen.py +++ b/tests/test_code_gen.py @@ -182,8 +182,6 @@ def test(a1, a2, b1=j, b2='123', b3={}, b4=[]): pass""" self.assertSrcRoundtrips(source) - @unittest.skipUnless(sys.version_info >= (3, 8, 0, "alpha", 4), - "positional only arguments introduced in Python 3.8") def test_positional_only_arguments(self): source = """ def test(a, b, /, c, *, d, **kwargs): @@ -518,23 +516,6 @@ def test_assignment_expr(self): for case in cases: self.assertAstRoundtripsGtVer(case, (3, 8)) - @unittest.skipUnless(sys.version_info <= (3, 3), - "ast.Name used for True, False, None until Python 3.4") - def test_deprecated_constants_as_name(self): - self.assertAstEqualsSource( - ast.Assign(targets=[ast.Name(id='spam')], value=ast.Name(id='True')), - "spam = True") - - self.assertAstEqualsSource( - ast.Assign(targets=[ast.Name(id='spam')], value=ast.Name(id='False')), - "spam = False") - - self.assertAstEqualsSource( - ast.Assign(targets=[ast.Name(id='spam')], value=ast.Name(id='None')), - "spam = None") - - @unittest.skipUnless(sys.version_info >= (3, 4), - "ast.NameConstant introduced in Python 3.4") def test_deprecated_name_constants(self): self.assertAstEqualsSource( ast.Assign(targets=[ast.Name(id='spam')], value=ast.NameConstant(value=True)), @@ -575,8 +556,6 @@ def test_deprecated_constant_nodes(self): ast.Assign(targets=[ast.Name(id='spam')], value=ast.Str("String")), "spam = 'String'") - @unittest.skipUnless(sys.version_info >= (3, 6), - "ast.Constant introduced in Python 3.6") def test_constant_nodes(self): self.assertAstEqualsSource( ast.Assign(targets=[ast.Name(id='spam')], value=ast.Constant(value=3)), @@ -634,9 +613,6 @@ def test_annassign(self): """ self.assertAstRoundtripsGtVer(source, (3, 6)) - @unittest.skipUnless(sys.version_info >= (3, 6), - "typing and annotated assignment was introduced in " - "Python 3.6") def test_function_typing(self): source = canonical(""" def foo(x : int) ->str: @@ -649,6 +625,7 @@ def foo(x: int) -> str: return i """) self.assertAstEqualsSource(ast.parse(source), target) + self.assertAstRoundtripsGtVer(source, (3, 6)) def test_compile_types(self): code = '(a + b + c) * (d + e + f)\n' @@ -996,8 +973,6 @@ def test_fstring_escaped_braces(self): ''' self.assertSrcRoundtripsGtVer(source, (3, 6)) - @unittest.skipUnless(sys.version_info >= (3, 8, 0, "alpha", 4), - "f-string debugging introduced in Python 3.8") def test_fstring_debugging(self): source = """ x = f'{5=}' diff --git a/tests/test_misc.py b/tests/test_misc.py index 4fe4013..37ce50c 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -12,8 +12,6 @@ class GetSymbolTestCase(unittest.TestCase): - @unittest.skipUnless(sys.version_info >= (3, 5), - "ast.MatMult introduced in Python 3.5") def test_get_mat_mult(self): self.assertEqual('@', astor.get_op_symbol(ast.MatMult()))