Skip to content

Commit 9e60d42

Browse files
committed
packagedcode: don't use removed ast module attributes
This is needed for Python 3.14 compatibility. Signed-off-by: Maxwell G <[email protected]>
1 parent 93a2d69 commit 9e60d42

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

src/packagedcode/build.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,14 @@ def parse(cls, location, package_only=False):
163163
args = {}
164164
for kw in statement.value.keywords:
165165
arg_name = kw.arg
166-
if isinstance(kw.value, ast.Str):
167-
args[arg_name] = kw.value.s
166+
if isinstance(kw.value, ast.Constant) and isinstance(kw.value.value, str):
167+
args[arg_name] = kw.value.value
168168

169169
if isinstance(kw.value, ast.List):
170170
# We collect the elements of a list if the element is
171171
# not a function call
172172
args[arg_name] = [
173-
elt.s for elt in kw.value.elts
173+
elt.value for elt in kw.value.elts
174174
if not isinstance(elt, ast.Call)
175175
]
176176
if args:
@@ -351,17 +351,17 @@ def parse(cls, location, package_only=True):
351351
statement_keys = statement.value.keys
352352
statement_values = statement.value.values
353353
for statement_k, statement_v in zip(statement_keys, statement_values):
354-
if isinstance(statement_k, ast.Str):
355-
key_name = statement_k.s
354+
if isinstance(statement_k, ast.Constant) and isinstance(statement_k.value, str):
355+
key_name = statement_k.value
356356
# The list values in a `METADATA.bzl` file seem to only contain strings
357357
if isinstance(statement_v, ast.List):
358358
value = []
359359
for e in statement_v.elts:
360-
if not isinstance(e, ast.Str):
360+
if not (isinstance(e, ast.Constant) and isinstance(e.value, str)):
361361
continue
362-
value.append(e.s)
363-
if isinstance(statement_v, (ast.Str, ast.Constant)):
364-
value = statement_v.s
362+
value.append(e.value)
363+
if isinstance(statement_v, ast.Constant) and isinstance(statement_v.value, str):
364+
value = statement_v.value
365365
metadata_fields[key_name] = value
366366

367367
parties = []

src/packagedcode/pypi.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,8 +2157,8 @@ def get_setup_py_args_legacy(location, include_not_parsable=False):
21572157
]
21582158
values.append(val)
21592159

2160-
elif isinstance(val, (ast.Str, ast.Constant,)):
2161-
values.append(val.s)
2160+
elif isinstance(val, ast.Constant) and isinstance(val.value, str):
2161+
values.append(val.value)
21622162

21632163
else:
21642164
if include_not_parsable:
@@ -2183,8 +2183,8 @@ def get_setup_py_args_legacy(location, include_not_parsable=False):
21832183
mapping = dict(zip(keys, values))
21842184
setup_args[arg_name] = mapping
21852185

2186-
elif isinstance(arg_value, (ast.Str, ast.Constant,)):
2187-
setup_args[arg_name] = arg_value.s
2186+
elif isinstance(arg_value, ast.Constant) and isinstance(arg_value.value, str):
2187+
setup_args[arg_name] = arg_value.value
21882188
else:
21892189
if include_not_parsable:
21902190
if isinstance(arg_value, ast.Attribute):

src/packagedcode/pypi_setup_py.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,9 @@ def node_to_value(node, body):
119119
"""
120120
if node is None:
121121
return
122-
if hasattr(ast, 'Constant'):
123-
if isinstance(node, ast.Constant):
124-
return node.value
125122

126-
if isinstance(node, ast.Str):
127-
return node.s
128-
129-
if isinstance(node, ast.Num):
130-
return node.n
123+
if isinstance(node, ast.Constant):
124+
return node.value
131125

132126
if isinstance(node, (ast.List, ast.Tuple, ast.Set,)):
133127
return [node_to_value(subnode, body) for subnode in node.elts]

0 commit comments

Comments
 (0)