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

Add typing information #68

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ jobs:
shell: bash
run: |
CFLAGS="-O0 -g" python setup.py test
- name: Install mypy
if: ${{ !startsWith(matrix.python, 'pypy') }}
run: python -m pip install mypy
- name: Check types
shell: bash
if: ${{ !startsWith(matrix.python, 'pypy') }}
run: |
mypy --config-file mypy.ini tests/test_tree_sitter.py
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ script:
- script/lint
- script/test


matrix:
include:
- python: 3.5
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include README.md
include LICENSE
include tree_sitter/binding.pyi
prune tree_sitter/core
graft tree_sitter/core/lib/src
graft tree_sitter/core/lib/include/tree_sitter
11 changes: 11 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[mypy]
warn_unused_configs= true
warn_incomplete_stub = true

[mypy-tree_sitter.binding]
disallow_incomplete_defs = true
disallow_untyped_defs = true

[mypy-tests.*]
check_untyped_defs = true
strict_optional = false
180 changes: 180 additions & 0 deletions tree_sitter/binding.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
from typing import Optional, List, Tuple, Any

import tree_sitter

# ----------------------------------- Node ----------------------------------- #

class Node:
"""A syntax node"""

def walk(self) -> TreeCursor:
"""Get a tree cursor for walking the tree starting at this node."""
...

def sexp(self) -> str:
"""Get an S-expression representing the node."""
...

def child_by_field_id(self, id: int) -> Optional[Node]:
"""Get child for the given field id."""
...

def child_by_field_name(self, name: str) -> Optional[Node]:
"""Get child for the given field name."""
...

@property
def type(self) -> str:
"""The node's type"""
...
@property
def is_named(self) -> bool:
"""Is this a named node"""
...
@property
def is_missing(self) -> bool:
"""Is this a node inserted by the parser"""
...
@property
def has_changes(self) -> bool:
"""Does this node have text changes since it was parsed"""
...
@property
def has_error(self) -> bool:
"""Does this node contain any errors"""
...
@property
def start_byte(self) -> int:
"""The node's start byte"""
...
@property
def end_byte(self) -> int:
"""The node's end byte"""
...
@property
def start_point(self) -> Tuple[int,int]:
"""The node's start point"""
...
@property
def end_point(self) -> Tuple[int,int]:
"""The node's end point"""
...
@property
def children(self) -> List[Node]:
"""The node's children"""
...
@property
def child_count(self) -> int:
"""The number of children for a node"""
...
@property
def named_child_count(self) -> int:
"""The number of named children for a node"""
...
@property
def next_sibling(self) -> Optional[Node]:
"""The node's next sibling"""
...
@property
def prev_sibling(self) -> Optional[Node]:
"""The node's previous sibling"""
...
@property
def next_named_sibling(self) -> Optional[Node]:
"""The node's next named sibling"""
...
@property
def prev_named_sibling(self) -> Optional[Node]:
"""The node's previous named sibling"""
...
@property
def parent(self) -> Optional[Node]:
"""The node's parent"""
...

class Tree:
"""A Syntax Tree"""

def walk(self) -> TreeCursor:
"""Get a tree cursor for walking this tree."""
...

def edit(self, start_byte: int, old_end_byte: int, new_end_byte: int, start_point: Tuple[int,int], old_end_point: Tuple[int,int], new_end_point: Tuple[int,int]) -> None:
"""Edit the syntax tree."""
...

@property
def root_node(self) -> Node:
"""The root node of this tree."""
...

class TreeCursor:
"""A syntax tree cursor."""

def current_field_name(self) -> Optional[str]:
"""Get the field name of the tree cursor's current node.

If the current node has the field name, return str. Otherwise, return None.
"""
...

def goto_parent(self) -> bool:
"""Go to parent.

If the current node is not the root, move to its parent and
return True. Otherwise, return False.
"""
...

def goto_first_child(self) -> bool:
"""Go to first child.

If the current node has children, move to the first child and
return True. Otherwise, return False.
"""
...

def goto_next_sibling(self) -> bool:
"""Go to next sibling.

If the current node has a next sibling, move to the next sibling
and return True. Otherwise, return False.
"""
...

@property
def node(self) -> Node:
"""The current node."""
...

class Parser:
"""A Parser"""

def parse(self, source_code: bytes, old_tree: Tree = None) -> Tree:
"""Parse source code, creating a syntax tree."""
...

def set_language(self, language: tree_sitter.Language) -> None:
"""Set the parser language."""
...

class Query:
"""A set of patterns to search for in a syntax tree."""

# Not implemented yet. Return type is wrong
def matches(self, node: Node) -> None:
"""Get a list of all of the matches within the given node."""
...

def captures(self, node: Node, start_point: Tuple[int,int] = None, end_point: Tuple[int,int] = None) -> List[Tuple[Node, str]]:
"""Get a list of all of the captures within the given node."""
...


def _language_field_id_for_name(language_id: Any, name: str) -> int:
"""(internal)"""
...

def _language_query(language_id: Any, source: str) -> Query:
"""(internal)"""
...