keysites.py

clew/query/keysites.py

Same-key sites: the other symbols carrying a property key a function writes (F1-mm; 2.0.0).

same_key_sites

list[KeySite] clew.query.keysites.same_key_sites(sqlite3.Connection conn, str file, int|None start, int|None end)

Other non-test symbols carrying the keys one function writes.

clew/query/keysites.py:38

Source · 32 lines
def same_key_sites(
    conn: sqlite3.Connection, file: str, start: int | None, end: int | None
) -> list[KeySite]:
    """THE FUNCTION'S KEYS ARE ITS BODY'S, by span rather than by holder name, so a key written in
    a nested arrow function still belongs to the function a reader asked about.

    @brief Find same-key sites.
    @return Sites.
    """
    if not table_exists(conn, "property_keys") or not start or not end:
        return []
    tests = (
        "AND k.path_rowid NOT IN (SELECT path_rowid FROM test_scope)"
        if table_exists(conn, "test_scope")
        else ""
    )
    own = conn.execute(
        "SELECT DISTINCT k.key FROM property_keys k JOIN path p ON p.rowid = k.path_rowid "
        "WHERE p.name = ? AND k.line BETWEEN ? AND ?",
        (file, start, end),
    ).fetchall()
    sites: list[KeySite] = []
    for (key,) in own:
        rows = conn.execute(
            "SELECT k.holder, p.name, MIN(k.line) FROM property_keys k JOIN path p ON p.rowid = k.path_rowid "  # noqa: S608
            f"WHERE k.key = ? AND NOT (p.name = ? AND k.line BETWEEN ? AND ?) {tests} "
            "GROUP BY k.holder, p.name ORDER BY p.name, 3",
            (key, file, start, end),
        ).fetchall()
        if len(rows) <= KEY_RARITY:
            sites += [KeySite(key=key, holder=h, file=f, line=int(ln)) for h, f, ln in rows]
    return sorted(sites, key=lambda s: (s.key, s.file, s.line))

1 more lines, not shown

Activity · 10 steps
lines 47–47◇ if not table_exists(conn, "property_keys") or not start or not endlines 48–48return []lines 49–53tests = ( "AND k.path_rowid NOT IN (SELECT path_rowid FROM test_scope)" if table_exists(conn, "test_scope") else "" )lines 54–58own = conn.execute( "SELECT DISTINCT k.key FROM property_keys k JOIN path p ON p.rowid = k.path_rowid " "WHERE p.name = ? AND k.line BETWEEN ? AND ?", (file, start, end), ).fetchall()lines 59–59sites: list[KeySite] = []lines 60–60◇ for (key,) in ownlines 61–66rows = conn.execute( "SELECT k.holder, p.name, MIN(k.line) FROM property_keys k JOIN path p ON p.rowid = k.path_rowid " # noqa: S608 f"WHERE k.key = ? AND NOT (p.name = ? AND k.line BETWEEN ? AND ?) {tests} " "GROUP BY k.holder, p.name ORDER BY p.name, 3", (key, file, start, end), ).fetchall()lines 67–67◇ if len(rows) <= KEY_RARITYlines 68–68sites += [KeySite(key=key, holder=h, file=f, line=int(ln)) for h, f, ln in rows]lines 69–69return sorted(sites, key=lambda s: (s.key, s.file, s.line))

Requirements

REQ-DDB-QUERY-004