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
Add stubs for tree_sitter #8533
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.
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 <Alex.Waygood@Gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
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.
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 <jelle.zijlstra@gmail.com>
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
|
* master: (51 commits) Update pytype_test to be compatible with the latest pytype version. (python#8551) Normalise use of `Never` vs `NoReturn` (python#8549) Add stubs for tree-sitter-languages (python#8548) Add SAFE_RESTARTABLE strategy to ldap3 Connection client_strategy parameter (python#8547) Add stubs for tree_sitter (python#8533) Additional return types for psycopg2 connections (python#8528) fpdf2: fix for 2.5.6 changes (python#8546) cryptography: Rename parameter of serialize_key_and_certificates (python#8543) Add `_threading_local.local.__delattr__` back to the stub (python#8526) Fix `UDPServer` to correctly inherit from `TCPServer` (python#8542) Upgrade pyright (python#8541) Add "Naming convention" to `test_cases/README.md` (python#8521) Improve `.keys()`, `.values()`, `.items()` methods for `TypedDict`s (python#8532) setuptools: fix stubtest (python#8540) Annotate Model in SQLAlchemy (python#8535) Unpin stubtest from 3.10.5 (python#8523) [stubsabot] Bump setuptools to 64.0.* (python#8534) Add `@type_check_only` to two fictional classes in `builtins.pyi` (python#8531) Enable flake8-pyi's Y034 check in `stdlib/typing.pyi` (python#8530) setuptools: delete _distutils.command.check.HAS_DOCUTILS (python#8529) ...
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.