Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/minds/changelog/mngr-inline-fn-ratchet-doublecount.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Re-trimmed the inline-function ratchet count from 9 to 7 after a shared-checker bug fix stopped double-counting a doubly-nested function. No minds source changed; the two removed counts were the same function counted once per enclosing function.
2 changes: 1 addition & 1 deletion apps/minds/imbue/minds/test_ratchets.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def test_prevent_inline_functions() -> None:
# ported Sentry HTTP transport's ``_send_request`` (it closes over the
# envelope being sent). The recorded count reflects the actual finder count
# for the current tree.
rc.check_inline_functions(_DIR, snapshot(9))
rc.check_inline_functions(_DIR, snapshot(7))


def test_prevent_underscore_imports() -> None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed the inline-function ratchet (`find_inline_functions`) double-counting a function nested two or more levels deep. It walked every `FunctionDef` in a file, including nested ones, and descended into all of each one's descendants, so a function with two enclosing functions was emitted once per ancestor. The recorded count therefore overstated the real number of inline functions. Nested defs are now collected keyed by source position and counted once. Only projects that actually nest functions two deep are affected; across the whole monorepo the sole recorded count that changes is `apps/minds` (from 9 to 7 before any code change).
31 changes: 18 additions & 13 deletions libs/imbue_common/imbue/imbue_common/ratchet_testing/ratchets.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,24 +235,29 @@ def find_inline_functions(
for file_path in file_paths:
func_def_nodes = get_ast_nodes_of_type(file_path, ast.FunctionDef)

# ast.walk from every FunctionDef reaches a function nested N deep once per
# ancestor, so collect the nested defs keyed by source position to count each once.
nested_by_position: dict[tuple[int, int], ast.FunctionDef] = {}
for node in func_def_nodes:
# Walk within each FunctionDef to find nested functions
for inner_node in ast.walk(node):
if inner_node is not node and isinstance(inner_node, ast.FunctionDef):
# Skip decorator wrapper functions that use @functools.wraps
if _has_functools_wraps_decorator(inner_node):
continue
nested_by_position[(inner_node.lineno, inner_node.col_offset)] = inner_node

start_line = LineNumber(inner_node.lineno)
end_line = LineNumber(inner_node.end_lineno if inner_node.end_lineno else inner_node.lineno)
for inner_node in nested_by_position.values():
# Skip decorator wrapper functions that use @functools.wraps
if _has_functools_wraps_decorator(inner_node):
continue

chunk = RatchetMatchChunk(
file_path=file_path,
matched_content=f"inline function '{inner_node.name}' at line {start_line}",
start_line=start_line,
end_line=end_line,
)
chunks.append(chunk)
start_line = LineNumber(inner_node.lineno)
end_line = LineNumber(inner_node.end_lineno if inner_node.end_lineno else inner_node.lineno)

chunk = RatchetMatchChunk(
file_path=file_path,
matched_content=f"inline function '{inner_node.name}' at line {start_line}",
start_line=start_line,
end_line=end_line,
)
chunks.append(chunk)

sorted_chunks = sorted(chunks, key=lambda c: (str(c.file_path), c.start_line))
return tuple(sorted_chunks)
Expand Down