def visit_module(self, node, modname, modpath, package): """visit a Module node by returning a fresh instance of it""" node, doc = self._get_doc(node) newnode = nodes.Module( name=modname, doc=doc, file=modpath, path=[modpath], package=package, parent=None, ) newnode.postinit([self.visit(child, newnode) for child in node.body]) return newnode
def test_relative_to_absolute_name(self): # package mod = nodes.Module('very.multi.package', 'doc') mod.package = True modname = mod.relative_to_absolute_name('utils', 1) self.assertEqual(modname, 'very.multi.package.utils') modname = mod.relative_to_absolute_name('utils', 2) self.assertEqual(modname, 'very.multi.utils') modname = mod.relative_to_absolute_name('utils', 0) self.assertEqual(modname, 'very.multi.package.utils') modname = mod.relative_to_absolute_name('', 1) self.assertEqual(modname, 'very.multi.package') # non package mod = nodes.Module('very.multi.module', 'doc') mod.package = False modname = mod.relative_to_absolute_name('utils', 0) self.assertEqual(modname, 'very.multi.utils') modname = mod.relative_to_absolute_name('utils', 1) self.assertEqual(modname, 'very.multi.utils') modname = mod.relative_to_absolute_name('utils', 2) self.assertEqual(modname, 'very.utils') modname = mod.relative_to_absolute_name('', 1) self.assertEqual(modname, 'very.multi')
def functiondef_node(draw, name=None, annotated=False, returns=False): name = name or draw(valid_identifier()) args = draw(arguments_node(annotated)) body = [] returns_node = nodes.Return() arg_node, arg_type_node = draw( hs.sampled_from(list(zip(args.args, args.annotations)))) if returns: returns_node.postinit(arg_node) else: returns_node.postinit(const_node(None)) body.append(returns_node) node = nodes.FunctionDef(name=name) node.parent = nodes.Module("Default", None) node.postinit(args, body, None, arg_type_node) return node
def testExtensionModules(self): transformer = MANAGER._transform for extender, _ in transformer.transforms[nodes.Module]: n = nodes.Module('__main__', None) extender(n)
def build_module(name, doc=None): """create and initialize an astroid Module node""" node = nodes.Module(name, doc, pure_python=False) node.package = False node.parent = None return node
def build_namespace_package_module(name, path): return nodes.Module(name, doc="", path=path, package=True)
def build_module(name: str, doc: Optional[str] = None) -> nodes.Module: """create and initialize an astroid Module node""" node = nodes.Module(name, doc, pure_python=False) node.package = False node.parent = None return node
def build_namespace_package_module(name: str, path: List[str]) -> nodes.Module: return nodes.Module(name, doc="", path=path, package=True)