def _infer_from_metaclass_constructor(cls, func): """Try to infer what the given *func* constructor is building :param astroid.FunctionDef func: A metaclass constructor. Metaclass definitions can be functions, which should accept three arguments, the name of the class, the bases of the class and the attributes. The function could return anything, but usually it should be a proper metaclass. :param astroid.ClassDef cls: The class for which the *func* parameter should generate a metaclass. :returns: The class generated by the function or None, if we couldn't infer it. :rtype: astroid.ClassDef """ context = astroid.context.InferenceContext() class_bases = astroid.List() class_bases.postinit(elts=cls.bases) attrs = astroid.Dict() local_names = [(name, values[-1]) for name, values in cls.locals.items()] attrs.postinit(local_names) builder_args = astroid.Tuple() builder_args.postinit([cls.name, class_bases, attrs]) context.callcontext = astroid.context.CallContext(builder_args) try: inferred = next(func.infer_call_result(func, context), None) except astroid.InferenceError: return None return inferred or None
def infer_random_sample(node, context=None): if len(node.args) != 2: raise astroid.UseInferenceDefault length = node.args[1] if not isinstance(length, astroid.Const): raise astroid.UseInferenceDefault if not isinstance(length.value, int): raise astroid.UseInferenceDefault inferred_sequence = helpers.safe_infer(node.args[0], context=context) if not inferred_sequence: raise astroid.UseInferenceDefault if not isinstance(inferred_sequence, ACCEPTED_ITERABLES_FOR_SAMPLE): raise astroid.UseInferenceDefault if length.value > len(inferred_sequence.elts): # In this case, this will raise a ValueError raise astroid.UseInferenceDefault try: elts = random.sample(inferred_sequence.elts, length.value) except ValueError: raise astroid.UseInferenceDefault new_node = astroid.List( lineno=node.lineno, col_offset=node.col_offset, parent=node.scope() ) new_elts = [ _clone_node_with_lineno(elt, parent=new_node, lineno=new_node.lineno) for elt in elts ] new_node.postinit(new_elts) return iter((new_node,))
def transform(self, node: ast.Call) -> ast.AugAssign: new_node = ast.AugAssign(op="+=") new_value = ast.List(parent=new_node) new_arg = node.args[0] new_arg.parent = new_value new_value.postinit(elts=[new_arg]) new_node.postinit(target=node.func.expr, value=new_value) new_node.target.parent = new_node return new_node
def _nose_tools_trivial_transform(): """Custom transform for the nose.tools module.""" stub = _BUILDER.string_build('''__all__ = []''') all_entries = ['ok_', 'eq_'] for pep8_name, method in _nose_tools_functions(): all_entries.append(pep8_name) stub[pep8_name] = method # Update the __all__ variable, since nose.tools # does this manually with .append. all_assign = stub['__all__'].parent all_object = astroid.List(all_entries) all_object.parent = all_assign all_assign.value = all_object return stub
def list_node(draw, elt=const_node(), **kwargs): """Return a List node with elements drawn from elt. """ node = astroid.List() node.postinit(draw(hs.lists(elt, **kwargs))) return node