Skip to content

Commit edc4c6a

Browse files
committed
chore: typing simplifications
1 parent 3d081dd commit edc4c6a

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/igraph_ctypes/_internal/attributes.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
from dataclasses import dataclass, field
77
from typing import Any, Callable, Optional
88

9-
from .lib import igraph_error, igraph_vector_ptr_size
9+
from .conversion import igraph_vector_int_t_to_numpy_array_view
10+
from .lib import igraph_error
1011
from .refcount import incref, decref
11-
from .types import igraph_attribute_table_t
12+
from .types import igraph_attribute_table_t, IntArray
1213
from .utils import nop, protect_with
1314

1415
__all__ = ("AttributeHandlerBase", "AttributeHandler", "AttributeStorage")
@@ -66,6 +67,13 @@ def add_vertices(self, graph, n: int) -> None:
6667
"""
6768
raise NotImplementedError
6869

70+
@abstractmethod
71+
def add_edges(self, graph, edges) -> None:
72+
"""Notifies the attribute storage object that the given edges were
73+
added to the graph.
74+
"""
75+
raise NotImplementedError
76+
6977
@abstractmethod
7078
def clear(self):
7179
"""Clears the storage area, removing all attributes."""
@@ -94,6 +102,9 @@ class DictAttributeStorage(AttributeStorage):
94102
def add_vertices(self, graph, n: int) -> None:
95103
pass
96104

105+
def add_edges(self, graph, edges: IntArray) -> None:
106+
pass
107+
97108
def clear(self) -> None:
98109
"""Clears the storage area, removing all attributes from the
99110
attribute dictionaries.
@@ -194,7 +205,16 @@ def combine_vertices(self, graph, to, mapping, combinations):
194205
pass
195206

196207
def add_edges(self, graph, edges, attr) -> None:
197-
pass
208+
# attr will only ever be NULL here so raise an error if it is not
209+
if attr:
210+
raise RuntimeError(
211+
"add_edges() attribute handler called with non-null attr; "
212+
"this is most likely a bug"
213+
)
214+
215+
# Extend the existing attribute containers
216+
edge_array = igraph_vector_int_t_to_numpy_array_view(edges).reshape((-1, 2))
217+
_get_storage_from_graph(graph).add_edges(graph, edge_array)
198218

199219
def permute_edges(self, graph, to, mapping):
200220
pass

src/igraph_ctypes/graph.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from numpy.typing import NDArray
21
from typing import Iterable, Optional, TypeVar
32

43
from .enums import NeighborMode
54
from .types import (
65
EdgeSelector,
6+
IntArray,
77
VertexLike,
88
VertexPair,
99
VertexSelector,
@@ -23,7 +23,6 @@
2323
neighbors,
2424
vcount,
2525
)
26-
from ._internal.types import np_type_of_igraph_integer_t
2726
from ._internal.wrappers import _Graph
2827

2928

@@ -87,7 +86,7 @@ def get_edge_id(
8786

8887
def incident(
8988
self, vid: VertexLike, mode: NeighborMode = NeighborMode.ALL
90-
) -> NDArray[np_type_of_igraph_integer_t]:
89+
) -> IntArray:
9190
return incident(self, vid, mode)
9291

9392
def is_directed(self) -> bool:
@@ -96,7 +95,7 @@ def is_directed(self) -> bool:
9695

9796
def neighbors(
9897
self, vid: VertexLike, mode: NeighborMode = NeighborMode.ALL
99-
) -> NDArray[np_type_of_igraph_integer_t]:
98+
) -> IntArray:
10099
"""Returns the list of neighbors of a vertex."""
101100
return neighbors(self, vid, mode)
102101

0 commit comments

Comments
 (0)