From 7b3ae2ddb2f337373d13714f9d7c6c2a00d21e13 Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Thu, 7 Sep 2023 13:38:24 +0300 Subject: [PATCH] Use ast.Constant instead of ast.Str Fixes multiple deprecation warnings on Python 3.12: - DeprecationWarning: ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead - DeprecationWarning: Attribute s is deprecated and will be removed in Python 3.14; use value instead --- src/check_python_versions/parsers/python.py | 15 +++++---------- tests/parsers/test_python.py | 12 ++++++------ 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/check_python_versions/parsers/python.py b/src/check_python_versions/parsers/python.py index 3102202..ee99fc3 100644 --- a/src/check_python_versions/parsers/python.py +++ b/src/check_python_versions/parsers/python.py @@ -209,11 +209,8 @@ def eval_ast_node( ``keyword`` is used for error reporting. """ - if isinstance(node, ast.Str): - # The assert is needed to placate mypy on Python 3.8 - # https://github.com/python/mypy/issues/8837 - assert isinstance(node.s, str) - return node.s + if isinstance(node, ast.Constant) and isinstance(node.value, str): + return node.value if isinstance(node, (ast.List, ast.Tuple)): values: List[str] = [] warned = False @@ -238,13 +235,11 @@ def eval_ast_node( return tuple(values) return values if (isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute) - and isinstance(node.func.value, ast.Str) + and isinstance(node.func.value, ast.Constant) + and isinstance(node.func.value.value, str) and node.func.attr == 'join'): try: - # The assert is needed to placate mypy on Python 3.8 - # https://github.com/python/mypy/issues/8837 - assert isinstance(node.func.value.s, str) - return node.func.value.s.join(ast.literal_eval(node.args[0])) + return node.func.value.value.join(ast.literal_eval(node.args[0])) except ValueError: pass if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Add): diff --git a/tests/parsers/test_python.py b/tests/parsers/test_python.py index acf2921..20bc0a5 100644 --- a/tests/parsers/test_python.py +++ b/tests/parsers/test_python.py @@ -15,23 +15,23 @@ def test_find_call_kwarg_in_ast(): tree = ast.parse('foo(bar="foo")') node = find_call_kwarg_in_ast(tree, 'foo', 'bar', filename='setup.py') - assert isinstance(node, ast.Str) - assert node.s == "foo" + assert isinstance(node, ast.Constant) + assert node.value == "foo" def test_find_call_kwarg_in_ast_dotted(): tree = ast.parse('mod.foo(bar="gronk")') node = find_call_kwarg_in_ast(tree, 'mod.foo', 'bar', filename='setup.py') - assert isinstance(node, ast.Str) - assert node.s == "gronk" + assert isinstance(node, ast.Constant) + assert node.value == "gronk" def test_find_call_kwarg_in_ast_alternatives(): tree = ast.parse('mod.foo(bar="gronk")') node = find_call_kwarg_in_ast(tree, ['foo', 'mod.foo'], 'bar', filename='a.py') - assert isinstance(node, ast.Str) - assert node.s == "gronk" + assert isinstance(node, ast.Constant) + assert node.value == "gronk" def test_find_call_kwarg_in_ast_no_arg(capsys):