def visit_call(self, node, parent): """visit a CallFunc node by returning a fresh instance of it""" newnode = nodes.Call(node.lineno, node.col_offset, parent) starargs = _visit_or_none(node, 'starargs', self, newnode) kwargs = _visit_or_none(node, 'kwargs', self, newnode) args = [self.visit(child, newnode) for child in node.args] if node.keywords: keywords = [self.visit(child, newnode) for child in node.keywords] else: keywords = None if starargs: new_starargs = nodes.Starred(col_offset=starargs.col_offset, lineno=starargs.lineno, parent=starargs.parent) new_starargs.postinit(value=starargs) args.append(new_starargs) if kwargs: new_kwargs = nodes.Keyword(arg=None, col_offset=kwargs.col_offset, lineno=kwargs.lineno, parent=kwargs.parent) new_kwargs.postinit(value=kwargs) if keywords: keywords.append(new_kwargs) else: keywords = [new_kwargs] newnode.postinit(self.visit(node.func, newnode), args, keywords) return newnode
def visit_call(self, node, parent, assign_ctx=None): """visit a CallFunc node by returning a fresh instance of it""" newnode = new.Call() _lineno_parent(node, newnode, parent) newnode.func = self.visit(node.func, newnode, assign_ctx) args = [self.visit(child, newnode, assign_ctx) for child in node.args] starargs = _visit_or_none(node, 'starargs', self, newnode, assign_ctx) kwargs = _visit_or_none(node, 'kwargs', self, newnode, assign_ctx) keywords = None if node.keywords: keywords = [self.visit(child, newnode, assign_ctx) for child in node.keywords] if starargs: new_starargs = new.Starred() new_starargs.col_offset = starargs.col_offset new_starargs.lineno = starargs.lineno new_starargs.parent = starargs.parent new_starargs.value = starargs args.append(new_starargs) if kwargs: new_kwargs = new.Keyword() new_kwargs.arg = None new_kwargs.col_offset = kwargs.col_offset new_kwargs.lineno = kwargs.lineno new_kwargs.parent = kwargs.parent new_kwargs.value = kwargs if keywords: keywords.append(new_kwargs) else: keywords = [new_kwargs] newnode.args = args newnode.keywords = keywords return newnode
def test_function_model(self) -> None: ast_nodes = builder.extract_node( ''' def func(a=1, b=2): """test""" func.__name__ #@ func.__doc__ #@ func.__qualname__ #@ func.__module__ #@ func.__defaults__ #@ func.__dict__ #@ func.__globals__ #@ func.__code__ #@ func.__closure__ #@ func.__init__ #@ func.__new__ #@ func.__subclasshook__ #@ func.__str__ #@ func.__sizeof__ #@ func.__repr__ #@ func.__reduce__ #@ func.__reduce_ex__ #@ func.__lt__ #@ func.__eq__ #@ func.__gt__ #@ func.__format__ #@ func.__delattr___ #@ func.__getattribute__ #@ func.__hash__ #@ func.__dir__ #@ func.__class__ #@ func.__setattr__ #@ ''', module_name="fake_module", ) assert isinstance(ast_nodes, list) name = next(ast_nodes[0].infer()) self.assertIsInstance(name, astroid.Const) self.assertEqual(name.value, "func") doc = next(ast_nodes[1].infer()) self.assertIsInstance(doc, astroid.Const) self.assertEqual(doc.value, "test") qualname = next(ast_nodes[2].infer()) self.assertIsInstance(qualname, astroid.Const) self.assertEqual(qualname.value, "fake_module.func") module = next(ast_nodes[3].infer()) self.assertIsInstance(module, astroid.Const) self.assertEqual(module.value, "fake_module") defaults = next(ast_nodes[4].infer()) self.assertIsInstance(defaults, astroid.Tuple) self.assertEqual([default.value for default in defaults.elts], [1, 2]) dict_ = next(ast_nodes[5].infer()) self.assertIsInstance(dict_, astroid.Dict) globals_ = next(ast_nodes[6].infer()) self.assertIsInstance(globals_, astroid.Dict) for ast_node in ast_nodes[7:9]: self.assertIs(next(ast_node.infer()), astroid.Uninferable) init_ = next(ast_nodes[9].infer()) assert isinstance(init_, bases.BoundMethod) init_result = next(init_.infer_call_result(nodes.Call())) assert isinstance(init_result, nodes.Const) assert init_result.value is None new_ = next(ast_nodes[10].infer()) assert isinstance(new_, bases.BoundMethod) # The following nodes are just here for theoretical completeness, # and they either return Uninferable or raise InferenceError. for ast_node in ast_nodes[11:26]: inferred = next(ast_node.infer()) assert inferred is util.Uninferable for ast_node in ast_nodes[26:27]: with pytest.raises(InferenceError): inferred = next(ast_node.infer())
def test_module_model(self) -> None: ast_nodes = builder.extract_node(""" import xml xml.__path__ #@ xml.__name__ #@ xml.__doc__ #@ xml.__file__ #@ xml.__spec__ #@ xml.__loader__ #@ xml.__cached__ #@ xml.__package__ #@ xml.__dict__ #@ xml.__init__ #@ xml.__new__ #@ xml.__subclasshook__ #@ xml.__str__ #@ xml.__sizeof__ #@ xml.__repr__ #@ xml.__reduce__ #@ xml.__setattr__ #@ xml.__reduce_ex__ #@ xml.__lt__ #@ xml.__eq__ #@ xml.__gt__ #@ xml.__format__ #@ xml.__delattr___ #@ xml.__getattribute__ #@ xml.__hash__ #@ xml.__dir__ #@ xml.__call__ #@ xml.__closure__ #@ """) assert isinstance(ast_nodes, list) path = next(ast_nodes[0].infer()) self.assertIsInstance(path, astroid.List) self.assertIsInstance(path.elts[0], astroid.Const) self.assertEqual(path.elts[0].value, xml.__path__[0]) name = next(ast_nodes[1].infer()) self.assertIsInstance(name, astroid.Const) self.assertEqual(name.value, "xml") doc = next(ast_nodes[2].infer()) self.assertIsInstance(doc, astroid.Const) self.assertEqual(doc.value, xml.__doc__) file_ = next(ast_nodes[3].infer()) self.assertIsInstance(file_, astroid.Const) self.assertEqual(file_.value, xml.__file__.replace(".pyc", ".py")) for ast_node in ast_nodes[4:7]: inferred = next(ast_node.infer()) self.assertIs(inferred, astroid.Uninferable) package = next(ast_nodes[7].infer()) self.assertIsInstance(package, astroid.Const) self.assertEqual(package.value, "xml") dict_ = next(ast_nodes[8].infer()) self.assertIsInstance(dict_, astroid.Dict) init_ = next(ast_nodes[9].infer()) assert isinstance(init_, bases.BoundMethod) init_result = next(init_.infer_call_result(nodes.Call())) assert isinstance(init_result, nodes.Const) assert init_result.value is None new_ = next(ast_nodes[10].infer()) assert isinstance(new_, bases.BoundMethod) # The following nodes are just here for theoretical completeness, # and they either return Uninferable or raise InferenceError. for ast_node in ast_nodes[11:28]: with pytest.raises(InferenceError): next(ast_node.infer())