class ConstructorMutableAttribsLineNumberFixture: definition_is_none = None definition_is_not_a_function = _ast.Pass constructor_with_empty_body = _ast.FunctionDef(name='__init__', body=[]) try: constructor_with_immutable = _ast.FunctionDef( name='__init__', body=[ _ast.Assign(lineno=1, value=_ast.Tuple(elts=[1, 2, 3])), _ast.Assign(lineno=2, value=_ast.Str(s='a')), ], ) except (AttributeError): constructor_with_immutable = _ast.FunctionDef( name='__init__', body=[ _ast.Assign(lineno=1, value=_ast.Tuple(elts=[1, 2, 3])), _ast.Assign(lineno=3, value=_ast.JoinedStr(values=None)), ], ) constructor_with_mutable = _ast.FunctionDef( name='__init__', body=[ _ast.Assign(lineno=1, value=_ast.List(elts=[1, 2, 3])), _ast.Assign(lineno=2, value=_ast.Set(elts=[1, 2, 3])), _ast.Assign(lineno=3, value=_ast.Dict(keys=['a'])), ], )
def visit_Expr(self, expression_node): value = expression_node.value if not isinstance(value, _ast.BinOp) or not isinstance( value.op, _ast.Mult) or not isinstance(value.right, _ast.Call): return expression_node number_of_invocations = value.left if MockAssertionTransformer._value_is_a_wildcard( number_of_invocations): number_of_invocations = _ast.Num(n=-1) target_mock = value.right.func.value target_method = _ast.Str(s=value.right.func.attr) list_of_arguments = [ MockAssertionTransformer._transform_arg_if_wildcard(x) for x in value.right.args ] spread_list_of_arguments = _ast.Starred(value=_ast.List( elts=list_of_arguments, ctx=_ast.Load()), ctx=_ast.Load()) expression_node.value = _ast.Call( func=_ast.Attribute(value=_ast.Name(id='self', ctx=_ast.Load()), attr='_assert_mock', ctx=_ast.Load()), args=[ number_of_invocations, target_mock, target_method, spread_list_of_arguments ], keywords=[]) return expression_node
def BUILD_LIST(self, instr): nitems = instr.oparg nodes = [] list_ = _ast.List(elts=nodes, ctx=_ast.Load(), lineno=instr.lineno, col_offset=0) for i in range(nitems): nodes.insert(0, self.ast_stack.pop()) self.ast_stack.append(list_)
def make_const(i, bytecode): arg = bytecode[i][3] if isinstance(arg, basestring): return i, _ast.Str(arg) elif isinstance(arg, int) or isinstance(arg, float) or isinstance(arg, long): return i, _ast.Num(arg) elif isinstance(arg, dict): return i, _ast.Dict(arg.keys(), arg.values()) elif isinstance(arg, set): return i, _ast.Dict(arg) elif isinstance(arg, tuple): return i, _ast.Tuple(arg, _ast.Load()) elif isinstance(arg, list): return i, _ast.List(arg, _ast.Load()) elif isinstance(arg, bytes): return i, _ast.Bytes(arg) return i, None
def _assign_matrix_form_variables(self, where_function_ast_node): copy_of_body = copy.deepcopy(where_function_ast_node.body) where_function_ast_node.body = [] variables_and_values = WhereBlockFunctions._get_variables_and_values(copy_of_body) # We might be screwing with line numbers here for variable_name, variable_values in variables_and_values.items(): self.spec_metadata.add_feature_variable(self.feature_name, variable_name) where_function_ast_node.body.append( _ast.Assign( targets=[ _ast.Subscript( value=_ast.Name(id='injectable_values', ctx=_ast.Load()), slice=_ast.Index(value=ast_proxy.ast_str(s=variable_name)), ctx=_ast.Store() ) ], value=_ast.List(elts=variable_values, ctx=_ast.Load()) ))
def test_bug_null_in_objspace_type(self): import ast code = ast.Expression( lineno=1, col_offset=1, body=ast.ListComp( lineno=1, col_offset=1, elt=ast.Call(lineno=1, col_offset=1, func=ast.Name(lineno=1, col_offset=1, id='str', ctx=ast.Load(lineno=1, col_offset=1)), args=[ ast.Name(lineno=1, col_offset=1, id='x', ctx=ast.Load(lineno=1, col_offset=1)) ], keywords=[]), generators=[ ast.comprehension( lineno=1, col_offset=1, target=ast.Name(lineno=1, col_offset=1, id='x', ctx=ast.Store(lineno=1, col_offset=1)), iter=ast.List( lineno=1, col_offset=1, elts=[ast.Num(lineno=1, col_offset=1, n=23)], ctx=ast.Load( lineno=1, col_offset=1, )), ifs=[]) ])) compile(code, '<template>', 'eval')
def List(*items, **kwargs): """Creates an _ast.List node. Automatically adjusts inner ctx attrs. Args: *items: The items in the list. **kwargs: Only recognized kwarg is 'ctx_type', which controls the ctx type of the list. See CtxEnum. Returns: An _ast.List node. """ ctx_type = kwargs.pop('ctx_type', CtxEnum.LOAD) for item in items: if isinstance(item, _ast.Name): item.ctx = GetCtx(ctx_type) elif isinstance(item, _ast.Attribute): name_node = _LeftmostNodeInDotVar(item) name_node.ctx = GetCtx(ctx_type) ctx = GetCtx(ctx_type) return _ast.List(elts=list(items), ctx=ctx)
def list_expr(elts: List[_ast.expr], ctx: _ast.expr_context) -> _ast.List: return _ast.List(elts=elts, ctx=ctx)
def test_return_value_is_filled_list(): # when method return ['1'] assert ReturnedExpression( _ast.Return(value=_ast.List(elts=['1'], ctx=_ast.Load()), lineno=1), ).value_not_none() is True
def test_return_value_is_empty_list(): # when method return [] assert ReturnedExpression( _ast.Return(value=_ast.List(elts=[], ctx=_ast.Load()), lineno=1), ).value_not_none() is False