ml_switcheroo.core.import_fixer.utils

Utilities for the Import Fixer.

Contains static helper functions for analyzing AST nodes, extracting names, generating signatures for deduplication, and creating CST nodes.

Attributes

REDUNDANT_SEGMENTS

Functions

get_root_name(→ str)

Recursively extracts the root identifier from a Name or Attribute chain.

create_dotted_name(→ Union[libcst.Name, libcst.Attribute])

Creates a CST node structure for a dotted path string.

get_signature(→ str)

Computes a deduplication signature for an AST node (usually an Import statement).

is_docstring(→ bool)

Determines if a statement node represents a module docstring.

is_future_import(→ bool)

Determines if a statement is a from __future__ import ... directive.

Module Contents

ml_switcheroo.core.import_fixer.utils.REDUNDANT_SEGMENTS
ml_switcheroo.core.import_fixer.utils.get_root_name(node: libcst.Name | libcst.Attribute) str[source]

Recursively extracts the root identifier from a Name or Attribute chain.

Parameters:

node – The CST node (e.g., Attribute(value=Name(‘torch’), …)).

Returns:

The root name (e.g., “torch”).

Return type:

str

ml_switcheroo.core.import_fixer.utils.create_dotted_name(name_str: str) libcst.Name | libcst.Attribute[source]

Creates a CST node structure for a dotted path string.

Parameters:

name_str (str) – Dot-separated path (e.g. “jax.numpy”).

Returns:

The constructed AST node.

Return type:

Union[cst.Name, cst.Attribute]

ml_switcheroo.core.import_fixer.utils.get_signature(node: libcst.CSTNode) str[source]

Computes a deduplication signature for an AST node (usually an Import statement).

It normalizes the source code representation to ignore basic formatting differences, allowing detection of duplicate import injections.

Parameters:

node – The CST node to sign.

Returns:

Normalized source code string.

Return type:

str

ml_switcheroo.core.import_fixer.utils.is_docstring(node: libcst.CSTNode, idx: int) bool[source]

Determines if a statement node represents a module docstring.

Parameters:
  • node – The statement node from the module body.

  • idx – The index of this statement in the body list.

Returns:

True if it is a docstring (string expression at index 0).

Return type:

bool

ml_switcheroo.core.import_fixer.utils.is_future_import(node: libcst.CSTNode) bool[source]

Determines if a statement is a from __future__ import … directive.

Parameters:

node – The statement node.

Returns:

True if it is a future import.

Return type:

bool