def test_2nd_order_lookup(): 'Seen in the wild to generate an out-of-scope error' r = (cms_aod_dataset().Select( lambda e: { "m": e.Muons("muons"), "p": e.Vertex("offlinePrimaryVertices")[0].position() }).Select( lambda i: i.m.Where(lambda m: m.isPFMuon( ) and m.isPFIsolationValid() and isNonnull(m.globalTrack( )) and abs((m.globalTrack()).dxy(i.p)) < 0.5 and abs( (m.globalTrack()).dz(i.p)) < 1.).Select(lambda m: m.p()), ).value()) lines = get_lines_of_code(r) print_lines(lines) # Make sure the vertex line isn't used after it goes out of scope vertex_decl_line = find_line_with('edm::Handle<reco::VertexCollection>', lines) vertex_variable_name = lines[vertex_decl_line].split(' ')[-1].strip(';') closing_scope = find_next_closing_bracket(lines[vertex_decl_line:]) vertex_used_too_late = find_line_with(vertex_variable_name, lines[vertex_decl_line + closing_scope:], throw_if_not_found=False) if vertex_used_too_late != -1: print('Here is where it is used and down') print_lines(lines[closing_scope + vertex_decl_line + vertex_used_too_late:]) assert vertex_used_too_late == -1
def test_count_after_double_sequence_with_filter(): r = atlas_xaod_dataset() \ .Select('lambda e: e.Jets("AllMyJets").SelectMany(lambda j: e.Tracks("InnerTracks").Where(lambda t: t.pt()>10.0)).Count()') \ .value() lines = get_lines_of_code(r) print_lines(lines) # Make sure there is just one for loop in here. assert 2 == ["for" in ln for ln in lines].count(True) # Make sure the +1 happens after the for, and before another } bracket. num_for = find_line_with("if", lines) num_inc = find_line_with("+1", lines[num_for:]) num_close = find_next_closing_bracket(lines[num_for:]) assert num_close > num_inc
def test_count_after_single_sequence_of_sequence_with_useless_where(): r = atlas_xaod_dataset() \ .Select('lambda e: e.Jets("AllMyJets").Select(lambda j: e.Tracks("InnerTracks").Where(lambda pt: pt > 10.0)).Count()') \ .value() lines = get_lines_of_code(r) print_lines(lines) # Make sure there is just one for loop in here. l_increment = find_line_with('+1', lines) block_headers = find_open_blocks(lines[:l_increment]) assert 1 == ["for" in ln for ln in block_headers].count(True) # Make sure the +1 happens after the for, and before another } bracket. num_for = find_line_with("for", lines) num_inc = find_line_with("+1", lines[num_for:]) num_close = find_next_closing_bracket(lines[num_for:]) assert num_close > num_inc