Skip to content
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

fix/python nested function declaration #434

Merged

Conversation

nohehf
Copy link
Contributor

@nohehf nohehf commented May 23, 2024

This fixes #404.
The problem was that the stanza targeted every function first param, not only methods.
As:

let @body.class_self_scope = #null
let @body.class_member_attr_scope = #null

Was run for each function, nested function would try to create an edge to a null value:

  edge @param.def -> @first_param.class_self_scope
  edge @param.class_member_attr_scope -> @first_param.output

I actually think there might be an issue with TSG itself, as class_self_scope and class_member_attr_scope are inherited, setting those to null for the function scope should fallback to a higher parent. I might investigate on this.

@nohehf nohehf requested a review from a team as a code owner May 23, 2024 19:58
Copy link

@Manue1137 Manue1137 left a comment

Choose a reason for hiding this comment

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

Cambio

Copy link
Collaborator

@hendrikvanantwerpen hendrikvanantwerpen left a comment

Choose a reason for hiding this comment

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

Thanks! I left two suggestions, but overall this looks sensible.

@@ -794,19 +794,29 @@ inherit .parent_module
edge @name.def -> @func.call

; Prevent functions defined inside of method bodies from being treated like methods
; There might be a bug here as calling .self_scope after this, will not fallback to the larger scope
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you have an idea what program might trigger this? Even if we don't fix it now, we can document it by adding test and giving it the .skip suffix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does not enters this branch anymore now that we only target method definitions (first level functions in classes). But I started testing stuff on tree-sitter-graph and the following stanzas reproduce this behaviour:

inherit .scope
(module) @mod {
    let @mod.scope = "module"
}

(function_definition) @def {
    let @def.scope = #null
}

(pass_statement) @pass {
    print @pass.scope ; will be null, not module
}
def f():
    pass

I don't think it is an issue anymore in stack-graphs tho (at least for python).
Maybe this is the expected behaviour but then being able to set a property to "undefined" / unset it would be useful. Let's track this in tree-sitter-graph.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure I completely understand the issue, but if this cannot be triggered by a Python program here, let's remove the comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll make sure to open an issue on tree sitter graph, just to keep track of it (as it might be an issue in the future), I removed the comment.
I'm not sure those two lines are needed at all now, but I might have missed something (and it's out of this pr scope I believe)

  ; Prevent functions defined inside of method bodies from being treated like methods
  let @body.class_self_scope = #null
  let @body.class_member_attr_scope = #null

nohehf and others added 2 commits May 27, 2024 14:17
@hendrikvanantwerpen hendrikvanantwerpen merged commit 558d1da into github:main May 27, 2024
8 checks passed
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.

Python TSG: Failing to parse function in function
3 participants