Skip to content

parse_python_code

Parse a source string in order to obtain an astroid AST from it.

Parameters:

Name Type Description Default
code str

The code for the module.

required
module_name str

The name for the module, if any

''
path str | None

The path for the module

None
ast_builder AstroidBuilder

The Astroid builder to use

None
Source code in src/library_analyzer/utils/_parsing.py
def parse_python_code(
    code: str,
    module_name: str = "",
    path: str | None = None,
    ast_builder: AstroidBuilder = None,
) -> astroid.Module:
    """Parse a source string in order to obtain an astroid AST from it.

    Parameters
    ----------
    code : str
        The code for the module.
    module_name : str
        The name for the module, if any
    path : str | None
        The path for the module
    ast_builder : AstroidBuilder
        The Astroid builder to use
    """
    if ast_builder is None:
        ast_builder = AstroidBuilder()

    code = textwrap.dedent(code)
    return ast_builder.string_build(code, modname=module_name, path=path)