for vertex in subgraph:
    anc = nx.ancestors(G, vertex)
dependencies.union(anc)
dependencies -= subgraph.nodes()
for vertex in subgraph:
    G[vertex].node['depends'] = dependencies
topo_sort_mod(G)
How to topological sort a sub/nested graph?