Skip to content

Commit 2784aec

Browse files
matthiasdienerinducer
authored andcommitted
graph: more mypy annotations
1 parent 43754b5 commit 2784aec

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

pytools/graph.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,19 @@ def reverse_graph(graph: Mapping[T, Collection[T]]) -> Dict[T, FrozenSet[T]]:
8181
# {{{ a_star
8282

8383
def a_star( # pylint: disable=too-many-locals
84-
initial_state, goal_state, neighbor_map,
85-
estimate_remaining_cost=None,
86-
get_step_cost=lambda x, y: 1
87-
):
84+
initial_state: T, goal_state: T, neighbor_map: Mapping[T, Collection[T]],
85+
estimate_remaining_cost: Optional[Callable[[T], float]] = None,
86+
get_step_cost: Callable[[Any, T], float] = lambda x, y: 1
87+
) -> List[T]:
8888
"""
8989
With the default cost and heuristic, this amounts to Dijkstra's algorithm.
9090
"""
9191

9292
from heapq import heappop, heappush
9393

9494
if estimate_remaining_cost is None:
95-
def estimate_remaining_cost(x): # pylint: disable=function-redefined
95+
# pylint: disable=function-redefined
96+
def estimate_remaining_cost(x: T) -> float:
9697
if x != goal_state:
9798
return 1
9899
else:
@@ -101,7 +102,7 @@ def estimate_remaining_cost(x): # pylint: disable=function-redefined
101102
class AStarNode:
102103
__slots__ = ["state", "parent", "path_cost"]
103104

104-
def __init__(self, state, parent, path_cost):
105+
def __init__(self, state: T, parent: Any, path_cost: float) -> None:
105106
self.state = state
106107
self.parent = parent
107108
self.path_cost = path_cost
@@ -119,7 +120,7 @@ def __init__(self, state, parent, path_cost):
119120

120121
if top.state == goal_state:
121122
result = []
122-
it = top
123+
it: Optional[AStarNode] = top
123124
while it is not None:
124125
result.append(it.state)
125126
it = it.parent
@@ -213,7 +214,7 @@ class CycleError(Exception):
213214
214215
:attr node: Node in a directed graph that is part of a cycle.
215216
"""
216-
def __init__(self, node) -> None:
217+
def __init__(self, node: T) -> None:
217218
self.node = node
218219

219220

@@ -225,11 +226,11 @@ class HeapEntry:
225226
Only needs to define :func:`pytools.graph.__lt__` according to
226227
<https://github.com/python/cpython/blob/8d21aa21f2cbc6d50aab3f420bb23be1d081dac4/Lib/heapq.py#L135-L138>.
227228
"""
228-
def __init__(self, node, key) -> None:
229+
def __init__(self, node: T, key: Any) -> None:
229230
self.node = node
230231
self.key = key
231232

232-
def __lt__(self, other) -> bool:
233+
def __lt__(self, other: "HeapEntry") -> bool:
233234
return self.key < other.key
234235

235236

0 commit comments

Comments
 (0)