Skip to content

Fix superclass keyword arguments in Python. #480

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 keyword arguments in its superclasses: (argument_list …), which somehow modify the superclass. The stanza capturing superclasses was erroneously capturing these keyword arguments as superclasses themselves as well, which would then cause an error upon emitting the edge:

0: Error executing statement edge @class.super_scope -> @superclass.output at (938, 3)
    src/stack-graphs.tsg:938:3:
    938 |   edge @class.super_scope -> @superclass.output
        |   ^
    in stanza
    src/stack-graphs.tsg:934:1:
    934 | (class_definition
        | ^
    matching (class_definition) node
    test/superclasses.py:11:1:
    11 | class B(A, foo="Bar"):
        | ^
1: Evaluating edge sink
2: Undefined scoped variable [syntax node keyword_argument (11, 12)].output

The fragment of the parsed Tree-sitter tree for such a superclass argument list with keyword arguments is:

superclasses: (argument_list
  (identifier)
  (keyword_argument …))

As of v0.23.5 of the Python grammar, the superclasses field is indeed an argument_list syntax node, which in turn can contain either an expression or a keyword_argument (among other things), and expression in turn can be a whole host of types of syntax nodes.

Explicitly capturing only identifier and attribute (which are dotted identifier chains like foo.bar) syntax nodes fixes the issue. This is possibly too strict, since technically any expression can be used here, but it likely captures the vast majority of cases out there.

@Copilot Copilot AI review requested due to automatic review settings April 2, 2025 01:06
@bm-w bm-w requested review from a team as code owners April 2, 2025 01:06
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 how Python superclass keyword arguments are handled by updating the captured nodes to only include valid superclass expressions.

  • Updated test file to include a subclass with a keyword argument in its superclass list.
  • Adjusted inline comments to reflect the updated definition line numbers.
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