ml_switcheroo.generated_tests.inputs

Input Value Code Generation.

This module handles the logic for generating Python source code strings that instantiate test data explicitly in the generated verification scripts. It parses argument definitions and emits code like random.randint(1, 5) or np.random.randn(…).

Features:

  • Infers generation logic from explicit types.

  • Default Value Inference: Uses default values to guess types when explicit hints are missing (e.g. default=True implies boolean generation).

  • Constraint Inference: Uses min/max to govern random ranges.

Functions

parse_arg_def(→ Dict[str, Any])

Normalizes a heterogeneous argument definition into a standard dictionary.

generate_input_value_code(→ str)

Generates Python code string to instantiate inputs based on type/constraints.

Module Contents

ml_switcheroo.generated_tests.inputs.parse_arg_def(arg: str | tuple | dict) Dict[str, Any]

Normalizes a heterogeneous argument definition into a standard dictionary.

Extracts default, min, max to help downstream inference. Defaults un-typed arguments to ‘Array’ to ensure gradient checks in test generators correctly identify implicit tensors.

Parameters:

arg – An argument definition. Can be a string (“x”), a tuple (“x”, “int”), or a dictionary (ODL schema).

Returns:

Normalized dict with keys ‘name’, ‘type’, ‘default’, and constraints.

ml_switcheroo.generated_tests.inputs.generate_input_value_code(name: str, arg_def: str | Dict[str, Any]) str

Generates Python code string to instantiate inputs based on type/constraints.

Prioritizes:

  1. Explicit options list.

  2. Explicit type hint.

  3. Inference from default value if type is Any.

  4. Inference from min/max constraints.

  5. Heuristics based on argument name.

Parameters:
  • name – The argument name (used for heuristics if type is missing).

  • arg_def – The normalized argument definition dictionary or type string.

Returns:

Valid Python source code expression (e.g., “random.randint(0, 5)”).