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

Commit b020128

Browse files
committed
fix: method self
1 parent 5164f4b commit b020128

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(class_definition
2+
body: (
3+
block
4+
(function_definition
5+
parameters:
6+
(parameters
7+
. (identifier) @first_param
8+
)
9+
body: (block) @method_body
10+
)
11+
)
12+
)

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -794,19 +794,29 @@ inherit .parent_module
794794
edge @name.def -> @func.call
795795

796796
; Prevent functions defined inside of method bodies from being treated like methods
797+
; There might be a bug here as calling .self_scope after this, will not fallback to the larger scope
797798
let @body.class_self_scope = #null
798799
let @body.class_member_attr_scope = #null
799800
}
800801

801-
(function_definition
802-
parameters: (parameters
803-
. (identifier) @param)
804-
body: (block) @body)
802+
; method definition
803+
(class_definition
804+
body: (
805+
block
806+
(function_definition
807+
parameters:
808+
(parameters
809+
. (identifier) @first_param
810+
)
811+
body: (block) @method_body
812+
)
813+
)
814+
)
805815
{
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
816+
edge @first_param.def -> @first_param.class_self_scope
817+
edge @first_param.class_member_attr_scope -> @first_param.output
818+
edge @first_param.output -> @method_body.after_scope
819+
attr (@first_param.output) push_node = @first_param
810820
}
811821

812822
[
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
class Foo:
22
a = 1
33

4-
# Self can be named anything
5-
def first_method(actually_self):
6-
return actually_self.a
7-
# ^ defined: 2
4+
def first_method(self):
5+
self.a
6+
# ^ defined: 2
87

98
def second_method(self):
9+
self.a
10+
# ^ defined: 2
11+
1012
# First argument here is not self
1113
def not_a_method(not_self):
1214
return not_self.a
1315
# ^ 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)