33
44# Metadata
55__author__ = 'https://md.land/md'
6- __version__ = '1.0 .0'
6+ __version__ = '1.1 .0'
77__all__ = (
88 # Metadata
99 '__author__' ,
@@ -40,13 +40,13 @@ class GraphExceptionInterface:
4040class TopologicalSortException (RuntimeError , GraphExceptionInterface ):
4141 CYCLE_DETECTED = 1
4242
43- def __init__ (self , * args , code : int = 0 , graph : GraphType = None , ** kwargs ) -> None :
44- super ().__init__ (* args , ** kwargs )
43+ def __init__ (self , * args , code : int = 0 , graph : typing . Optional [ GraphType ] = None ) -> None :
44+ super ().__init__ (* args )
4545 self .code = code
4646 self .graph = graph
4747
4848 @classmethod
49- def as_cycle_detected (class_ , graph : GraphType = None ) -> 'TopologicalSortException' :
49+ def as_cycle_detected (class_ , graph : typing . Optional [ GraphType ] = None ) -> 'TopologicalSortException' :
5050 return class_ (
5151 'Unable to perform topological sort, graph contains a cycle' ,
5252 code = class_ .CYCLE_DETECTED ,
@@ -55,8 +55,8 @@ def as_cycle_detected(class_, graph: GraphType = None) -> 'TopologicalSortExcept
5555
5656
5757# Contract
58- class TopologicalSortInterface :
59- def sort (self , graph : GraphType ) -> typing .Iterable [NodeType ]:
58+ class TopologicalSortInterface ( typing . Generic [ NodeType ]) :
59+ def sort (self , graph : GraphType [ NodeType ] ) -> typing .Iterable [NodeType ]:
6060 """
6161 Performs graph topological sorting and returns sequence of nodes
6262 :param graph: Generic graph structure represented by Mapping of Hashable nodes
@@ -66,7 +66,7 @@ def sort(self, graph: GraphType) -> typing.Iterable[NodeType]:
6666
6767
6868# Implementation
69- def topological_sort_ascending (graph : GraphType ) -> typing .Iterable [NodeType ]:
69+ def topological_sort_ascending (graph : GraphType [ NodeType ] ) -> typing .Iterable [NodeType ]:
7070 """
7171 Performs graph topological sorting and returns sequence of nodes (from the bottom)
7272
@@ -82,7 +82,7 @@ def topological_sort_ascending(graph: GraphType) -> typing.Iterable[NodeType]:
8282 for node , related_node_collection in graph .items ():
8383 normalized_graph [node ] = set (related_node_collection )
8484
85- leave_node_set = set ()
85+ leave_node_set : typing . Set [ NodeType ] = set ()
8686
8787 # 2. Search for nodes which are not explicitly defined as an empty graph
8888 for related_node_set in normalized_graph .values ():
@@ -100,7 +100,7 @@ def topological_sort_ascending(graph: GraphType) -> typing.Iterable[NodeType]:
100100 break
101101
102102 try :
103- yield from sorted (leave_node_set )
103+ yield from sorted (leave_node_set ) # type: ignore
104104 except TypeError :
105105 yield from leave_node_set
106106
@@ -116,8 +116,8 @@ def topological_sort_ascending(graph: GraphType) -> typing.Iterable[NodeType]:
116116
117117
118118def topological_sort_descending (
119- graph : GraphType ,
120- initial_node : typing .Iterable [NodeType ] = None
119+ graph : GraphType [ NodeType ] ,
120+ initial_node : typing .Optional [ typing . Iterable [NodeType ] ] = None
121121) -> typing .Iterable [NodeType ]:
122122 """
123123 Performs graph topological sorting and returns sequence of nodes (from the top)
@@ -160,9 +160,9 @@ def topological_sort_descending(
160160 yield pending_node
161161
162162
163- def get_paths (graph : GraphType , include_subtree : bool = False ) -> typing .Tuple [
164- typing .List [GraphPathType ], # path list without cycle
165- typing .List [GraphPathType ], # path list with cycle
163+ def get_paths (graph : GraphType [ NodeType ] , include_subtree : bool = False ) -> typing .Tuple [
164+ typing .Sequence [GraphPathType [ NodeType ] ], # path list without cycle
165+ typing .Sequence [GraphPathType [ NodeType ] ], # path list with cycle
166166]:
167167 """
168168 Returns two-sized tuple of graph paths list:
@@ -209,9 +209,9 @@ def get_paths(graph: GraphType, include_subtree: bool = False) -> typing.Tuple[
209209 # 2. Build result
210210 node_list = elder_node_heap
211211 if include_subtree :
212- node_list = node_path_map .keys ()
212+ node_list = list ( node_path_map .keys () )
213213
214- path_list : typing .List [GraphPathType ] = []
214+ path_list : typing .List [GraphPathType [ NodeType ] ] = []
215215 cycle_path_list = []
216216
217217 for root_node in node_list :
@@ -223,11 +223,11 @@ def get_paths(graph: GraphType, include_subtree: bool = False) -> typing.Tuple[
223223 return path_list , cycle_path_list
224224
225225
226- class AscendingTopologicalSort (TopologicalSortInterface , typing . Generic [NodeType ]):
227- def sort (self , graph : GraphType ) -> typing .Iterable [NodeType ]:
226+ class AscendingTopologicalSort (TopologicalSortInterface [NodeType ]):
227+ def sort (self , graph : GraphType [ NodeType ] ) -> typing .Iterable [NodeType ]:
228228 return topological_sort_ascending (graph = graph )
229229
230230
231- class DescendingTopologicalSort (TopologicalSortInterface , typing . Generic [NodeType ]):
232- def sort (self , graph : GraphType ) -> typing .Iterable [NodeType ]:
231+ class DescendingTopologicalSort (TopologicalSortInterface [NodeType ]):
232+ def sort (self , graph : GraphType [ NodeType ] ) -> typing .Iterable [NodeType ]:
233233 return topological_sort_descending (graph = graph )
0 commit comments