Skip to content

Commit 7b3ae2d

Browse files
committed
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
1 parent 7958bad commit 7b3ae2d

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

src/check_python_versions/parsers/python.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,8 @@ def eval_ast_node(
209209
210210
``keyword`` is used for error reporting.
211211
"""
212-
if isinstance(node, ast.Str):
213-
# The assert is needed to placate mypy on Python 3.8
214-
# https://github.com/python/mypy/issues/8837
215-
assert isinstance(node.s, str)
216-
return node.s
212+
if isinstance(node, ast.Constant) and isinstance(node.value, str):
213+
return node.value
217214
if isinstance(node, (ast.List, ast.Tuple)):
218215
values: List[str] = []
219216
warned = False
@@ -238,13 +235,11 @@ def eval_ast_node(
238235
return tuple(values)
239236
return values
240237
if (isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute)
241-
and isinstance(node.func.value, ast.Str)
238+
and isinstance(node.func.value, ast.Constant)
239+
and isinstance(node.func.value.value, str)
242240
and node.func.attr == 'join'):
243241
try:
244-
# The assert is needed to placate mypy on Python 3.8
245-
# https://github.com/python/mypy/issues/8837
246-
assert isinstance(node.func.value.s, str)
247-
return node.func.value.s.join(ast.literal_eval(node.args[0]))
242+
return node.func.value.value.join(ast.literal_eval(node.args[0]))
248243
except ValueError:
249244
pass
250245
if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Add):

tests/parsers/test_python.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@
1515
def test_find_call_kwarg_in_ast():
1616
tree = ast.parse('foo(bar="foo")')
1717
node = find_call_kwarg_in_ast(tree, 'foo', 'bar', filename='setup.py')
18-
assert isinstance(node, ast.Str)
19-
assert node.s == "foo"
18+
assert isinstance(node, ast.Constant)
19+
assert node.value == "foo"
2020

2121

2222
def test_find_call_kwarg_in_ast_dotted():
2323
tree = ast.parse('mod.foo(bar="gronk")')
2424
node = find_call_kwarg_in_ast(tree, 'mod.foo', 'bar', filename='setup.py')
25-
assert isinstance(node, ast.Str)
26-
assert node.s == "gronk"
25+
assert isinstance(node, ast.Constant)
26+
assert node.value == "gronk"
2727

2828

2929
def test_find_call_kwarg_in_ast_alternatives():
3030
tree = ast.parse('mod.foo(bar="gronk")')
3131
node = find_call_kwarg_in_ast(tree, ['foo', 'mod.foo'], 'bar',
3232
filename='a.py')
33-
assert isinstance(node, ast.Str)
34-
assert node.s == "gronk"
33+
assert isinstance(node, ast.Constant)
34+
assert node.value == "gronk"
3535

3636

3737
def test_find_call_kwarg_in_ast_no_arg(capsys):

0 commit comments

Comments
 (0)