Skip to content

Commit a8919d2

Browse files
committed
refactor: improve typing, reduce amount of code needed in hand-written part
1 parent 57690b1 commit a8919d2

File tree

8 files changed

+520
-455
lines changed

8 files changed

+520
-455
lines changed

src/codegen/internal_functions.py.in

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import numpy.typing as npt
1+
from __future__ import annotations
22

3-
from typing import Any, Iterable, List, Optional, Tuple
3+
from typing import Any, Iterable, List, Optional, Tuple, TYPE_CHECKING
44

55
from .conversion import * # noqa
66
from .enums import * # noqa
@@ -16,9 +16,6 @@ from .types import (
1616
VertexLike,
1717
VertexPair,
1818
VertexSelector,
19-
np_type_of_igraph_bool_t,
20-
np_type_of_igraph_integer_t,
21-
np_type_of_igraph_real_t,
2219
)
2320
from .wrappers import (
2421
_Graph,
@@ -28,8 +25,12 @@ from .wrappers import (
2825
_VectorBool,
2926
_VectorInt,
3027
_VectorIntList,
28+
_create_graph_from_boxed,
3129
)
3230

31+
if TYPE_CHECKING:
32+
from igraph_ctypes.graph import Graph
33+
3334
# fmt: off
3435
# flake8: noqa: E743
3536
# The rest of this file is generated by Stimulus

src/codegen/types.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ MATRIX_INT:
9292
OUTCONV: "%I% = igraph_matrix_int_t_to_numpy_array(%C%)"
9393

9494
GRAPH:
95-
PY_TYPE: _Graph
95+
PY_TYPE: Graph
9696
INCONV:
9797
OUT: "%C% = _Graph()"
9898
OUTCONV:
9999
OUT: |-
100-
%I% = %C%.mark_initialized()
100+
%I% = _create_graph_from_boxed(%C%)
101101
INOUT: ~
102102

103103
ATTRIBUTES:

src/igraph_ctypes/_internal/conversion.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"""Conversion functions from Python types to internal igraph types."""
22

3+
from __future__ import annotations
4+
35
import numpy as np
4-
import numpy.typing as npt
56

67
from ctypes import memmove, POINTER
7-
from typing import Any, Iterable, List, Optional, Sequence
8+
from typing import Any, Iterable, List, Optional, Sequence, TYPE_CHECKING
89

910
from .enums import MatrixStorage
1011
from .lib import (
@@ -70,7 +71,6 @@
7071
from .utils import bytes_to_str
7172
from .wrappers import (
7273
_EdgeSelector,
73-
_Graph,
7474
_Matrix,
7575
_MatrixInt,
7676
_Vector,
@@ -81,6 +81,10 @@
8181
_VertexSelector,
8282
)
8383

84+
if TYPE_CHECKING:
85+
from igraph_ctypes.graph import Graph
86+
87+
8488
__all__ = (
8589
"any_to_igraph_bool_t",
8690
"bytes_to_str",
@@ -145,9 +149,7 @@ def edgelike_to_igraph_integer_t(edge: EdgeLike) -> igraph_integer_t:
145149
raise ValueError(f"{edge!r} cannot be converted to an igraph edge index")
146150

147151

148-
def edge_selector_to_igraph_es_t(
149-
selector: EdgeSelector, graph: _Graph
150-
) -> _EdgeSelector:
152+
def edge_selector_to_igraph_es_t(selector: EdgeSelector, graph: Graph) -> _EdgeSelector:
151153
"""Converts a Python object representing a selection of edges to an
152154
igraph_es_t object.
153155
"""
@@ -166,15 +168,15 @@ def edge_selector_to_igraph_es_t(
166168
return _EdgeSelector.create_with(igraph_es_1, index)
167169

168170

169-
def edge_weights_to_igraph_vector_t(weights: Iterable[float], graph: _Graph) -> _Vector:
171+
def edge_weights_to_igraph_vector_t(weights: Iterable[float], graph: Graph) -> _Vector:
170172
"""Converts a Python iterable of floating-point numbers to a vector of
171173
edge weights.
172174
"""
173175
return iterable_to_igraph_vector_t(weights)
174176

175177

176178
def edge_weights_to_igraph_vector_t_view(
177-
weights: Optional[Iterable[float]], graph: _Graph
179+
weights: Optional[Iterable[float]], graph: Graph
178180
) -> Optional[_Vector]:
179181
"""Converts a Python iterable of floating-point numbers to a vector of
180182
edge weights, possibly creating a shallow view if the input is an
@@ -198,13 +200,13 @@ def edge_weights_to_igraph_vector_t_view(
198200
edge_capacities_to_igraph_vector_t_view = edge_weights_to_igraph_vector_t_view
199201

200202

201-
def edge_colors_to_igraph_vector_t(colors: Iterable[int], graph: _Graph) -> _VectorInt:
203+
def edge_colors_to_igraph_vector_t(colors: Iterable[int], graph: Graph) -> _VectorInt:
202204
"""Converts a Python iterable of integers to a vector of edge colors."""
203205
return iterable_to_igraph_vector_int_t(colors)
204206

205207

206208
def edge_colors_to_igraph_vector_t_view(
207-
colors: Iterable[int], graph: _Graph
209+
colors: Iterable[int], graph: Graph
208210
) -> _VectorInt:
209211
"""Converts a Python iterable of integers to a vector of edge colors,
210212
possibly creating a shallow view if the input is an appropriate NumPy array.
@@ -596,7 +598,7 @@ def vertex_pairs_to_igraph_vector_int_t(pairs: Iterable[VertexPair]) -> _VectorI
596598

597599

598600
def vertex_selector_to_igraph_vs_t(
599-
selector: VertexSelector, graph: _Graph
601+
selector: VertexSelector, graph: Graph
600602
) -> _VertexSelector:
601603
"""Converts a Python object representing a selection of vertices to an
602604
igraph_vs_t object.
@@ -609,38 +611,38 @@ def vertex_selector_to_igraph_vs_t(
609611
# TODO(ntamas): implement name lookup?
610612
raise TypeError("vertex selector cannot be a string")
611613
elif hasattr(selector, "__iter__"):
612-
indices = iterable_vertex_indices_to_igraph_vector_int_t(selector) # type: ignore
614+
indices = iterable_vertex_indices_to_igraph_vector_int_t(
615+
selector # type: ignore
616+
)
613617
return _VertexSelector.create_with(igraph_vs_vector_copy, indices)
614618
else:
615619
index = vertexlike_to_igraph_integer_t(selector) # type: ignore
616620
return _VertexSelector.create_with(igraph_vs_1, index)
617621

618622

619-
def vertex_colors_to_igraph_vector_t(
620-
colors: Iterable[int], graph: _Graph
621-
) -> _VectorInt:
623+
def vertex_colors_to_igraph_vector_t(colors: Iterable[int], graph: Graph) -> _VectorInt:
622624
"""Converts a Python iterable of integers to a vector of vertex colors."""
623625
return iterable_to_igraph_vector_int_t(colors)
624626

625627

626628
def vertex_colors_to_igraph_vector_t_view(
627-
colors: Iterable[int], graph: _Graph
629+
colors: Iterable[int], graph: Graph
628630
) -> _VectorInt:
629631
"""Converts a Python iterable of integers to a vector of vertex colors,
630632
possibly creating a shallow view if the input is an appropriate NumPy array.
631633
"""
632634
return iterable_to_igraph_vector_int_t_view(colors)
633635

634636

635-
def vertex_qtys_to_igraph_vector_t(weights: Iterable[float], graph: _Graph) -> _Vector:
637+
def vertex_qtys_to_igraph_vector_t(weights: Iterable[float], graph: Graph) -> _Vector:
636638
"""Converts a Python iterable of floating-point numbers to a vector of
637639
vertex-related quantities.
638640
"""
639641
return iterable_to_igraph_vector_t(weights)
640642

641643

642644
def vertex_qtys_to_igraph_vector_t_view(
643-
weights: Optional[Iterable[float]], graph: _Graph
645+
weights: Optional[Iterable[float]], graph: Graph
644646
) -> Optional[_Vector]:
645647
"""Converts a Python iterable of floating-point numbers to a vector of
646648
vertex-related quantities, possibly creating a shallow view if the input is

0 commit comments

Comments
 (0)