-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add stubs for tree_sitter #8533
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
Conversation
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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.
I'm fairly useless on this one as I'm not much of a C programmer, but here's a review for the bit where I'm confident I can read the source code correctly :)
Co-authored-by: Alex Waygood <[email protected]>
Co-authored-by: Alex Waygood <[email protected]>
I didn't read the source code when making this PR, I just experimented on the interactive prompt. But installing py-tree-sitter locally requires you to have a C compiler, so getting it to work at all on Windows isn't easy either :) |
This comment has been minimized.
This comment has been minimized.
2 similar comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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.
I took a look at the C code and have a few pieces of feedback
|
||
@final | ||
class Node: | ||
@property |
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.
There's a bunch of other properties: https://github.com/tree-sitter/py-tree-sitter/blob/7dc704fa37057c685fe2d86611f318146c95c34d/tree_sitter/binding.c#L501. I didn't check them all but you're missing at least id
and named_children
. But we don't necessarily need to include those if they're undocumented.
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.
These don't exist in the latest released version of py-tree-sitter (0.20.0).
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.
That'll teach me to look at the master branch :)
@property | ||
def type(self) -> str: ... | ||
__hash__: ClassVar[None] # type: ignore[assignment] | ||
def child_by_field_id(self, __id: int) -> Node | None: ... |
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.
There are more methods: children_by_field_id, children_by_field_name, field_name_for_child
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.
They are not in the latest released version.
class Parser: | ||
# At runtime, Parser(1, 2, 3) ignores the arguments, but that's most likely buggy code | ||
def __init__(self) -> None: ... | ||
def parse(self, source: bytes, old_tree: Tree | None = ..., keep_text: bool = ...) -> Tree: ... |
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.
Source can be any ReadableBuffer, or a Callable: https://github.com/tree-sitter/py-tree-sitter/blob/7dc704fa37057c685fe2d86611f318146c95c34d/tree_sitter/binding.c#L960
Looking at https://github.com/tree-sitter/py-tree-sitter/blob/7dc704fa37057c685fe2d86611f318146c95c34d/tree_sitter/binding.c#L895 it seems to be a Callable[[int, tuple[int, int]], bytes]
.
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.
Version in pypi is more strict:
>>> parser.parse(source=print)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: First argument to parse must be bytes
>>> parser.parse(source=bytearray(b'asd'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: First argument to parse must be bytes
def current_field_name(self) -> None | Incomplete: ... | ||
def goto_first_child(self) -> bool: ... | ||
def goto_next_sibling(self) -> bool: ... | ||
def goto_parent(self) -> bool: ... |
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.
def goto_parent(self) -> bool: ... | |
def goto_parent(self) -> bool: ... | |
def copy(self) -> TreeCursor: ... |
One more
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.
Doesn't exist in the released version.
Co-authored-by: Jelle Zijlstra <[email protected]>
This comment has been minimized.
This comment has been minimized.
6 similar comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: porcupine (https://github.com/Akuli/porcupine)
+ porcupine/plugins/highlight/tree_sitter_highlighter.py:20: error: Unused "type: ignore" comment
|
tree-sitter is a library for parsing various languages. I just merged a pull request that uses it into https://github.com/Akuli/porcupine/, so we should have a basic level of mypy_primer coverage.