Skip to content

Commit

Permalink
Use ast.Constant instead of ast.Str
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mgedmin committed Sep 7, 2023
1 parent 7958bad commit 7b3ae2d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
15 changes: 5 additions & 10 deletions src/check_python_versions/parsers/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
12 changes: 6 additions & 6 deletions tests/parsers/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 7b3ae2d

Please sign in to comment.