Skip to content

Fix nested assignments in Python. #481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bm-w
Copy link

@bm-w bm-w commented Apr 2, 2025

Python supports “nested” assignments like x = y = 42. In that particular context the nested y = 42 assignment sort of acts like an expression, and the result is bound to the LHS of the first assignment: x = <result>. However, assignments are not generally considered expressions, and as such the TSSG definition does not create an output node for it, causing an error in the the stanza that deals with assignments:

0: Error executing statement edge @pattern.input -> @value.output at (1299, 3)
     src/stack-graphs.tsg:1299:3:
     1299 |   edge @pattern.input -> @value.output
          |   ^
     in stanza
     src/stack-graphs.tsg:1288:1:
     1288 | [
          | ^
     matching (assignment) node
     test/assignments.py:6:1:
     6 | y = z = 1337
       | ^
1: Evaluating edge sink
2: Undefined scoped variable [syntax node assignment (6, 5)].output

The fragment of the parsed Tree-sitter tree for such a nested assignment is:

(assignment
  left: (…)
  right: (assignment
    left: (…)
    right: (…)))

As of v0.23.5 of the Python grammar, the right field of an assignment indeed accepts another assignment (via _right_hand_side).

Simply creating an output node for an assignment fixes the issue. Copious testing reveals no negative side effects.

@Copilot Copilot AI review requested due to automatic review settings April 2, 2025 14:25
@bm-w bm-w requested review from a team as code owners April 2, 2025 14:25
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes the issue with nested assignments in Python by creating an output node for assignment expressions. The changes ensure that nested assignments like "y = z = 1337" are handled correctly, and they add tests to verify the fix.

Files not reviewed (1)
  • languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg: Language not supported

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant