Repository: ManimCommunity/manim
File: manim/utils/docbuild/module_parsing.py (Python)

Code before:
# In the following, we will use ``type(xyz) is xyz_type`` instead of
# isinstance checks to make sure no subclasses of the type pass the
# check
# ruff: noqa: E721


def parse_module_attributes() -> tuple[AliasDocsDict, DataDict, TypeVarDict]:
    """Read all files, generate Abstract Syntax Trees from them, and
    extract useful information about the type aliases defined in the
    files: the category they belong to, their definition and their
    description, separating them from the "regular" module attributes.

    Returns
    -------
    ALIAS_DOCS_DICT : `AliasDocsDict`
        A dictionary containing the information from all the type
        aliases in Manim. See `AliasDocsDict` for more information.

    DATA_DICT : `DataDict`
        A dictionary containing the names of all DOCUMENTED
        module-level attributes which are not a :class:`TypeAlias`.
    """
    global ALIAS_DOCS_DICT
    global DATA_DICT
    global TYPEVAR_DICT

    if ALIAS_DOCS_DICT or DATA_DICT or TYPEVAR_DICT:
        return ALIAS_DOCS_DICT, DATA_DICT, TYPEVAR_DICT

    for module_path in MANIM_ROOT.rglob("*.py"):
        module_name = module_path.resolve().relative_to(MANIM_ROOT)
        module_name = list(module_name.parts)
        module_name[-1] = module_name[-1].removesuffix(".py")
        module_name = ".".join(module_name)

        module_content = module_path.read_text(encoding="utf-8")

        # For storing TypeAliases
        module_dict: ModuleLevelAliasDict = {}
        category_dict: AliasCategoryDict | None = None
        alias_info: AliasInfo | None = None

        # For storing TypeVars
        module_typevars: ModuleTypeVarDict = {}

        # For storing regular module attributes
        data_list: list[str] = []
        data_name: str | None = None

        for node in ast.iter_child_nodes(ast.parse(module_content)):
            # If we encounter a string:

Review comment (suggestion):
Don't forget to add a description to the new returned variable (btw I noticed that I missed the `:class:` prefixes for the return values and other stuff, I intend to fix it in another PR):
```suggestion
    
    TYPEVAR_DICT : `TypeVarDict`
        A dictionary containing the definitions of :class:`TypeVar` objects,
        organized by modules.
    """
```

Code after:
# In the following, we will use ``type(xyz) is xyz_type`` instead of
# isinstance checks to make sure no subclasses of the type pass the
# check
# ruff: noqa: E721


def parse_module_attributes() -> tuple[AliasDocsDict, DataDict, TypeVarDict]:
    """Read all files, generate Abstract Syntax Trees from them, and
    extract useful information about the type aliases defined in the
    files: the category they belong to, their definition and their
    description, separating them from the "regular" module attributes.

    Returns
    -------
    ALIAS_DOCS_DICT : :class:`AliasDocsDict`
        A dictionary containing the information from all the type
        aliases in Manim. See :class:`AliasDocsDict` for more information.

    DATA_DICT : :class:`DataDict`
        A dictionary containing the names of all DOCUMENTED
        module-level attributes which are not a :class:`TypeAlias`.

    TYPEVAR_DICT : :class:`TypeVarDict`
        A dictionary containing the definitions of :class:`TypeVar` objects,
        organized by modules.
    """
    global ALIAS_DOCS_DICT
    global DATA_DICT
    global TYPEVAR_DICT

    if ALIAS_DOCS_DICT or DATA_DICT or TYPEVAR_DICT:
        return ALIAS_DOCS_DICT, DATA_DICT, TYPEVAR_DICT

    for module_path in MANIM_ROOT.rglob("*.py"):
        module_name = module_path.resolve().relative_to(MANIM_ROOT)
        module_name = list(module_name.parts)
        module_name[-1] = module_name[-1].removesuffix(".py")
        module_name = ".".join(module_name)

        module_content = module_path.read_text(encoding="utf-8")

        # For storing TypeAliases
        module_dict: ModuleLevelAliasDict = {}
        category_dict: AliasCategoryDict | None = None
        alias_info: AliasInfo | None = None

        # For storing TypeVars
        module_typevars: ModuleTypeVarDict = {}

        # For storing regular module attributes
        data_list: list[str] = []