ml_switcheroo.discovery.harvester¶

Semantic Harvester for extracting knowledge from manual tests.

This module provides the SemanticHarvester, which inspects Python test files written by developers (Human Override Workflow). It analyzes successful test calls to reverse-engineer valid argument mappings for the Knowledge Base.

Logic:
  1. Parse a test file’s AST.

  2. Scan for imports to resolve aliases (e.g. import jax.numpy as jnp).

  3. Identify test functions (e.g., test_matmul).

  4. Find calls to the target framework (e.g., jax.numpy.matmul).

  5. Correlate input arguments to target parameters using: - Naming Convention: np_x matches standard arg x. - Explicit Keywords: kwargs match standard arg names directly. - Value-Based Inference: Matches literals (e.g. 1, True) to

    type hints defined in the Semantics (e.g., axis: int).

  6. Construct and return valid mapping dictionaries.

Classes¶

ImportScanner

Scans AST for imports relevant to the target framework to build an alias map.

SemanticHarvester

Analyzes Python source code to extract valid API signatures from usage.

TargetCallVisitor

Helper AST walker to find specific API calls and extract arguments.

Module Contents¶

class ml_switcheroo.discovery.harvester.ImportScanner(root_fw: str)¶

Bases: ast.NodeVisitor

Scans AST for imports relevant to the target framework to build an alias map.

root_fw¶
aliases¶
visit_Import(node: ast.Import) → Any¶
visit_ImportFrom(node: ast.ImportFrom) → Any¶
class ml_switcheroo.discovery.harvester.SemanticHarvester(semantics: ml_switcheroo.semantics.manager.SemanticsManager, target_fw: str = 'jax')¶

Analyzes Python source code to extract valid API signatures from usage.

semantics¶
target_fw = 'jax'¶
harvest_file(file_path: pathlib.Path, dry_run: bool = False) → int¶

Scans a file, extracts mappings, and updates the semantics JSONs.

Parameters:
  • file_path – Path to the python test file.

  • dry_run – If True, does not write changes to disk.

Returns:

Number of definitions updated.

Return type:

int

class ml_switcheroo.discovery.harvester.TargetCallVisitor(target_api: str, aliases: Dict[str, str], std_args_info: List[Any])¶

Bases: ast.NodeVisitor

Helper AST walker to find specific API calls and extract arguments.

Implements Value-Based Inference: - Matches naming conventions (np_x). - Matches literal types (e.g. 1 is int) to Semantic Types (axis: int).

target_api¶
aliases¶
std_args_info = []¶
mappings: Dict[str, str] | None = None¶
visit_Call(node: ast.Call) → Any¶

Inspects calls. Check if they match target_api and extract args.