Skip to content

Fix lambdas without params. in Python. #479

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

A lambda without parameters does not have a syntax for the parameters, unlike a function without parameters (which does, containing just the ( and ) characters). The stanza for callables did not optionally match @params, causing it to miss lambdas without parameters entirely, which would then cause an error in a later stanza:

0: Error executing statement edge @lam.output -> @lam.call at (984, 3)
    src/stack-graphs.tsg:984:3:
    984 |   edge @lam.output -> @lam.call
        |   ^
    in stanza
    src/stack-graphs.tsg:975:1:
    975 | (lambda
        | ^
    matching (lambda) node
    test/lambdas.py:6:7:
    6 | foo = lambda: print(y)
        |       ^
1: Evaluating edge sink
2: Undefined scoped variable [syntax node lambda (6, 7)].call

The fragments of the parsed Tree-sitter tree for such lambda without and with parameters is:

(lambda [6, 7] - [6, 23])
  body: …)
(lambda [9, 7] - [9, 32])
  parameters: (lambda parameters …)
  body: …)

As of v0.23.5 of the Python grammar, lambda indeed declares an optional parameters field.

Making the @params capture optional and only emitting the edges if it’s there fixes the issue.

@Copilot Copilot AI review requested due to automatic review settings April 2, 2025 00:30
@bm-w bm-w requested review from a team as code owners April 2, 2025 00:30
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 an issue with lambdas without parameters by making the capture of @params optional, ensuring that call edges are only emitted if parameters are present.

  • Added a test case for lambdas without parameters.
  • Verified lambdas with parameters continue to work as expected.
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