Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit 558d1da

Browse files
author
Hendrik van Antwerpen
authored
Merge pull request #434 from nohehf/fix/python-nested-function-declaration
fix/python nested function declaration
2 parents ea70811 + b049439 commit 558d1da

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -798,15 +798,21 @@ inherit .parent_module
798798
let @body.class_member_attr_scope = #null
799799
}
800800

801-
(function_definition
802-
parameters: (parameters
803-
. (identifier) @param)
804-
body: (block) @body)
801+
; method definition
802+
(class_definition
803+
body: (block
804+
(function_definition
805+
parameters:
806+
(parameters . (identifier) @first_param)
807+
body: (block) @method_body
808+
)
809+
)
810+
)
805811
{
806-
edge @param.def -> @param.class_self_scope
807-
edge @param.class_member_attr_scope -> @param.output
808-
edge @param.output -> @body.after_scope
809-
attr (@param.output) push_node = @param
812+
edge @first_param.def -> @first_param.class_self_scope
813+
edge @first_param.class_member_attr_scope -> @first_param.output
814+
edge @first_param.output -> @method_body.after_scope
815+
attr (@first_param.output) push_node = @first_param
810816
}
811817

812818
[
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def outer(a):
2+
def inner(b):
3+
pass
4+
5+
inner(1)
6+
# ^ defined: 2
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Foo:
2+
a = 1
3+
4+
def first_method(self):
5+
self.a
6+
# ^ defined: 2
7+
8+
def second_method(self):
9+
self.a
10+
# ^ defined: 2
11+
12+
# First argument here is not self
13+
def not_a_method(not_self):
14+
return not_self.a
15+
# ^ defined:
16+
17+
18+
def function(self):
19+
self.a
20+
# ^ defined:
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Foo:
2+
a = 1
3+
4+
def mathod_1(self):
5+
return self.a
6+
# ^ defined: 2
7+
8+
# Self can be named anything
9+
def method_2(actually_self):
10+
return actually_self.a
11+
# ^ defined: 2

0 commit comments

Comments
 (0)