Every line of 'python list comprehension if else' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.
1042 @utils.check_messages("using-constant-test", "missing-parentheses-for-call-in-test") 1043 def visit_comprehension(self, node): 1044 if node.ifs: 1045 for if_test in node.ifs: 1046 self._check_using_constant_test(node, if_test)
466 def p_if_stmt(p): 467 'if_stmt : IF test COLON suite' 468 p[0] = ast.If([(p[2], p[4])], None)
45 def create_comprehension(for_node: ast.For) -> ast.comprehension: 46 return ast.comprehension(target=for_node.target, iter=for_node.iter, ifs=[])
1276 def visit_ComprehensionNode(self, node): 1277 if node.expr_scope: 1278 self.env_stack.append(self.env) 1279 self.env = node.expr_scope 1280 # Skip append node here 1281 self._visit(node.loop) 1282 if node.expr_scope: 1283 self.env = self.env_stack.pop() 1284 return node
231 def is_comprehension(x): 232 """Returns true if this is a comprehension type.""" 233 return isinstance(x, (ObjectComprehension, SetComprehension, ArrayComprehension))
446 def visit_comprehension(self, node, parent): 447 """visit a Comprehension node by returning a fresh instance of it""" 448 newnode = nodes.Comprehension(parent) 449 newnode.postinit( 450 self.visit(node.target, newnode), 451 self.visit(node.iter, newnode), 452 [self.visit(child, newnode) for child in node.ifs], 453 getattr(node, "is_async", None), 454 ) 455 return newnode
79 def visit_If(self, n: ca.If) -> str: 80 n2 = n 81 if ( 82 n.iffalse 83 and isinstance(n.iffalse, ca.Compound) 84 and n.iffalse.block_items 85 and len(n.iffalse.block_items) == 1 86 and isinstance(n.iffalse.block_items[0], ca.If) 87 ): 88 n2 = ca.If(cond=n.cond, iftrue=n.iftrue, iffalse=n.iffalse.block_items[0]) 89 return super().visit_If(n2) # type: ignore
332 def visitIf(self, n): 333 if not n.else_: 334 return 335 visits = [] 336 for test, code in n.tests: 337 visits.append(walk(code, Visitor(self.defines, self.uses))) 338 visits.append(walk(n.else_, Visitor(self.defines, self.uses))) 339 # compute the intersection of defines 340 self.defines = intersect([v.defines for v in visits]) 341 # compute the union of uses, perserving first occurances 342 union = {} 343 visits.reverse() 344 for v in visits: 345 union.update(v.uses) 346 union.update(self.uses) 347 self.uses = union
691 def handle_gen_for (self, node): 692 return self._handle_comp_for(node)
1216 @specialize.arg(2) 1217 def comprehension_helper(self, comp_node, 1218 handle_source_expr_meth="handle_expr", 1219 for_type=syms.comp_for, if_type=syms.comp_if, 1220 iter_type=syms.comp_iter, 1221 comp_fix_unamed_tuple_location=False): 1222 handle_source_expression = getattr(self, handle_source_expr_meth) 1223 fors_count = self.count_comp_fors(comp_node, for_type, if_type) 1224 comps = [] 1225 for i in range(fors_count): 1226 for_node = comp_node.get_child(1) 1227 for_targets = self.handle_exprlist(for_node, ast.Store) 1228 expr = handle_source_expression(comp_node.get_child(3)) 1229 assert isinstance(expr, ast.expr) 1230 if for_node.num_children() == 1: 1231 comp = ast.comprehension(for_targets[0], expr, None) 1232 else: 1233 col = comp_node.get_column() 1234 line = comp_node.get_lineno() 1235 # Modified in python2.7, see http://bugs.python.org/issue6704 1236 if comp_fix_unamed_tuple_location: 1237 expr_node = for_targets[0] 1238 assert isinstance(expr_node, ast.expr) 1239 col = expr_node.col_offset 1240 line = expr_node.lineno 1241 target = ast.Tuple(for_targets, ast.Store, line, col) 1242 comp = ast.comprehension(target, expr, None) 1243 if comp_node.num_children() == 5: 1244 comp_node = comp_iter = comp_node.get_child(4) 1245 assert comp_iter.type == iter_type 1246 ifs_count = self.count_comp_ifs(comp_iter, for_type) 1247 if ifs_count: 1248 ifs = [] 1249 for j in range(ifs_count): 1250 comp_node = comp_if = comp_iter.get_child(0) 1251 ifs.append(self.handle_expr(comp_if.get_child(1))) 1252 if comp_if.num_children() == 3: 1253 comp_node = comp_iter = comp_if.get_child(2) 1254 comp.ifs = ifs 1255 if comp_node.type == iter_type: 1256 comp_node = comp_node.get_child(0) 1257 assert isinstance(comp, ast.comprehension) 1258 comps.append(comp) 1259 return comps