Skip to content

Commit 9103fd9

Browse files
committed
fix: minor tweaks to make Pyright accept _Graph type annotations
1 parent 4d4c02c commit 9103fd9

File tree

5 files changed

+47
-34
lines changed

5 files changed

+47
-34
lines changed

src/codegen/internal_functions.py.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ from .wrappers import (
2727
_Vector,
2828
_VectorBool,
2929
_VectorInt,
30-
_VectorIntList
30+
_VectorIntList,
3131
)
3232

3333
# fmt: off

src/igraph_ctypes/_internal/functions.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
_Vector,
2828
_VectorBool,
2929
_VectorInt,
30-
_VectorIntList
30+
_VectorIntList,
3131
)
3232

3333
# fmt: off
@@ -845,17 +845,18 @@ def erdos_renyi_game_gnp(n: int, p: float, directed: bool = False, loops: bool =
845845
return graph
846846

847847

848-
def erdos_renyi_game_gnm(n: int, m: int, directed: bool = False, loops: bool = False) -> _Graph:
848+
def erdos_renyi_game_gnm(n: int, m: int, directed: bool = False, loops: bool = False, multiple: bool = False) -> _Graph:
849849
"""Type-annotated wrapper for ``igraph_erdos_renyi_game_gnm``."""
850850
# Prepare input arguments
851851
c_graph = _Graph()
852852
c_n = n
853853
c_m = m
854854
c_directed = any_to_igraph_bool_t(directed)
855855
c_loops = any_to_igraph_bool_t(loops)
856+
c_multiple = any_to_igraph_bool_t(multiple)
856857

857858
# Call wrapped function
858-
igraph_erdos_renyi_game_gnm(c_graph, c_n, c_m, c_directed, c_loops)
859+
igraph_erdos_renyi_game_gnm(c_graph, c_n, c_m, c_directed, c_loops, c_multiple)
859860

860861
# Prepare output arguments
861862
graph = c_graph.mark_initialized()
@@ -3475,8 +3476,8 @@ def create_bipartite(types: Iterable[Any], edges: Iterable[int], directed: bool
34753476
return graph
34763477

34773478

3478-
def incidence(incidence: MatrixLike, directed: bool = False, mode: NeighborMode = NeighborMode.ALL, multiple: bool = False) -> Tuple[_Graph, BoolArray]:
3479-
"""Type-annotated wrapper for ``igraph_incidence``."""
3479+
def biadjacency(incidence: MatrixLike, directed: bool = False, mode: NeighborMode = NeighborMode.ALL, multiple: bool = False) -> Tuple[_Graph, BoolArray]:
3480+
"""Type-annotated wrapper for ``igraph_biadjacency``."""
34803481
# Prepare input arguments
34813482
c_graph = _Graph()
34823483
c_types = _VectorBool.create(0)
@@ -3486,7 +3487,7 @@ def incidence(incidence: MatrixLike, directed: bool = False, mode: NeighborMode
34863487
c_multiple = any_to_igraph_bool_t(multiple)
34873488

34883489
# Call wrapped function
3489-
igraph_incidence(c_graph, c_types, c_incidence, c_directed, c_mode, c_multiple)
3490+
igraph_biadjacency(c_graph, c_types, c_incidence, c_directed, c_mode, c_multiple)
34903491

34913492
# Prepare output arguments
34923493
graph = c_graph.mark_initialized()
@@ -3496,8 +3497,8 @@ def incidence(incidence: MatrixLike, directed: bool = False, mode: NeighborMode
34963497
return graph, types
34973498

34983499

3499-
def get_incidence(graph: _Graph, types: Optional[Iterable[Any]] = None) -> Tuple[RealArray, IntArray, IntArray]:
3500-
"""Type-annotated wrapper for ``igraph_get_incidence``."""
3500+
def get_biadjacency(graph: _Graph, types: Optional[Iterable[Any]] = None) -> Tuple[RealArray, IntArray, IntArray]:
3501+
"""Type-annotated wrapper for ``igraph_get_biadjacency``."""
35013502
# Prepare input arguments
35023503
c_graph = graph
35033504
c_types = iterable_to_igraph_vector_bool_t_view(types) if types is not None else None
@@ -3506,7 +3507,7 @@ def get_incidence(graph: _Graph, types: Optional[Iterable[Any]] = None) -> Tuple
35063507
c_col_ids = _VectorInt.create(0)
35073508

35083509
# Call wrapped function
3509-
igraph_get_incidence(c_graph, c_types, c_res, c_row_ids, c_col_ids)
3510+
igraph_get_biadjacency(c_graph, c_types, c_res, c_row_ids, c_col_ids)
35103511

35113512
# Prepare output arguments
35123513
res = igraph_matrix_t_to_numpy_array(c_res)
@@ -4216,17 +4217,18 @@ def bibcoupling(graph: _Graph, vids: VertexSelector = "all") -> RealArray:
42164217
return res
42174218

42184219

4219-
def similarity_dice(graph: _Graph, vids: VertexSelector = "all", mode: NeighborMode = NeighborMode.ALL, loops: bool = False) -> RealArray:
4220+
def similarity_dice(graph: _Graph, vit_from: VertexSelector = "all", vit_to: VertexSelector = "all", mode: NeighborMode = NeighborMode.ALL, loops: bool = False) -> RealArray:
42204221
"""Type-annotated wrapper for ``igraph_similarity_dice``."""
42214222
# Prepare input arguments
42224223
c_graph = graph
42234224
c_res = _Matrix.create(0)
4224-
c_vids = vertex_selector_to_igraph_vs_t(vids, graph)
4225+
c_vit_from = vertex_selector_to_igraph_vs_t(vit_from, graph)
4226+
c_vit_to = vertex_selector_to_igraph_vs_t(vit_to, graph)
42254227
c_mode = c_int(mode)
42264228
c_loops = any_to_igraph_bool_t(loops)
42274229

42284230
# Call wrapped function
4229-
igraph_similarity_dice(c_graph, c_res, c_vids.unwrap(), c_mode, c_loops)
4231+
igraph_similarity_dice(c_graph, c_res, c_vit_from.unwrap(), c_vit_to.unwrap(), c_mode, c_loops)
42304232

42314233
# Prepare output arguments
42324234
res = igraph_matrix_t_to_numpy_array(c_res)
@@ -4291,17 +4293,18 @@ def similarity_inverse_log_weighted(graph: _Graph, vids: VertexSelector = "all",
42914293
return res
42924294

42934295

4294-
def similarity_jaccard(graph: _Graph, vids: VertexSelector = "all", mode: NeighborMode = NeighborMode.ALL, loops: bool = False) -> RealArray:
4296+
def similarity_jaccard(graph: _Graph, vit_from: VertexSelector = "all", vit_to: VertexSelector = "all", mode: NeighborMode = NeighborMode.ALL, loops: bool = False) -> RealArray:
42954297
"""Type-annotated wrapper for ``igraph_similarity_jaccard``."""
42964298
# Prepare input arguments
42974299
c_graph = graph
42984300
c_res = _Matrix.create(0)
4299-
c_vids = vertex_selector_to_igraph_vs_t(vids, graph)
4301+
c_vit_from = vertex_selector_to_igraph_vs_t(vit_from, graph)
4302+
c_vit_to = vertex_selector_to_igraph_vs_t(vit_to, graph)
43004303
c_mode = c_int(mode)
43014304
c_loops = any_to_igraph_bool_t(loops)
43024305

43034306
# Call wrapped function
4304-
igraph_similarity_jaccard(c_graph, c_res, c_vids.unwrap(), c_mode, c_loops)
4307+
igraph_similarity_jaccard(c_graph, c_res, c_vit_from.unwrap(), c_vit_to.unwrap(), c_mode, c_loops)
43054308

43064309
# Prepare output arguments
43074310
res = igraph_matrix_t_to_numpy_array(c_res)
@@ -4786,8 +4789,6 @@ def graphlets_project(graph: _Graph, cliques: Iterable[Iterable[VertexLike]], Mu
47864789

47874790
# igraph_hrg_game: no Python type known for type: HRG
47884791

4789-
# igraph_hrg_dendrogram: no Python type known for type: HRG
4790-
47914792
# igraph_hrg_consensus: no Python type known for type: HRG
47924793

47934794
# igraph_hrg_predict: no Python type known for type: HRG
@@ -4798,6 +4799,8 @@ def graphlets_project(graph: _Graph, cliques: Iterable[Iterable[VertexLike]], Mu
47984799

47994800
# igraph_hrg_size: no Python type known for type: HRG
48004801

4802+
# igraph_from_hrg_dendrogram: no Python type known for type: HRG
4803+
48014804
# igraph_get_adjacency: no Python type known for type: GETADJACENCY
48024805

48034806
# igraph_get_adjacency_sparse: no Python type known for type: SPARSEMAT

src/igraph_ctypes/_internal/lib.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ def _load_igraph_c_library():
622622

623623
igraph_erdos_renyi_game_gnm = _lib.igraph_erdos_renyi_game_gnm
624624
igraph_erdos_renyi_game_gnm.restype = handle_igraph_error_t
625-
igraph_erdos_renyi_game_gnm.argtypes = [POINTER(igraph_t), igraph_integer_t, igraph_integer_t, igraph_bool_t, igraph_bool_t]
625+
igraph_erdos_renyi_game_gnm.argtypes = [POINTER(igraph_t), igraph_integer_t, igraph_integer_t, igraph_bool_t, igraph_bool_t, igraph_bool_t]
626626

627627
igraph_degree_sequence_game = _lib.igraph_degree_sequence_game
628628
igraph_degree_sequence_game.restype = handle_igraph_error_t
@@ -1212,13 +1212,13 @@ def _load_igraph_c_library():
12121212
igraph_create_bipartite.restype = handle_igraph_error_t
12131213
igraph_create_bipartite.argtypes = [POINTER(igraph_t), POINTER(igraph_vector_bool_t), POINTER(igraph_vector_int_t), igraph_bool_t]
12141214

1215-
igraph_incidence = _lib.igraph_incidence
1216-
igraph_incidence.restype = handle_igraph_error_t
1217-
igraph_incidence.argtypes = [POINTER(igraph_t), POINTER(igraph_vector_bool_t), POINTER(igraph_matrix_t), igraph_bool_t, igraph_neimode_t, igraph_bool_t]
1215+
igraph_biadjacency = _lib.igraph_biadjacency
1216+
igraph_biadjacency.restype = handle_igraph_error_t
1217+
igraph_biadjacency.argtypes = [POINTER(igraph_t), POINTER(igraph_vector_bool_t), POINTER(igraph_matrix_t), igraph_bool_t, igraph_neimode_t, igraph_bool_t]
12181218

1219-
igraph_get_incidence = _lib.igraph_get_incidence
1220-
igraph_get_incidence.restype = handle_igraph_error_t
1221-
igraph_get_incidence.argtypes = [POINTER(igraph_t), POINTER(igraph_vector_bool_t), POINTER(igraph_matrix_t), POINTER(igraph_vector_int_t), POINTER(igraph_vector_int_t)]
1219+
igraph_get_biadjacency = _lib.igraph_get_biadjacency
1220+
igraph_get_biadjacency.restype = handle_igraph_error_t
1221+
igraph_get_biadjacency.argtypes = [POINTER(igraph_t), POINTER(igraph_vector_bool_t), POINTER(igraph_matrix_t), POINTER(igraph_vector_int_t), POINTER(igraph_vector_int_t)]
12221222

12231223
igraph_is_bipartite = _lib.igraph_is_bipartite
12241224
igraph_is_bipartite.restype = handle_igraph_error_t
@@ -1450,7 +1450,7 @@ def _load_igraph_c_library():
14501450

14511451
igraph_similarity_dice = _lib.igraph_similarity_dice
14521452
igraph_similarity_dice.restype = handle_igraph_error_t
1453-
igraph_similarity_dice.argtypes = [POINTER(igraph_t), POINTER(igraph_matrix_t), igraph_vs_t, igraph_neimode_t, igraph_bool_t]
1453+
igraph_similarity_dice.argtypes = [POINTER(igraph_t), POINTER(igraph_matrix_t), igraph_vs_t, igraph_vs_t, igraph_neimode_t, igraph_bool_t]
14541454

14551455
igraph_similarity_dice_es = _lib.igraph_similarity_dice_es
14561456
igraph_similarity_dice_es.restype = handle_igraph_error_t
@@ -1466,7 +1466,7 @@ def _load_igraph_c_library():
14661466

14671467
igraph_similarity_jaccard = _lib.igraph_similarity_jaccard
14681468
igraph_similarity_jaccard.restype = handle_igraph_error_t
1469-
igraph_similarity_jaccard.argtypes = [POINTER(igraph_t), POINTER(igraph_matrix_t), igraph_vs_t, igraph_neimode_t, igraph_bool_t]
1469+
igraph_similarity_jaccard.argtypes = [POINTER(igraph_t), POINTER(igraph_matrix_t), igraph_vs_t, igraph_vs_t, igraph_neimode_t, igraph_bool_t]
14701470

14711471
igraph_similarity_jaccard_es = _lib.igraph_similarity_jaccard_es
14721472
igraph_similarity_jaccard_es.restype = handle_igraph_error_t
@@ -1580,10 +1580,6 @@ def _load_igraph_c_library():
15801580
igraph_hrg_game.restype = handle_igraph_error_t
15811581
igraph_hrg_game.argtypes = [POINTER(igraph_t), POINTER(igraph_hrg_t)]
15821582

1583-
igraph_hrg_dendrogram = _lib.igraph_hrg_dendrogram
1584-
igraph_hrg_dendrogram.restype = handle_igraph_error_t
1585-
igraph_hrg_dendrogram.argtypes = [POINTER(igraph_t), POINTER(igraph_hrg_t)]
1586-
15871583
igraph_hrg_consensus = _lib.igraph_hrg_consensus
15881584
igraph_hrg_consensus.restype = handle_igraph_error_t
15891585
igraph_hrg_consensus.argtypes = [POINTER(igraph_t), POINTER(igraph_vector_int_t), POINTER(igraph_vector_t), POINTER(igraph_hrg_t), igraph_bool_t, igraph_integer_t]
@@ -1604,6 +1600,10 @@ def _load_igraph_c_library():
16041600
igraph_hrg_size.restype = igraph_integer_t
16051601
igraph_hrg_size.argtypes = [POINTER(igraph_hrg_t)]
16061602

1603+
igraph_from_hrg_dendrogram = _lib.igraph_from_hrg_dendrogram
1604+
igraph_from_hrg_dendrogram.restype = handle_igraph_error_t
1605+
igraph_from_hrg_dendrogram.argtypes = [POINTER(igraph_t), POINTER(igraph_hrg_t), POINTER(igraph_vector_t)]
1606+
16071607
igraph_get_adjacency = _lib.igraph_get_adjacency
16081608
igraph_get_adjacency.restype = handle_igraph_error_t
16091609
igraph_get_adjacency.argtypes = [POINTER(igraph_t), POINTER(igraph_matrix_t), igraph_get_adjacency_t, POINTER(igraph_vector_t), igraph_loops_t]

src/igraph_ctypes/_internal/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
from ctypes import c_char_p
2+
from typing import Union
23

4+
__all__ = ("bytes_to_str",)
35

4-
def bytes_to_str(value: bytes, encoding: str = "utf-8", errors: str = "replace") -> str:
6+
7+
def bytes_to_str(
8+
value: Union[bytes, c_char_p], encoding: str = "utf-8", errors: str = "replace"
9+
) -> str:
510
"""Converts a C string represented as a Python bytes object or as a
611
ctypes ``c_char_p`` object into a Python string, using the given encoding
712
and error handling.

src/igraph_ctypes/_internal/wrappers.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ def __init__(self, instance: Optional[T] = None):
168168
initialized = False
169169
elif not isinstance(instance, cls_outer):
170170
raise TypeError(
171-
f"{name} must wrap an object of type {cls_outer!r}, got {type(instance)!r}"
171+
f"{name} must wrap an object of type {cls_outer!r}, "
172+
f"got {type(instance)!r}"
172173
)
173174
else:
174175
initialized = True
@@ -178,7 +179,11 @@ def __init__(self, instance: Optional[T] = None):
178179
return BoxedClass
179180

180181

181-
_Graph = create_boxed("_Graph", igraph_t, destructor=igraph_destroy)
182+
# This trickery is needed to ensure that Pyright does to trip up on _Graph annotations
183+
class _Graph(create_boxed("_Graph", igraph_t, destructor=igraph_destroy)):
184+
pass
185+
186+
182187
_Matrix = create_boxed(
183188
"_Matrix",
184189
igraph_matrix_t,

0 commit comments

Comments
 (0)