Commits

Shu Zong Chen  committed af675f8

Fixed issue where it fails if your graph is empty

  • Participants
  • Parent commits b1b51cb

Comments (0)

Files changed (1)

File dependency_graph.py

 	@property
 	def ends(self):
 		"""Return nodes with no outgoing edges"""
-		f, t = zip(*self.vectors)
-		return [i for i in set(t) if i not in set(f)]
+		if len(self.vectors):
+			f, t = zip(*self.vectors)
+			return [i for i in set(t) if i not in set(f)]
+		return []
 
 	@property
 	def roots(self):
 		"""Return nodes with no incoming edges"""
-		f, t = zip(*self.vectors)
-		return [i for i in set(f) if i not in set(t)]
+		if len(self.vectors):
+			f, t = zip(*self.vectors)
+			return [i for i in set(f) if i not in set(t)]
+		return []
 
 	def _ancestors_of(self, target, bundle=None):
 		if bundle is None:
 			self.assertEqual(h.vectors is h.vectors, True)
 			self.assertEqual(g.vectors is h.vectors, False)
 
+		def test_empty_graph(self):
+			g = dependency_graph()
+			self.assertEqual(g.ends, [])
+			self.assertEqual(g.roots, [])
+
 		def test_kahntopological(self):
 			g = dependency_graph(self.straight_graph)
 			self.assertEqual(g.kahn_topological_sort(), [1, 2, 3, 4, 5, 6])