ml_switcheroo.plugins.utils

Shared Utilities for Plugins.

This module provides common helper functions for AST manipulation and framework detection, decoupling individual plugins from hardcoded lists of libraries. It relies on the HookContext to dynamically resolve framework identities.

Functions

create_dotted_name(→ libcst.BaseExpression)

Creates a CST Attribute chain from a dotted string.

is_framework_module_node(→ bool)

Determines if a CST node represents a known framework namespace root.

Module Contents

ml_switcheroo.plugins.utils.create_dotted_name(name_str: str) libcst.BaseExpression[source]

Creates a CST Attribute chain from a dotted string.

Example: “jax.numpy.add” -> Attribute(value=Attribute(value=Name(“jax”), …), …)

Parameters:

name_str (str) – The dot-separated API path.

Returns:

The constructed AST node.

Return type:

cst.BaseExpression

ml_switcheroo.plugins.utils.is_framework_module_node(node: libcst.CSTNode, ctx: ml_switcheroo.core.hooks.HookContext) bool[source]

Determines if a CST node represents a known framework namespace root.

This is used to distinguish function calls (e.g. torch.add(x)) from method calls (e.g. x.add()). If the receiver x is a variable, this returns False. If the receiver is torch, it returns True.

Logic:
  1. Checks the configured Source and Target frameworks in ctx.

  2. Checks the Global Semantics Registry (loaded adapters) via ctx.semantics.

Decoupling Note:

This logic no longer checks for hardcoded strings like “torch” or “jax”. It relies entirely on the configuration passed in ctx.

Parameters:
  • node (cst.CSTNode) – The node to inspect (e.g. the value of an Attribute).

  • ctx (HookContext) – The execution context containing framework configuration.

Returns:

True if the node is a known framework identifier.

Return type:

bool