Skip to content

Commit

Permalink
feat: deprecate old binding methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ObserverOfTime committed Feb 25, 2024
1 parent e934613 commit 9b8fb19
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 164 deletions.
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Py-Tree-sitter
"""
"""Py-Tree-sitter"""

from platform import system

Expand All @@ -22,6 +20,9 @@
"tree_sitter/core/lib/include",
"tree_sitter/core/lib/src"
],
define_macros=[
("PY_SSIZE_T_CLEAN", None)
],
extra_compile_args=(
["-std=gnu11", "-Wno-unused-variable"] if system() != "Windows" else None
),
Expand Down
33 changes: 20 additions & 13 deletions tree_sitter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from platform import system
from tempfile import TemporaryDirectory
from typing import Callable, List, Optional, Union
from warnings import warn

from tree_sitter.binding import (
LookaheadIterator,
Expand All @@ -30,17 +31,9 @@
_next_state,
)

__all__ = [
"Language",
"Node",
"Parser",
"Query",
"Range",
"Tree",
"TreeCursor",
"LookaheadIterator",
"LookaheadNamesIterator",
]

def _deprecate(old: str, new: str):
warn("{} is deprecated. Use {} instead.".format(old, new), FutureWarning)


class SymbolType(enum.IntEnum):
Expand Down Expand Up @@ -69,6 +62,7 @@ def build_library(output_path: str, repo_paths: List[str]) -> bool:
the library already existed and was modified more recently than
any of the source files.
"""
_deprecate("Language.build_library", "the new bindings")
output_mtime = path.getmtime(output_path) if path.exists(output_path) else 0

if not repo_paths:
Expand Down Expand Up @@ -129,6 +123,7 @@ def __init__(self, path_or_ptr: Union[str, int], name: str):
given path.
"""
if isinstance(path_or_ptr, str):
_deprecate("Language(path, name)", "Language(ptr, name)")
self.name = name
self.lib = cdll.LoadLibrary(path_or_ptr)
language_function: Callable[[], int] = getattr(self.lib, "tree_sitter_%s" % name)
Expand Down Expand Up @@ -195,8 +190,7 @@ def field_id_for_name(self, name: str) -> Optional[int]:

def next_state(self, state: int, id: int) -> int:
"""
Get the next parse state. Combine this with
[`lookahead_iterator`](Language.lookahead_iterator) to
Get the next parse state. Combine this with `lookahead_iterator` to
generate completion suggestions or valid symbols in error nodes.
"""
return _next_state(self.language_id, state, id)
Expand All @@ -222,3 +216,16 @@ def lookahead_iterator(self, state: int) -> Optional[LookaheadIterator]:
def query(self, source: str) -> Query:
"""Create a Query with the given source code."""
return _language_query(self.language_id, source)


__all__ = [
"Language",
"Node",
"Parser",
"Query",
"Range",
"Tree",
"TreeCursor",
"LookaheadIterator",
"LookaheadNamesIterator",
]
44 changes: 0 additions & 44 deletions tree_sitter/__init__.pyi

This file was deleted.

4 changes: 2 additions & 2 deletions tree_sitter/binding.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "tree_sitter/api.h"

#include <Python.h>
#include <wctype.h>

// Types
Expand Down
Loading

0 comments on commit 9b8fb19

Please sign in to comment.