Skip to content

Commit 9135645

Browse files
authored
Add type annotations for hnswlib (#13529)
1 parent c230773 commit 9135645

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

pyrightconfig.stricter.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"stubs/geopandas",
5353
"stubs/google-cloud-ndb",
5454
"stubs/hdbcli/hdbcli/dbapi.pyi",
55+
"stubs/hnswlib",
5556
"stubs/html5lib",
5657
"stubs/httplib2",
5758
"stubs/humanfriendly",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# The metaclass of Index is pybind11_builtins.pybind11_type. Since pybind11 is not a
2+
# runtime dependency of hnswlib, we exclude Index from stubtest checks.
3+
hnswlib.Index

stubs/hnswlib/METADATA.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version = "0.8.*"
2+
# Requires a version of numpy with a `py.typed` file
3+
requires = ["numpy>=1.20"]
4+
upstream_repository = "https://github.com/nmslib/hnswlib"

stubs/hnswlib/hnswlib.pyi

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from _typeshed import Incomplete
2+
from collections.abc import Callable
3+
from typing import Any, Literal, overload
4+
5+
import numpy as np
6+
from numpy.typing import NDArray
7+
8+
BFIndex: Incomplete
9+
10+
class Index:
11+
ef: int
12+
num_threads: int
13+
14+
@overload
15+
def __init__(self, params: dict[str, Any]) -> None: ...
16+
@overload
17+
def __init__(self, index: Index) -> None: ...
18+
@overload
19+
def __init__(self, space: Literal["l2", "ip", "cosine"], dim: int) -> None: ...
20+
def add_items(self, data, ids: Incomplete | None = None, num_threads: int = -1, replace_deleted: bool = False) -> None: ...
21+
def get_current_count(self) -> int: ...
22+
def get_ids_list(self) -> list[int]: ...
23+
@overload
24+
def get_items(self, ids: Incomplete | None = ..., return_type: Literal["list"] = ...) -> list[float]: ...
25+
@overload
26+
def get_items(self, ids: Incomplete | None = ..., return_type: Literal["numpy"] = ...) -> NDArray[np.float32]: ...
27+
@overload
28+
def get_items(
29+
self, ids: Incomplete | None = None, return_type: Literal["numpy", "list"] = "numpy"
30+
) -> NDArray[np.float32] | list[float]: ...
31+
def get_max_elements(self) -> int: ...
32+
def index_file_size(self) -> int: ...
33+
def init_index(
34+
self,
35+
max_elements: int,
36+
M: int = 16,
37+
ef_construction: int = 200,
38+
random_seed: int = 100,
39+
allow_replace_delete: bool = False,
40+
) -> None: ...
41+
def knn_query(
42+
self, data, k: int = 1, num_threads: int = -1, filter: Callable[[int], bool] | None = None
43+
) -> tuple[NDArray[np.uint64], NDArray[np.float32]]: ...
44+
def load_index(self, path_to_index: str, max_elements: int = 0, allow_replace_delete: bool = False) -> None: ...
45+
def mark_deleted(self, label: int) -> None: ...
46+
def resize_index(self, new_size: int) -> None: ...
47+
def save_index(self, path_to_index: str) -> None: ...
48+
def set_ef(self, ef: int) -> None: ...
49+
def set_num_threads(self, num_threads: int) -> None: ...
50+
def unmark_deleted(self, label: int) -> None: ...
51+
@property
52+
def M(self) -> int: ...
53+
@property
54+
def dim(self) -> int: ...
55+
@property
56+
def ef_construction(self) -> int: ...
57+
@property
58+
def element_count(self) -> int: ...
59+
@property
60+
def max_elements(self) -> int: ...
61+
@property
62+
def space(self) -> str: ...

0 commit comments

Comments
 (0)