Skip to content

Commit 7bc8788

Browse files
committed
fix(pip): handle None entries in AST list fields
Python's AST represents dict unpacking with None in Dict.keys. The existing code assumed that list fields only contain AST nodes and would therefore crash when encountering None. Seen in the wild with lxml: https://github.com/lxml/lxml/blob/283d02ec8966c0e99f4666dc7bdd936479e97246/setup.py#L98 Signed-off-by: Taylor Madore <tmadore@redhat.com>
1 parent 66290de commit 7bc8788

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

hermeto/core/package_managers/pip/project_files.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,12 @@ def _find_setup_call(self, root_node: ast.AST) -> tuple[ast.Call | None, list[AS
585585
if setup_call is not None:
586586
setup_path.append(ASTPathElement(root_node, name))
587587
return setup_call, setup_path
588-
# Value is a list of nodes (use any(), nodes will never be mixed with non-nodes)
588+
# Value is a list that contains AST nodes. Some lists also contain None
589+
# (e.g. Dict.keys uses None to represent ** unpacking).
589590
elif isinstance(value, list) and any(isinstance(x, ast.AST) for x in value):
590591
for i, node in enumerate(value):
592+
if not isinstance(node, ast.AST):
593+
continue
591594
setup_call, setup_path = self._find_setup_call(node)
592595
if setup_call is not None:
593596
setup_path.append(ASTPathElement(root_node, name, i))

tests/unit/package_managers/pip/test_project_files.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,21 @@ def test_exists(self, exists: bool, rooted_tmp_path: RootedPath) -> None:
10131013
[],
10141014
id="none",
10151015
),
1016+
pytest.param(
1017+
dedent(
1018+
"""
1019+
from setuptools import setup
1020+
extra = {'bar': False}
1021+
opts = {
1022+
'foo': True,
1023+
**extra,
1024+
}
1025+
setup(setup_requires=["maturin==0.14.0"], **opts)
1026+
"""
1027+
),
1028+
["maturin==0.14.0"],
1029+
id="dict-unpacking",
1030+
),
10161031
],
10171032
)
10181033
def test_get_setup_requires(

0 commit comments

Comments
 (0)