Skip to content

Fix typed splat function parameters in Python. #478

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 1, 2025

This addresses issue #476.

Eliding the identifier node type in the stanza query allows it to erroneously match list_splat_pattern or dictionary_splat_pattern when those appear within a typed_parameter. This causes the stack graph generation to fail with an error:

0: Error executing statement edge @name.def -> @param.param_name at (871, 3)
    src/stack-graphs.tsg:871:3:
    871 |   edge @name.def -> @param.param_name
        |   ^
    in stanza
    src/stack-graphs.tsg:861:1:
    861 | [
        | ^
    matching (typed_parameter) node
    test/typed_splat_parameters.py:3:9:
    5 | def foo(*bar: typing.Any):
        |         ^
1: Evaluating edge source
2: Undefined scoped variable [syntax node list_splat_pattern (3, 9)].def

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

(typed_parameter [3, 9] - [3, 25])
  (list_splat_pattern [3, 9] - [3, 13])
    (identifier [3, 10] - [3, 13]))
  type: (…))

As of v0.23.5 of the Python grammar, typed_parameter may contain identifier or either of the *_splat_pattern node types. As such, making the identifier node type explicit fixes the issue precisely without introducing other problems.

@Copilot Copilot AI review requested due to automatic review settings April 1, 2025 20:16
@bm-w bm-w requested review from a team as code owners April 1, 2025 20:16
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 the Tree-sitter Python grammar by explicitly eliding the identifier node type in typed_parameter nodes. The changes ensure that list_splat_pattern and dictionary_splat_pattern are not erroneously matched, resolving stack graph generation errors.

  • Updated tests to validate proper behavior for typed splat function parameters.
  • Added two test cases to confirm correct handling of both *args and **kwargs.
Files not reviewed (1)
  • languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg: Language not supported

@bm-w bm-w force-pushed the bm-w/fix-python-typed-splats branch from a843137 to 07d950a Compare April 1, 2025 20:20
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