def __init__(self, parsed, par_names): self.def_use = beniget.DefUseChains() self.def_use.visit(parsed) self.use_def = beniget.UseDefChains(self.def_use) self.params = {k: v for (k, v) in self.use_def.chains.items() if hasattr(k, 'id') and k.id in par_names and v} self.graph = pydot.Dot(graph_type='digraph') self.node_to_dot_node = {} self.src_to_target = set() self.already_visited = set() for param_node in self.params.keys(): self.visit(param_node)
def checkChains(self, code, ref): class StrictDefUseChains(beniget.DefUseChains): def unbound_identifier(self, name, node): raise RuntimeError( "W: unbound identifier '{}' at {}:{}".format( name, node.lineno, node.col_offset)) node = ast.parse(code) c = StrictDefUseChains() c.visit(node) cc = beniget.UseDefChains(c) self.assertEqual(str(cc), ref)
def visit_Module(self, node): udc = beniget.UseDefChains(self.def_use_chains) self.result = udc.chains
import gast as ast import beniget mod = ast.parse(""" T = int def func() -> T: return 1 """) fdef = mod.body[1] node = fdef.returns du = beniget.DefUseChains() du.visit(mod) du.chains[node] ud = beniget.UseDefChains(du) ud.chains[node]