Skip to content

infer_purity

Infer the purity of functions.

Given the code of a module, this function infers the purity of the functions inside a module. It uses the PurityAnalyzer to determine the purity of the functions in a module.

Parameters:

Name Type Description Default
code str | None

The source code of the module. If None is provided, the package data must be provided (or else an exception is raised).

required
module_name str

The name of the module, by default "".

''
path str | None

The path of the module, by default None.

None
results dict[NodeID, dict[NodeID, PurityResult]] | None

The results of all previously analyzed modules. The key is the NodeID of the module, the value is a dictionary of the purity results of the functions in the module. After the analysis of the module, the results are saved in this dictionary. All imported modules are saved in this dictionary too for further runtime reduction. Is None if no results are available.

None
package_data PackageData | None

The module data of all modules the package. If provided, the references are resolved with the package data, else the module data is collected first. It is used for the inference of the purity between modules in the package.

None

Returns:

Name Type Description
purity_results dict[NodeID, dict[NodeID, PurityResult]]

The purity results of the functions in the module. The key is the NodeID of the module, the value is a dictionary of the purity results of the functions in the module.

Source code in src/library_analyzer/processing/api/purity_analysis/_infer_purity.py
def infer_purity(
    code: str | None,
    module_name: str = "",
    path: str | None = None,
    results: dict[NodeID, dict[NodeID, PurityResult]] | None = None,
    package_data: PackageData | None = None,
) -> dict[NodeID, dict[NodeID, PurityResult]]:
    """
    Infer the purity of functions.

    Given the code of a module, this function infers the purity of the functions inside a module.
    It uses the PurityAnalyzer to determine the purity of the functions in a module.

    Parameters
    ----------
    code :
        The source code of the module.
        If None is provided, the package data must be provided (or else an exception is raised).
    module_name :
        The name of the module, by default "".
    path :
        The path of the module, by default None.
    results :
        The results of all previously analyzed modules.
        The key is the NodeID of the module, the value is a dictionary of the purity results of the functions in the module.
        After the analysis of the module, the results are saved in this dictionary.
        All imported modules are saved in this dictionary too for further runtime reduction.
        Is None if no results are available.
    package_data :
        The module data of all modules the package.
        If provided, the references are resolved with the package data, else the module data is collected first.
        It is used for the inference of the purity between modules in the package.

    Returns
    -------
    purity_results :
        The purity results of the functions in the module.
        The key is the NodeID of the module, the value is a dictionary of the purity results of the functions in the module.
    """
    purity_analyzer = PurityAnalyzer(code, module_name, path, results, package_data)
    return purity_analyzer.current_purity_results