2525 class CanHashAndCompareLT (typing .Protocol ):
2626 def __hash__ (self ) -> int : ...
2727
28- def __lt__ (self , other : typing .Self , / ) -> bool : ...
28+ def __lt__ (self , other : typing .Any , / ) -> bool : ...
2929
3030 class CanHashAndCompareGT (typing .Protocol ):
3131 def __hash__ (self ) -> int : ...
3232
33- def __gt__ (self , other : typing .Self , / ) -> bool : ...
33+ def __gt__ (self , other : typing .Any , / ) -> bool : ...
3434
3535else :
3636
@@ -62,7 +62,7 @@ def cycle(self) -> list[T]:
6262 return self .args [0 ]
6363
6464
65- class NodeData [T ]:
65+ class NodeData [T : CanHashAndCompare ]:
6666 __slots__ = ("dependants" , "ndependencies" , "node" )
6767
6868 def __init__ (self , node : T ) -> None :
@@ -74,6 +74,9 @@ def __init_subclass__(cls) -> t.Never:
7474 msg = "Don't subclass this"
7575 raise RuntimeError (msg )
7676
77+ def __lt__ (self , other : t .Self ) -> bool : # falback for tuple sorting
78+ return True
79+
7780 __final__ = True
7881
7982
@@ -174,14 +177,15 @@ def __iter__(self) -> Generator[T, None, None]:
174177 return self .__iter ()
175178
176179 def __iter (self ) -> Generator [T , None , None ]:
177- while ready := [
178- i .node for i in self ._nodemap .values () if not i .ndependencies
179- ]:
180- next_node = min (ready )
181- self ._nodemap [next_node ].ndependencies = - 1
180+ ready = [(n , i ) for n , i in self ._nodemap .items () if not i .ndependencies ]
181+ while ready :
182+ next_node , info = min (ready )
183+ info .ndependencies = - 1
182184
183185 yield next_node
184186
185- for dep in self . _nodemap [ next_node ] .dependants :
187+ for dep in info .dependants :
186188 dep_info = self ._nodemap [dep ]
187189 dep_info .ndependencies -= 1
190+ if not dep_info .ndependencies :
191+ ready .append ((dep , dep_info ))
0 commit comments