-
Notifications
You must be signed in to change notification settings - Fork 106
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
Make match captures a dict[str,Node|list[Node]] #165
Make match captures a dict[str,Node|list[Node]] #165
Conversation
Although I agree it'd be nicer to index a Node/Nodes by the capture name, this is a breaking change. Are there any other benefits to use a dict besides that? |
This only changes the API for the Query.matches method, not the Query.captures method. Given that matches has not been implemented yet in the main branch it shouldn't break anything. The benefit to using the dict is just a simpler API. At least in the code I've written so far, I always want to look up using the name so this avoids writing extra utility functions to search through the list. So it is just a convenience and not critical. If leaving this out means getting the matches PR (#159) merged faster that it is fine with me. I can work around the lack of dict easily but the workaround for not having Query.matches is not so easy. |
Oops sorry, I just noticed it was for matches, yeah this is totally fine It's currently a draft, if you think it's ready let me know (unless you intend to change some stuff still) |
I marked it as ready. Note that is against the tree-sitter:prepare-bindings branch, not master. Let me know if I can help get the matches PR over the finish line. Having matches would be a big help for our current project so I can put some time into it if it would help. |
92a8d3b
to
9b8fb19
Compare
855b6a3
to
3ea0780
Compare
Also note that |
52b2991
to
a074346
Compare
eb33be6
to
01c507e
Compare
661afb2
to
863cd84
Compare
Return the captures of a match as a dict where the key is the capture name and the value is a Node or a list of Nodes. For regular captures, the value is a Node. For captures that can contain multiple nodes because they use a * or + quantifier, the value is a list of Nodes.
863cd84
to
3599d89
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. This looks good.
This is true, but there's still a way for Python 3.8 compatible projects to use Adding |
Did the docs never get updated for this? |
Hi, sorry for chiming late here. I don’t think this is an improvement. Having to check, for every usage, whether the values in the dictionary are lists or single nodes is going to make the API more error prone and verbose. The captures within a match have a meaningful order to them, so I think a list of tuples makes more sense than this dictionary of maybe-lists-maybe-nodes. |
That is true. I'll update them soon.
The values are always lists now. py-tree-sitter/tree_sitter/__init__.pyi Lines 282 to 293 in e164972
|
Return the captures of a match as a dict where the key is the capture name and the value is a Node or a list of Nodes. For regular captures, the value is a Node. For captures that can contain multiple nodes because they use a * or + quantifier, the value is a list of Nodes.
Closes #182