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

Make match captures a dict[str,Node|list[Node]] #165

Merged
merged 3 commits into from
Feb 26, 2024

Conversation

jhandley
Copy link
Contributor

@jhandley jhandley commented Oct 8, 2023

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

@amaanq
Copy link
Member

amaanq commented Nov 12, 2023

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?

@jhandley
Copy link
Contributor Author

jhandley commented Nov 12, 2023

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.

@amaanq
Copy link
Member

amaanq commented Nov 13, 2023

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)

@jhandley jhandley marked this pull request as ready for review November 14, 2023 12:08
@jhandley
Copy link
Contributor Author

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.

@ObserverOfTime
Copy link
Member

@jhandley Are you available to rebase this PR once #159 is merged?

@jhandley
Copy link
Contributor Author

@jhandley Are you available to rebase this PR once #159 is merged?

Sure. Will do.

@ObserverOfTime
Copy link
Member

ObserverOfTime commented Feb 26, 2024

Also note that | unions are a Python 3.9 feature and py-tree-sitter will continue to support 3.8 until the next version.
Same for list[] and dict[].

@ObserverOfTime ObserverOfTime linked an issue Feb 26, 2024 that may be closed by this pull request
@amaanq amaanq force-pushed the prepare-bindings branch 3 times, most recently from 52b2991 to a074346 Compare February 26, 2024 13:28
@amaanq
Copy link
Member

amaanq commented Feb 26, 2024

feel free to rebase now @jhandley, sorry for taking so long to get to #159

@jhandley jhandley changed the base branch from prepare-bindings to master February 26, 2024 18:52
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.
Copy link
Member

@ObserverOfTime ObserverOfTime left a 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.

@ObserverOfTime ObserverOfTime merged commit c1d1126 into tree-sitter:master Feb 26, 2024
13 checks passed
@Akuli
Copy link
Contributor

Akuli commented Feb 26, 2024

Also note that | unions are a Python 3.9 feature and py-tree-sitter will continue to support 3.8 until the next version.
Same for list[] and dict[].

This is true, but there's still a way for Python 3.8 compatible projects to use list[], dict[], and | unions.

Adding from __future__ import annotations to the beginning of the file makes Python ignore all type annotations (more specifically, make them strings implicitly), so that you can use list[], dict[] and | unions even though Python wouldn't understand them. Type checkers, IDEs and other typing tools know this.

@sgoai
Copy link

sgoai commented Sep 14, 2024

Did the docs never get updated for this?

@maxbrunsfeld
Copy link
Contributor

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.

@ObserverOfTime
Copy link
Member

Did the docs never get updated for this?

That is true. I'll update them soon.

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 values are always lists now.

def captures(
self,
node: Node,
/,
predicate: QueryPredicate | None = None
) -> dict[str, list[Node]]: ...
def matches(
self,
node: Node,
/,
predicate: QueryPredicate | None = None
) -> list[tuple[int, dict[str, list[Node]]]]: ...

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.

Captures are not grouped
6 participants