Skip to content

Commit

Permalink
Fix for IndexError in an expression list assignment (#408)
Browse files Browse the repository at this point in the history
There are some cases where the left and right hand sides of the
assignment don't have equal number of arguments. This change ensures the
node children have identical sizes to avoid this condition.

A longer term solution might be to handle star arguments.

Fixes #407

Signed-off-by: Eric Brown <eric.brown@securesauce.dev>
  • Loading branch information
ericwb authored Apr 2, 2024
1 parent ce9e9ed commit 9d071e6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions precli/parsers/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def visit_assignment(self, nodes: list[Node]):
if (
nodes[0].type == tokens.PATTERN_LIST
and nodes[2].type == tokens.EXPRESSION_LIST
and len(nodes[0].named_children) == len(nodes[2].named_children)
):
for i in range(len(nodes[0].named_children)):
self.visit_assignment(
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/parsers/examples/expression_list_assignment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import torch


torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])
x = torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])
b, *_, device = *x.shape, x.device
7 changes: 7 additions & 0 deletions tests/unit/parsers/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ def setUp(self):
"examples",
)

def test_expression_list_assignment(self):
artifact = Artifact(
os.path.join(self.base_path, "expression_list_assignment.py")
)
results = self.parser.parse(artifact)
self.assertEqual(0, len(results))

def test_importlib_import_module(self):
artifact = Artifact(
os.path.join(self.base_path, "importlib_import_module.py")
Expand Down

0 comments on commit 9d071e6

Please sign in to comment.