injector_fw.utils¶
Utilities for AST Node Construction and Inspection.
This module provides helper functions to convert Python runtime objects (dictionaries, lists, primitives) into LibCST nodes, as well as utilities for inspecting import definitions.
Functions¶
|
Recursively extracts the root package name from a CST node. |
|
Checks if a statement node represents a module docstring. |
|
Checks if a statement is a from __future__ import ... directive. |
|
Recursively converts a python primitive or container to a CST node. |
Module Contents¶
- injector_fw.utils.get_import_root(node: libcst.Name | libcst.Attribute) str¶
Recursively extracts the root package name from a CST node.
- Parameters:
node – The AST node representing the module name.
- Returns:
The root package identifier (e.g. “scipy” from “scipy.special”).
- Return type:
str
- injector_fw.utils.is_docstring(node: libcst.CSTNode, idx: int) bool¶
Checks if a statement node represents a module docstring.
- Parameters:
node – The statement node to check.
idx – The index of the statement within the module body.
- Returns:
True if it is a docstring (Expr containing String at index 0).
- Return type:
bool
- injector_fw.utils.is_future_import(node: libcst.CSTNode) bool¶
Checks if a statement is a from __future__ import … directive.
- Parameters:
node – The statement node to check.
- Returns:
True if it is a future import.
- Return type:
bool
- injector_fw.utils.convert_to_cst_literal(val: Any) libcst.BaseExpression¶
Recursively converts a python primitive or container to a CST node. Robustly handles strings using standard JSON encoding to prevent syntax errors and ensure double-quotes are used (matching test expectations).
- Parameters:
val – The python value to convert.
- Returns:
The literal node.
- Return type:
cst.BaseExpression