Skip to content

Commit

Permalink
Merge pull request #17809 from github/tausbn/python-fix-kwargs-in-cla…
Browse files Browse the repository at this point in the history
…ss-bases

Python: Fix bug in handling of `**kwargs` in class bases
  • Loading branch information
tausbn authored Oct 23, 2024
2 parents e1e3568 + 9803bbd commit 24ae548
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion python/extractor/tests/parser/class.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
class Foo(int, object, metaclass=type):
class Foo(int, object, metaclass=type, **kwargs):
x = 5
17 changes: 14 additions & 3 deletions python/extractor/tsg-python/python.tsg
Original file line number Diff line number Diff line change
Expand Up @@ -1277,16 +1277,18 @@
attr (@class.inner_scope -> @stmt.node) body = (named-child-index @stmt)
}

; Class.bases - using `(_ !name)` as a proxy for all non-keyword arguments.
; Class.bases - using `(_ !value !name)` as a proxy for all non-keyword arguments.
; In particular, `keyword_argument` nodes have a `name` field, and `dictionary_splat`
; nodes have a `value` field.
(class_definition
superclasses: (argument_list element: (_ !name) @arg)
superclasses: (argument_list element: (_ !value !name) @arg)
) @class
{
edge @class.class_expr -> @arg.node
attr (@class.class_expr -> @arg.node) bases = (named-child-index @arg)
}

; Class.keywords
; Class.keywords of the form `foo=bar`
(class_definition
superclasses: (argument_list element: (keyword_argument) @arg)
) @class
Expand All @@ -1295,6 +1297,15 @@
attr (@class.class_expr -> @arg.node) keywords = (named-child-index @arg)
}

; Class.keywords of the form `**kwargs`
(class_definition
superclasses: (argument_list element: (dictionary_splat) @arg)
) @class
{
edge @class.class_expr -> @arg.node
attr (@class.class_expr -> @arg.node) keywords = (named-child-index @arg)
}

;;;;;; End of Class

;;;;;; Assign statements
Expand Down

0 comments on commit 24ae548

Please sign in to comment.