Skip to content

Loaders¤

Main API¤

load ¤

load(
    objspec: str | Path | None = None,
    /,
    *,
    submodules: bool = True,
    try_relative_path: bool = True,
    extensions: Extensions | None = None,
    search_paths: Sequence[str | Path] | None = None,
    docstring_parser: DocstringStyle | Parser | None = None,
    docstring_options: DocstringOptions | None = None,
    lines_collection: LinesCollection | None = None,
    modules_collection: ModulesCollection | None = None,
    allow_inspection: bool = True,
    force_inspection: bool = False,
    store_source: bool = True,
    find_stubs_package: bool = False,
    resolve_aliases: bool = False,
    resolve_external: bool | None = None,
    resolve_implicit: bool = False,
) -> Object | Alias

Load and return a Griffe object.

In Griffe's context, loading means:

  • searching for a package, and finding it on the file system or as a builtin module (see the ModuleFinder class for more information)
  • extracting information from each of its (sub)modules, by either parsing the source code (see the visit function) or inspecting the module at runtime (see the inspect function)

The extracted information is stored in a collection of modules, which can be queried later. Each collected module is a tree of objects, representing the structure of the module. See the Module, Class, Function, Attribute, and TypeAlias classes for more information.

The main class used to load modules is GriffeLoader. Convenience functions like this one and load_git are also available.

Example
import griffe

module = griffe.load(...)

This is a shortcut for:

from griffe import GriffeLoader

loader = GriffeLoader(...)
module = loader.load(...)

See the documentation for the loader: GriffeLoader.

Parameters:

  • objspec ¤

    (str | Path | None, default: None ) –

    The Python path of an object, or file path to a module.

  • submodules ¤

    (bool, default: True ) –

    Whether to recurse on the submodules. This parameter only makes sense when loading a package (top-level module).

  • try_relative_path ¤

    (bool, default: True ) –

    Whether to try finding the module as a relative path.

  • extensions ¤

    (Extensions | None, default: None ) –

    The extensions to use.

  • search_paths ¤

    (Sequence[str | Path] | None, default: None ) –

    The paths to search into.

  • docstring_parser ¤

    (DocstringStyle | Parser | None, default: None ) –

    The docstring parser to use. By default, no parsing is done.

  • docstring_options ¤

    (DocstringOptions | None, default: None ) –

    Docstring parsing options.

  • lines_collection ¤

    (LinesCollection | None, default: None ) –

    A collection of source code lines.

  • modules_collection ¤

    (ModulesCollection | None, default: None ) –

    A collection of modules.

  • allow_inspection ¤

    (bool, default: True ) –

    Whether to allow inspecting modules when visiting them is not possible.

  • force_inspection ¤

    (bool, default: False ) –

    Whether to force using dynamic analysis when loading data.

  • store_source ¤

    (bool, default: True ) –

    Whether to store code source in the lines collection.

  • find_stubs_package ¤

    (bool, default: False ) –

    Whether to search for stubs-only package. If both the package and its stubs are found, they'll be merged together. If only the stubs are found, they'll be used as the package itself.

  • resolve_aliases ¤

    (bool, default: False ) –

    Whether to resolve aliases.

  • resolve_external ¤

    (bool | None, default: None ) –

    Whether to try to load unspecified modules to resolve aliases. Default value (None) means to load external modules only if they are the private sibling or the origin module (for example when ast imports from _ast).

  • resolve_implicit ¤

    (bool, default: False ) –

    When false, only try to resolve an alias if it is explicitly exported.

Returns:

Source code in src/griffe/_internal/loader.py
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
def load(
    objspec: str | Path | None = None,
    /,
    *,
    submodules: bool = True,
    try_relative_path: bool = True,
    extensions: Extensions | None = None,
    search_paths: Sequence[str | Path] | None = None,
    docstring_parser: DocstringStyle | Parser | None = None,
    docstring_options: DocstringOptions | None = None,
    lines_collection: LinesCollection | None = None,
    modules_collection: ModulesCollection | None = None,
    allow_inspection: bool = True,
    force_inspection: bool = False,
    store_source: bool = True,
    find_stubs_package: bool = False,
    resolve_aliases: bool = False,
    resolve_external: bool | None = None,
    resolve_implicit: bool = False,
) -> Object | Alias:
    """Load and return a Griffe object.

    In Griffe's context, loading means:

    - searching for a package, and finding it on the file system or as a builtin module
        (see the [`ModuleFinder`][griffe.ModuleFinder] class for more information)
    - extracting information from each of its (sub)modules, by either parsing
        the source code (see the [`visit`][griffe.visit] function)
        or inspecting the module at runtime (see the [`inspect`][griffe.inspect] function)

    The extracted information is stored in a collection of modules, which can be queried later.
    Each collected module is a tree of objects, representing the structure of the module.
    See the [`Module`][griffe.Module], [`Class`][griffe.Class],
    [`Function`][griffe.Function], [`Attribute`][griffe.Attribute], and
    [`TypeAlias`][griffe.TypeAlias] classes for more information.

    The main class used to load modules is [`GriffeLoader`][griffe.GriffeLoader].
    Convenience functions like this one and [`load_git`][griffe.load_git] are also available.

    Example:
        ```python
        import griffe

        module = griffe.load(...)
        ```

        This is a shortcut for:

        ```python
        from griffe import GriffeLoader

        loader = GriffeLoader(...)
        module = loader.load(...)
        ```

        See the documentation for the loader: [`GriffeLoader`][griffe.GriffeLoader].

    Parameters:
        objspec: The Python path of an object, or file path to a module.
        submodules: Whether to recurse on the submodules.
            This parameter only makes sense when loading a package (top-level module).
        try_relative_path: Whether to try finding the module as a relative path.
        extensions: The extensions to use.
        search_paths: The paths to search into.
        docstring_parser: The docstring parser to use. By default, no parsing is done.
        docstring_options: Docstring parsing options.
        lines_collection: A collection of source code lines.
        modules_collection: A collection of modules.
        allow_inspection: Whether to allow inspecting modules when visiting them is not possible.
        force_inspection: Whether to force using dynamic analysis when loading data.
        store_source: Whether to store code source in the lines collection.
        find_stubs_package: Whether to search for stubs-only package.
            If both the package and its stubs are found, they'll be merged together.
            If only the stubs are found, they'll be used as the package itself.
        resolve_aliases: Whether to resolve aliases.
        resolve_external: Whether to try to load unspecified modules to resolve aliases.
            Default value (`None`) means to load external modules only if they are the private sibling
            or the origin module (for example when `ast` imports from `_ast`).
        resolve_implicit: When false, only try to resolve an alias if it is explicitly exported.

    Returns:
        A Griffe object.
    """
    loader = GriffeLoader(
        extensions=extensions,
        search_paths=search_paths,
        docstring_parser=docstring_parser,
        docstring_options=docstring_options,
        lines_collection=lines_collection,
        modules_collection=modules_collection,
        allow_inspection=allow_inspection,
        force_inspection=force_inspection,
        store_source=store_source,
    )
    result = loader.load(
        objspec,
        submodules=submodules,
        try_relative_path=try_relative_path,
        find_stubs_package=find_stubs_package,
    )
    if resolve_aliases:
        loader.resolve_aliases(implicit=resolve_implicit, external=resolve_external)
    return result

load_git ¤

load_git(
    objspec: str | Path | None = None,
    /,
    *,
    ref: str = "HEAD",
    repo: str | Path = ".",
    submodules: bool = True,
    extensions: Extensions | None = None,
    search_paths: Sequence[str | Path] | None = None,
    docstring_parser: DocstringStyle | Parser | None = None,
    docstring_options: DocstringOptions | None = None,
    lines_collection: LinesCollection | None = None,
    modules_collection: ModulesCollection | None = None,
    allow_inspection: bool = True,
    force_inspection: bool = False,
    find_stubs_package: bool = False,
    resolve_aliases: bool = False,
    resolve_external: bool | None = None,
    resolve_implicit: bool = False,
) -> Object | Alias

Load and return a module from a specific Git reference.

This function will create a temporary git worktree at the requested reference before loading module with griffe.load.

This function requires that the git executable is installed.

Examples:

from griffe import load_git

old_api = load_git("my_module", ref="v0.1.0", repo="path/to/repo")

Parameters:

  • objspec ¤

    (str | Path | None, default: None ) –

    The Python path of an object, or file path to a module.

  • ref ¤

    (str, default: 'HEAD' ) –

    A Git reference such as a commit, tag or branch.

  • repo ¤

    (str | Path, default: '.' ) –

    Path to the repository (i.e. the directory containing the .git directory)

  • submodules ¤

    (bool, default: True ) –

    Whether to recurse on the submodules. This parameter only makes sense when loading a package (top-level module).

  • extensions ¤

    (Extensions | None, default: None ) –

    The extensions to use.

  • search_paths ¤

    (Sequence[str | Path] | None, default: None ) –

    The paths to search into (relative to the repository root).

  • docstring_parser ¤

    (DocstringStyle | Parser | None, default: None ) –

    The docstring parser to use. By default, no parsing is done.

  • docstring_options ¤

    (DocstringOptions | None, default: None ) –

    Docstring parsing options.

  • lines_collection ¤

    (LinesCollection | None, default: None ) –

    A collection of source code lines.

  • modules_collection ¤

    (ModulesCollection | None, default: None ) –

    A collection of modules.

  • allow_inspection ¤

    (bool, default: True ) –

    Whether to allow inspecting modules when visiting them is not possible.

  • force_inspection ¤

    (bool, default: False ) –

    Whether to force using dynamic analysis when loading data.

  • find_stubs_package ¤

    (bool, default: False ) –

    Whether to search for stubs-only package. If both the package and its stubs are found, they'll be merged together. If only the stubs are found, they'll be used as the package itself.

  • resolve_aliases ¤

    (bool, default: False ) –

    Whether to resolve aliases.

  • resolve_external ¤

    (bool | None, default: None ) –

    Whether to try to load unspecified modules to resolve aliases. Default value (None) means to load external modules only if they are the private sibling or the origin module (for example when ast imports from _ast).

  • resolve_implicit ¤

    (bool, default: False ) –

    When false, only try to resolve an alias if it is explicitly exported.

Returns:

Source code in src/griffe/_internal/loader.py
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
def load_git(
    objspec: str | Path | None = None,
    /,
    *,
    ref: str = "HEAD",
    repo: str | Path = ".",
    submodules: bool = True,
    extensions: Extensions | None = None,
    search_paths: Sequence[str | Path] | None = None,
    docstring_parser: DocstringStyle | Parser | None = None,
    docstring_options: DocstringOptions | None = None,
    lines_collection: LinesCollection | None = None,
    modules_collection: ModulesCollection | None = None,
    allow_inspection: bool = True,
    force_inspection: bool = False,
    find_stubs_package: bool = False,
    resolve_aliases: bool = False,
    resolve_external: bool | None = None,
    resolve_implicit: bool = False,
) -> Object | Alias:
    """Load and return a module from a specific Git reference.

    This function will create a temporary
    [git worktree](https://git-scm.com/docs/git-worktree) at the requested reference
    before loading `module` with [`griffe.load`][griffe.load].

    This function requires that the `git` executable is installed.

    Examples:
        ```python
        from griffe import load_git

        old_api = load_git("my_module", ref="v0.1.0", repo="path/to/repo")
        ```

    Parameters:
        objspec: The Python path of an object, or file path to a module.
        ref: A Git reference such as a commit, tag or branch.
        repo: Path to the repository (i.e. the directory *containing* the `.git` directory)
        submodules: Whether to recurse on the submodules.
            This parameter only makes sense when loading a package (top-level module).
        extensions: The extensions to use.
        search_paths: The paths to search into (relative to the repository root).
        docstring_parser: The docstring parser to use. By default, no parsing is done.
        docstring_options: Docstring parsing options.
        lines_collection: A collection of source code lines.
        modules_collection: A collection of modules.
        allow_inspection: Whether to allow inspecting modules when visiting them is not possible.
        force_inspection: Whether to force using dynamic analysis when loading data.
        find_stubs_package: Whether to search for stubs-only package.
            If both the package and its stubs are found, they'll be merged together.
            If only the stubs are found, they'll be used as the package itself.
        resolve_aliases: Whether to resolve aliases.
        resolve_external: Whether to try to load unspecified modules to resolve aliases.
            Default value (`None`) means to load external modules only if they are the private sibling
            or the origin module (for example when `ast` imports from `_ast`).
        resolve_implicit: When false, only try to resolve an alias if it is explicitly exported.

    Returns:
        A Griffe object.
    """
    with _tmp_worktree(repo, ref) as worktree:
        search_paths = [worktree / path for path in search_paths or ["."]]
        if isinstance(objspec, Path):
            objspec = worktree / objspec

        return load(
            objspec,
            submodules=submodules,
            try_relative_path=False,
            extensions=extensions,
            search_paths=search_paths,
            docstring_parser=docstring_parser,
            docstring_options=docstring_options,
            lines_collection=lines_collection,
            modules_collection=modules_collection,
            allow_inspection=allow_inspection,
            force_inspection=force_inspection,
            find_stubs_package=find_stubs_package,
            resolve_aliases=resolve_aliases,
            resolve_external=resolve_external,
            resolve_implicit=resolve_implicit,
        )

load_pypi ¤

Load and return a module from a specific package version downloaded using pip.

Parameters:

  • package ¤

    (str) –

    The package import name.

  • distribution ¤

    (str) –

    The distribution name.

  • version_spec ¤

    (str) –

    The version specifier to use when installing with pip.

  • submodules ¤

    (bool, default: True ) –

    Whether to recurse on the submodules. This parameter only makes sense when loading a package (top-level module).

  • extensions ¤

    (Extensions | None, default: None ) –

    The extensions to use.

  • search_paths ¤

    (Sequence[str | Path] | None, default: None ) –

    The paths to search into (relative to the repository root).

  • docstring_parser ¤

    (DocstringStyle | Parser | None, default: None ) –

    The docstring parser to use. By default, no parsing is done.

  • docstring_options ¤

    (DocstringOptions | None, default: None ) –

    Docstring parsing options.

  • lines_collection ¤

    (LinesCollection | None, default: None ) –

    A collection of source code lines.

  • modules_collection ¤

    (ModulesCollection | None, default: None ) –

    A collection of modules.

  • allow_inspection ¤

    (bool, default: True ) –

    Whether to allow inspecting modules when visiting them is not possible.

  • force_inspection ¤

    (bool, default: False ) –

    Whether to force using dynamic analysis when loading data.

  • find_stubs_package ¤

    (bool, default: False ) –

    Whether to search for stubs-only package. If both the package and its stubs are found, they'll be merged together. If only the stubs are found, they'll be used as the package itself.

  • resolve_aliases ¤

    (bool, default: False ) –

    Whether to resolve aliases.

  • resolve_external ¤

    (bool | None, default: None ) –

    Whether to try to load unspecified modules to resolve aliases. Default value (None) means to load external modules only if they are the private sibling or the origin module (for example when ast imports from _ast).

  • resolve_implicit ¤

    (bool, default: False ) –

    When false, only try to resolve an alias if it is explicitly exported.

Source code in src/griffe/_internal/loader.py
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
def load_pypi(
    package: str,
    distribution: str,
    version_spec: str,
    *,
    submodules: bool = True,
    extensions: Extensions | None = None,
    search_paths: Sequence[str | Path] | None = None,
    docstring_parser: DocstringStyle | Parser | None = None,
    docstring_options: DocstringOptions | None = None,
    lines_collection: LinesCollection | None = None,
    modules_collection: ModulesCollection | None = None,
    allow_inspection: bool = True,
    force_inspection: bool = False,
    find_stubs_package: bool = False,
    resolve_aliases: bool = False,
    resolve_external: bool | None = None,
    resolve_implicit: bool = False,
) -> Object | Alias:
    """Load and return a module from a specific package version downloaded using pip.

    Parameters:
        package: The package import name.
        distribution: The distribution name.
        version_spec: The version specifier to use when installing with pip.
        submodules: Whether to recurse on the submodules.
            This parameter only makes sense when loading a package (top-level module).
        extensions: The extensions to use.
        search_paths: The paths to search into (relative to the repository root).
        docstring_parser: The docstring parser to use. By default, no parsing is done.
        docstring_options: Docstring parsing options.
        lines_collection: A collection of source code lines.
        modules_collection: A collection of modules.
        allow_inspection: Whether to allow inspecting modules when visiting them is not possible.
        force_inspection: Whether to force using dynamic analysis when loading data.
        find_stubs_package: Whether to search for stubs-only package.
            If both the package and its stubs are found, they'll be merged together.
            If only the stubs are found, they'll be used as the package itself.
        resolve_aliases: Whether to resolve aliases.
        resolve_external: Whether to try to load unspecified modules to resolve aliases.
            Default value (`None`) means to load external modules only if they are the private sibling
            or the origin module (for example when `ast` imports from `_ast`).
        resolve_implicit: When false, only try to resolve an alias if it is explicitly exported.
    """
    if not all(find_spec(pkg) for pkg in ("pip", "wheel", "platformdirs")):
        raise RuntimeError("Please install Griffe with the 'pypi' extra to use this feature.")

    import platformdirs  # noqa: PLC0415

    pypi_cache_dir = Path(platformdirs.user_cache_dir("griffe"))
    install_dir = pypi_cache_dir / f"{distribution}{version_spec}"
    if install_dir.exists():
        logger.debug("Using cached %s%s", distribution, version_spec)
    else:
        with tempfile.TemporaryDirectory(dir=pypi_cache_dir) as tmpdir:
            install_dir = Path(tmpdir) / distribution
            logger.debug("Downloading %s%s", distribution, version_spec)
            process = subprocess.run(  # noqa: S603
                [
                    sys.executable,
                    "-mpip",
                    "install",
                    "--no-deps",
                    "--no-compile",
                    "--no-warn-script-location",
                    "--no-input",
                    "--disable-pip-version-check",
                    "--no-python-version-warning",
                    "-t",
                    str(install_dir),
                    f"{distribution}{version_spec}",
                ],
                text=True,
                stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT,
                check=False,
            )
            if process.returncode:
                logger.error(process.stdout)
                raise RuntimeError(f"Could not pip install {distribution}{version_spec}")
            logger.debug(process.stdout)
            shutil.rmtree(install_dir / "bin", ignore_errors=True)
            re_dist = re.sub("[._-]", "[._-]", distribution)
            version = next(
                match.group(1)
                for file in install_dir.iterdir()
                if (match := re.match(rf"{re_dist}-(.+)\.dist-info", file.name, re.IGNORECASE))
            )
            dest_dir = pypi_cache_dir / f"{distribution}=={version}"
            if not dest_dir.exists():
                install_dir.rename(dest_dir)
            install_dir = dest_dir

    if not package:
        files = sorted((file.name.lower() for file in install_dir.iterdir()), reverse=True)
        name = distribution.lower().replace("-", "_")
        if name in files or f"{name}.py" in files:
            package = name
        elif len(files) == 1:
            raise RuntimeError(f"No package found in {distribution}=={version}")
        else:
            try:
                package = next(file.split(".", 1)[0] for file in files if not file.endswith(".dist-info"))
            except StopIteration:
                raise RuntimeError(f"Could not guess package name for {distribution}=={version} (files; {files})")  # noqa: B904

    return load(
        package,
        submodules=submodules,
        try_relative_path=False,
        extensions=extensions,
        search_paths=[install_dir, *(search_paths or ())],
        docstring_parser=docstring_parser,
        docstring_options=docstring_options,
        lines_collection=lines_collection,
        modules_collection=modules_collection,
        allow_inspection=allow_inspection,
        force_inspection=force_inspection,
        find_stubs_package=find_stubs_package,
        resolve_aliases=resolve_aliases,
        resolve_external=resolve_external,
        resolve_implicit=resolve_implicit,
    )

Advanced API¤

GriffeLoader ¤

GriffeLoader(
    *,
    extensions: Extensions | None = None,
    search_paths: Sequence[str | Path] | None = None,
    docstring_parser: DocstringStyle | Parser | None = None,
    docstring_options: DocstringOptions | None = None,
    lines_collection: LinesCollection | None = None,
    modules_collection: ModulesCollection | None = None,
    allow_inspection: bool = True,
    force_inspection: bool = False,
    store_source: bool = True,
)

The Griffe loader, allowing to load data from modules.

Parameters:

  • extensions ¤

    (Extensions | None, default: None ) –

    The extensions to use.

  • search_paths ¤

    (Sequence[str | Path] | None, default: None ) –

    The paths to search into.

  • docstring_parser ¤

    (DocstringStyle | Parser | None, default: None ) –

    The docstring parser to use. By default, no parsing is done.

  • docstring_options ¤

    (DocstringOptions | None, default: None ) –

    Docstring parsing options.

  • lines_collection ¤

    (LinesCollection | None, default: None ) –

    A collection of source code lines.

  • modules_collection ¤

    (ModulesCollection | None, default: None ) –

    A collection of modules.

  • allow_inspection ¤

    (bool, default: True ) –

    Whether to allow inspecting modules when visiting them is not possible.

  • store_source ¤

    (bool, default: True ) –

    Whether to store code source in the lines collection.

Methods:

  • expand_exports

    Expand exports: try to recursively expand all module exports (__all__ values).

  • expand_wildcards

    Expand wildcards: try to recursively expand all found wildcards.

  • load

    Load an object as a Griffe object, given its Python or file path.

  • resolve_aliases

    Resolve aliases.

  • resolve_module_aliases

    Follow aliases: try to recursively resolve all found aliases.

  • stats

    Compute some statistics.

Attributes:

Source code in src/griffe/_internal/loader.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def __init__(
    self,
    *,
    extensions: Extensions | None = None,
    search_paths: Sequence[str | Path] | None = None,
    docstring_parser: DocstringStyle | Parser | None = None,
    docstring_options: DocstringOptions | None = None,
    lines_collection: LinesCollection | None = None,
    modules_collection: ModulesCollection | None = None,
    allow_inspection: bool = True,
    force_inspection: bool = False,
    store_source: bool = True,
) -> None:
    """Initialize the loader.

    Parameters:
        extensions: The extensions to use.
        search_paths: The paths to search into.
        docstring_parser: The docstring parser to use. By default, no parsing is done.
        docstring_options: Docstring parsing options.
        lines_collection: A collection of source code lines.
        modules_collection: A collection of modules.
        allow_inspection: Whether to allow inspecting modules when visiting them is not possible.
        store_source: Whether to store code source in the lines collection.
    """
    self.extensions: Extensions = extensions or load_extensions()
    """Loaded Griffe extensions."""
    self.docstring_parser: DocstringStyle | Parser | None = docstring_parser
    """Selected docstring parser."""
    self.docstring_options: DocstringOptions = docstring_options or {}
    """Configured parsing options."""
    self.lines_collection: LinesCollection = lines_collection or LinesCollection()
    """Collection of source code lines."""
    self.modules_collection: ModulesCollection = modules_collection or ModulesCollection()
    """Collection of modules."""
    self.allow_inspection: bool = allow_inspection
    """Whether to allow inspecting (importing) modules for which we can't find sources."""
    self.force_inspection: bool = force_inspection
    """Whether to force inspecting (importing) modules, even when sources were found."""
    self.store_source: bool = store_source
    """Whether to store source code in the lines collection."""
    self._search_paths: Sequence[str | Path] | None = search_paths
    self._time_stats: dict = {
        "time_spent_visiting": 0,
        "time_spent_inspecting": 0,
    }

allow_inspection instance-attribute ¤

allow_inspection: bool = allow_inspection

Whether to allow inspecting (importing) modules for which we can't find sources.

docstring_options instance-attribute ¤

docstring_options: DocstringOptions = (
    docstring_options or {}
)

Configured parsing options.

docstring_parser instance-attribute ¤

docstring_parser: DocstringStyle | Parser | None = (
    docstring_parser
)

Selected docstring parser.

extensions instance-attribute ¤

Loaded Griffe extensions.

finder cached property ¤

finder: ModuleFinder

The module source finder.

force_inspection instance-attribute ¤

force_inspection: bool = force_inspection

Whether to force inspecting (importing) modules, even when sources were found.

ignored_modules class-attribute ¤

ignored_modules: set[str] = {'debugpy', '_pydev'}

Special modules to ignore when loading.

For example, debugpy and _pydev are used when debugging with VSCode and should generally never be loaded.

lines_collection instance-attribute ¤

lines_collection: LinesCollection = (
    lines_collection or LinesCollection()
)

Collection of source code lines.

modules_collection instance-attribute ¤

Collection of modules.

store_source instance-attribute ¤

store_source: bool = store_source

Whether to store source code in the lines collection.

expand_exports ¤

expand_exports(
    module: Module, seen: set | None = None
) -> None

Expand exports: try to recursively expand all module exports (__all__ values).

See also: Module.exports.

Parameters:

  • module ¤

    (Module) –

    The module to recurse on.

  • seen ¤

    (set | None, default: None ) –

    Used to avoid infinite recursion.

Source code in src/griffe/_internal/loader.py
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
def expand_exports(self, module: Module, seen: set | None = None) -> None:
    """Expand exports: try to recursively expand all module exports (`__all__` values).

    See also: [`Module.exports`][griffe.Module.exports].

    Parameters:
        module: The module to recurse on.
        seen: Used to avoid infinite recursion.
    """
    seen = seen or set()
    seen.add(module.path)
    if module.exports is None:
        return

    expanded = []
    for export in module.exports:
        # It's a name: we resolve it, get the module it comes from,
        # recurse into it, and add its exports to the current ones.
        if isinstance(export, ExprName):
            module_path = export.canonical_path.rsplit(".", 1)[0]  # Remove trailing `.__all__`.
            try:
                next_module = self.modules_collection.get_member(module_path)
            except KeyError:
                logger.debug("Cannot expand '%s', try pre-loading corresponding package", export.canonical_path)
                continue
            if next_module.path not in seen:
                self.expand_exports(next_module, seen)
            try:
                expanded += [export for export in next_module.exports if export not in expanded]
            except TypeError:
                logger.warning("Unsupported item in %s.__all__: %s (use strings only)", module.path, export)
        # It's a string, simply add it to the current exports.
        else:
            expanded.append(export)
    module.exports = expanded

    # Make sure to expand exports in all modules.
    for submodule in module.modules.values():
        if not submodule.is_alias and submodule.path not in seen:
            self.expand_exports(submodule, seen)

expand_wildcards ¤

expand_wildcards(
    obj: Object,
    *,
    external: bool | None = None,
    seen: set | None = None,
) -> None

Expand wildcards: try to recursively expand all found wildcards.

See also: Alias.wildcard.

Parameters:

  • obj ¤

    (Object) –

    The object and its members to recurse on.

  • external ¤

    (bool | None, default: None ) –

    When true, try to load unspecified modules to expand wildcards.

  • seen ¤

    (set | None, default: None ) –

    Used to avoid infinite recursion.

Source code in src/griffe/_internal/loader.py
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
def expand_wildcards(
    self,
    obj: Object,
    *,
    external: bool | None = None,
    seen: set | None = None,
) -> None:
    """Expand wildcards: try to recursively expand all found wildcards.

    See also: [`Alias.wildcard`][griffe.Alias.wildcard].

    Parameters:
        obj: The object and its members to recurse on.
        external: When true, try to load unspecified modules to expand wildcards.
        seen: Used to avoid infinite recursion.
    """
    expanded = []
    to_remove = []
    seen = seen or set()
    seen.add(obj.path)

    # First we expand wildcard imports and store the objects in a temporary `expanded` variable,
    # while also keeping track of the members representing wildcard import, to remove them later.
    for member in obj.members.values():
        # Handle a wildcard.
        if member.is_alias and member.wildcard:  # type: ignore[union-attr]
            package = member.wildcard.split(".", 1)[0]  # type: ignore[union-attr]
            not_loaded = obj.package.path != package and package not in self.modules_collection

            # Try loading the (unknown) package containing the wildcard importe module (if allowed to).
            if not_loaded:
                if external is False or (external is None and package != f"_{obj.package.name}"):
                    continue
                try:
                    self.load(package, try_relative_path=False)
                except (ImportError, LoadingError) as error:
                    logger.debug("Could not expand wildcard import %s in %s: %s", member.name, obj.path, error)
                    continue

            # Try getting the module from which every public object is imported.
            try:
                target = self.modules_collection.get_member(member.target_path)  # type: ignore[union-attr]
            except KeyError:
                logger.debug(
                    "Could not expand wildcard import %s in %s: %s not found in modules collection",
                    member.name,
                    obj.path,
                    cast("Alias", member).target_path,
                )
                continue

            # Recurse into this module, expanding wildcards there before collecting everything.
            if target.path not in seen:
                try:
                    self.expand_wildcards(target, external=external, seen=seen)
                except (AliasResolutionError, CyclicAliasError) as error:
                    logger.debug("Could not expand wildcard import %s in %s: %s", member.name, obj.path, error)
                    continue

            # Collect every imported object.
            expanded.extend(self._expand_wildcard(member))  # type: ignore[arg-type]
            to_remove.append(member.name)

        # Recurse in unseen submodules.
        elif not member.is_alias and member.is_module and member.path not in seen:
            self.expand_wildcards(member, external=external, seen=seen)  # type: ignore[arg-type]

    # Then we remove the members representing wildcard imports.
    for name in to_remove:
        obj.del_member(name)

    # Finally we process the collected objects.
    for new_member, alias_lineno, alias_endlineno in expanded:
        overwrite = False
        already_present = new_member.name in obj.members
        self_alias = (
            new_member.is_alias and cast("Alias", new_member).target_path == f"{obj.path}.{new_member.name}"
        )

        # If a member with the same name is already present in the current object,
        # we only overwrite it if the alias is imported lower in the module
        # (meaning that the alias takes precedence at runtime).
        if already_present:
            old_member = obj.get_member(new_member.name)
            old_lineno = old_member.alias_lineno if old_member.is_alias else old_member.lineno
            overwrite = alias_lineno > (old_lineno or 0)  # type: ignore[operator]

        # 1. If the expanded member is an alias with a target path equal to its own path, we stop.
        #    This situation can arise because of Griffe's mishandling of (abusive) wildcard imports.
        #    We have yet to check how Python handles this itself, or if there's an algorithm
        #    that we could follow to untangle back-and-forth wildcard imports.
        # 2. If the expanded member was already present and we decided not to overwrite it, we stop.
        # 3. Otherwise we proceed further.
        if not self_alias and (not already_present or overwrite):
            alias = Alias(
                new_member.name,
                new_member,
                lineno=alias_lineno,
                endlineno=alias_endlineno,
                parent=obj,  # type: ignore[arg-type]
                wildcard_imported=True,
            )
            # Special case: we avoid overwriting a submodule with an alias.
            # Griffe suffers from this limitation where an object cannot store both
            # a submodule and a member of the same name, while this poses (almost) no issue in Python.
            # We always give precedence to the submodule.
            # See the "avoid member-submodule name shadowing" section in the "Python code" docs page.
            if already_present:
                prev_member = obj.get_member(new_member.name)
                with suppress(AliasResolutionError, CyclicAliasError):
                    if prev_member.is_module:
                        continue

            # Everything went right (supposedly), we add the alias as a member of the current object.
            obj.set_member(new_member.name, alias)
            # YORE: Bump 2: Remove line.
            self.extensions.call("on_wildcard_expansion", alias=alias, loader=self)

load ¤

load(
    objspec: str | Path | None = None,
    /,
    *,
    submodules: bool = True,
    try_relative_path: bool = True,
    find_stubs_package: bool = False,
) -> Object | Alias

Load an object as a Griffe object, given its Python or file path.

Note that this will load the whole object's package, and return only the specified object. The rest of the package can be accessed from the returned object with regular methods and properties (parent, members, etc.).

Examples:

>>> loader.load("griffe.Module")
Alias("Module", "griffe._internal.models.Module")

Parameters:

  • objspec ¤

    (str | Path | None, default: None ) –

    The Python path of an object, or file path to a module.

  • submodules ¤

    (bool, default: True ) –

    Whether to recurse on the submodules. This parameter only makes sense when loading a package (top-level module).

  • try_relative_path ¤

    (bool, default: True ) –

    Whether to try finding the module as a relative path.

  • find_stubs_package ¤

    (bool, default: False ) –

    Whether to search for stubs-only package. If both the package and its stubs are found, they'll be merged together. If only the stubs are found, they'll be used as the package itself.

Raises:

Returns:

Source code in src/griffe/_internal/loader.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
def load(
    self,
    objspec: str | Path | None = None,
    /,
    *,
    submodules: bool = True,
    try_relative_path: bool = True,
    find_stubs_package: bool = False,
) -> Object | Alias:
    """Load an object as a Griffe object, given its Python or file path.

    Note that this will load the whole object's package,
    and return only the specified object.
    The rest of the package can be accessed from the returned object
    with regular methods and properties (`parent`, `members`, etc.).

    Examples:
        >>> loader.load("griffe.Module")
        Alias("Module", "griffe._internal.models.Module")

    Parameters:
        objspec: The Python path of an object, or file path to a module.
        submodules: Whether to recurse on the submodules.
            This parameter only makes sense when loading a package (top-level module).
        try_relative_path: Whether to try finding the module as a relative path.
        find_stubs_package: Whether to search for stubs-only package.
            If both the package and its stubs are found, they'll be merged together.
            If only the stubs are found, they'll be used as the package itself.

    Raises:
        LoadingError: When loading a module failed for various reasons.
        ModuleNotFoundError: When a module was not found and inspection is disallowed.

    Returns:
        A Griffe object.
    """
    obj_path: str
    package = None
    top_module = None

    # We always start by searching paths on the disk,
    # even if inspection is forced.
    logger.debug("Searching path(s) for %s", objspec)
    try:
        obj_path, package = self.finder.find_spec(
            objspec,  # type: ignore[arg-type]
            try_relative_path=try_relative_path,
            find_stubs_package=find_stubs_package,
        )
    except ModuleNotFoundError:
        # If we couldn't find paths on disk and inspection is disabled,
        # re-raise ModuleNotFoundError.
        logger.debug("Could not find path for %s on disk", objspec)
        if not (self.allow_inspection or self.force_inspection):
            raise

        # Otherwise we try to dynamically import the top-level module.
        obj_path = str(objspec)
        top_module_name = obj_path.split(".", 1)[0]
        logger.debug("Trying to dynamically import %s", top_module_name)
        top_module_object = dynamic_import(top_module_name, self.finder.search_paths)

        try:
            top_module_path = top_module_object.__path__
            if not top_module_path:
                raise ValueError(f"Module {top_module_name} has no paths set")  # noqa: TRY301
        except (AttributeError, ValueError):
            # If the top-level module has no `__path__`, we inspect it as-is,
            # and do not try to recurse into submodules (there shouldn't be any in builtin/compiled modules).
            logger.debug("Module %s has no paths set (built-in module?). Inspecting it as-is.", top_module_name)
            top_module = self._inspect_module(top_module_name)
            self.modules_collection.set_member(top_module.path, top_module)
            return self._post_load(top_module, obj_path)

        # We found paths, and use them to build our intermediate Package or NamespacePackage struct.
        logger.debug("Module %s has paths set: %s", top_module_name, top_module_path)
        top_module_path = [Path(path) for path in top_module_path]
        if len(top_module_path) > 1:
            package = NamespacePackage(top_module_name, top_module_path)
        else:
            package = Package(top_module_name, top_module_path[0])

    # We have an intermediate package, and an object path: we're ready to load.
    logger.debug("Found %s: loading", objspec)
    try:
        top_module = self._load_package(package, submodules=submodules)
    except LoadingError:
        logger.exception("Could not load package %s", package)
        raise

    return self._post_load(top_module, obj_path)

resolve_aliases ¤

resolve_aliases(
    *,
    implicit: bool = False,
    external: bool | None = None,
    max_iterations: int | None = None,
) -> tuple[set[str], int]

Resolve aliases.

Parameters:

  • implicit ¤

    (bool, default: False ) –

    When false, only try to resolve an alias if it is explicitly exported.

  • external ¤

    (bool | None, default: None ) –

    When false, don't try to load unspecified modules to resolve aliases.

  • max_iterations ¤

    (int | None, default: None ) –

    Maximum number of iterations on the loader modules collection.

Returns:

  • tuple[set[str], int]

    The unresolved aliases and the number of iterations done.

Source code in src/griffe/_internal/loader.py
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
def resolve_aliases(
    self,
    *,
    implicit: bool = False,
    external: bool | None = None,
    max_iterations: int | None = None,
) -> tuple[set[str], int]:
    """Resolve aliases.

    Parameters:
        implicit: When false, only try to resolve an alias if it is explicitly exported.
        external: When false, don't try to load unspecified modules to resolve aliases.
        max_iterations: Maximum number of iterations on the loader modules collection.

    Returns:
        The unresolved aliases and the number of iterations done.
    """
    if max_iterations is None:
        max_iterations = float("inf")  # type: ignore[assignment]
    prev_unresolved: set[str] = set()
    unresolved: set[str] = set("0")  # Init to enter loop.
    iteration = 0
    collection = self.modules_collection.members

    # Before resolving aliases, we try to expand wildcard imports again
    # (this was already done in `_post_load()`),
    # this time with the user-configured `external` setting,
    # and with potentially more packages loaded in the collection,
    # allowing to resolve more aliases.
    for wildcards_module in list(collection.values()):
        self.expand_wildcards(wildcards_module, external=external)

    load_failures: set[str] = set()
    while unresolved and unresolved != prev_unresolved and iteration < max_iterations:  # type: ignore[operator]
        prev_unresolved = unresolved - {"0"}
        unresolved = set()
        resolved: set[str] = set()
        iteration += 1
        for module_name in list(collection.keys()):
            module = collection[module_name]
            next_resolved, next_unresolved = self.resolve_module_aliases(
                module,
                implicit=implicit,
                external=external,
                load_failures=load_failures,
            )
            resolved |= next_resolved
            unresolved |= next_unresolved
        logger.debug(
            "Iteration %s finished, %s aliases resolved, still %s to go",
            iteration,
            len(resolved),
            len(unresolved),
        )
    return unresolved, iteration

resolve_module_aliases ¤

resolve_module_aliases(
    obj: Object | Alias,
    *,
    implicit: bool = False,
    external: bool | None = None,
    seen: set[str] | None = None,
    load_failures: set[str] | None = None,
) -> tuple[set[str], set[str]]

Follow aliases: try to recursively resolve all found aliases.

Parameters:

  • obj ¤

    (Object | Alias) –

    The object and its members to recurse on.

  • implicit ¤

    (bool, default: False ) –

    When false, only try to resolve an alias if it is explicitly exported.

  • external ¤

    (bool | None, default: None ) –

    When false, don't try to load unspecified modules to resolve aliases.

  • seen ¤

    (set[str] | None, default: None ) –

    Used to avoid infinite recursion.

  • load_failures ¤

    (set[str] | None, default: None ) –

    Set of external packages we failed to load (to prevent retries).

Returns:

Source code in src/griffe/_internal/loader.py
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
def resolve_module_aliases(
    self,
    obj: Object | Alias,
    *,
    implicit: bool = False,
    external: bool | None = None,
    seen: set[str] | None = None,
    load_failures: set[str] | None = None,
) -> tuple[set[str], set[str]]:
    """Follow aliases: try to recursively resolve all found aliases.

    Parameters:
        obj: The object and its members to recurse on.
        implicit: When false, only try to resolve an alias if it is explicitly exported.
        external: When false, don't try to load unspecified modules to resolve aliases.
        seen: Used to avoid infinite recursion.
        load_failures: Set of external packages we failed to load (to prevent retries).

    Returns:
        Both sets of resolved and unresolved aliases.
    """
    resolved = set()
    unresolved = set()
    if load_failures is None:
        load_failures = set()
    seen = seen or set()
    seen.add(obj.path)

    for member in obj.members.values():
        # Handle aliases.
        if member.is_alias:
            if member.wildcard or member.resolved:  # type: ignore[union-attr]
                continue
            if not implicit and not member.is_exported:
                continue

            # Try resolving the alias. If it fails, check if it is because it comes
            # from an external package, and decide if we should load that package
            # to allow the alias to be resolved at the next iteration (maybe).
            try:
                member.resolve_target()  # type: ignore[union-attr]
            except AliasResolutionError as error:
                target = error.alias.target_path
                unresolved.add(member.path)
                package = target.split(".", 1)[0]
                load_module = (
                    (external is True or (external is None and package == f"_{obj.package.name}"))
                    and package not in load_failures
                    and obj.package.path != package
                    and package not in self.modules_collection
                )
                if load_module:
                    logger.debug("Failed to resolve alias %s -> %s", member.path, target)
                    try:
                        self.load(package, try_relative_path=False)
                    except (ImportError, LoadingError) as error:
                        logger.debug("Could not follow alias %s: %s", member.path, error)
                        load_failures.add(package)
            except CyclicAliasError as error:
                logger.debug(str(error))
            else:
                logger.debug("Alias %s was resolved to %s", member.path, member.final_target.path)  # type: ignore[union-attr]
                resolved.add(member.path)

        # Recurse into unseen modules and classes.
        elif member.kind in {Kind.MODULE, Kind.CLASS} and member.path not in seen:
            sub_resolved, sub_unresolved = self.resolve_module_aliases(
                member,
                implicit=implicit,
                external=external,
                seen=seen,
                load_failures=load_failures,
            )
            resolved |= sub_resolved
            unresolved |= sub_unresolved

    return resolved, unresolved

stats ¤

stats() -> Stats

Compute some statistics.

Returns:

  • Stats

    Some statistics.

Source code in src/griffe/_internal/loader.py
530
531
532
533
534
535
536
537
538
539
def stats(self) -> Stats:
    """Compute some statistics.

    Returns:
        Some statistics.
    """
    stats = Stats(self)
    stats.time_spent_visiting = self._time_stats["time_spent_visiting"]
    stats.time_spent_inspecting = self._time_stats["time_spent_inspecting"]
    return stats

ModulesCollection ¤

ModulesCollection()

Bases: GetMembersMixin, SetMembersMixin, DelMembersMixin


              flowchart TD
              griffe.ModulesCollection[ModulesCollection]
              griffe._internal.mixins.GetMembersMixin[GetMembersMixin]
              griffe._internal.mixins.SetMembersMixin[SetMembersMixin]
              griffe._internal.mixins.DelMembersMixin[DelMembersMixin]

                              griffe._internal.mixins.GetMembersMixin --> griffe.ModulesCollection
                
                griffe._internal.mixins.SetMembersMixin --> griffe.ModulesCollection
                
                griffe._internal.mixins.DelMembersMixin --> griffe.ModulesCollection
                


              click griffe.ModulesCollection href "" "griffe.ModulesCollection"
              click griffe._internal.mixins.GetMembersMixin href "" "griffe._internal.mixins.GetMembersMixin"
              click griffe._internal.mixins.SetMembersMixin href "" "griffe._internal.mixins.SetMembersMixin"
              click griffe._internal.mixins.DelMembersMixin href "" "griffe._internal.mixins.DelMembersMixin"
            

A collection of modules, allowing easy access to members.

Initialize the collection.

Methods:

  • __bool__

    A modules collection is always true-ish.

  • __contains__

    Check if a module is in the collection.

  • __delitem__

    Delete a member with its name or path.

  • __getitem__

    Get a member with its name or path.

  • __setitem__

    Set a member with its name or path.

  • del_member

    Delete a member with its name or path.

  • get_member

    Get a member with its name or path.

  • set_member

    Set a member with its name or path.

Attributes:

Source code in src/griffe/_internal/collections.py
71
72
73
74
def __init__(self) -> None:
    """Initialize the collection."""
    self.members: dict[str, Module] = {}
    """Members (modules) of the collection."""

all_members property ¤

all_members: dict[str, Module]

Members of the collection.

This property is overwritten to simply return self.members, as all_members does not make sense for a modules collection.

is_collection class-attribute instance-attribute ¤

is_collection = True

Marked as collection to distinguish from objects.

members instance-attribute ¤

members: dict[str, Module] = {}

Members (modules) of the collection.

__bool__ ¤

__bool__() -> bool

A modules collection is always true-ish.

Source code in src/griffe/_internal/collections.py
76
77
78
def __bool__(self) -> bool:
    """A modules collection is always true-ish."""
    return True

__contains__ ¤

__contains__(item: Any) -> bool

Check if a module is in the collection.

Source code in src/griffe/_internal/collections.py
80
81
82
def __contains__(self, item: Any) -> bool:
    """Check if a module is in the collection."""
    return item in self.members

__delitem__ ¤

__delitem__(key: str | Sequence[str]) -> None

Delete a member with its name or path.

This method is part of the consumer API: do not use when producing Griffe trees!

Members will be looked up in both declared members and inherited ones, triggering computation of the latter.

Parameters:

Examples:

>>> del griffe_object["foo"]
>>> del griffe_object["path.to.bar"]
>>> del griffe_object[("path", "to", "qux")]
Source code in src/griffe/_internal/mixins.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def __delitem__(self, key: str | Sequence[str]) -> None:
    """Delete a member with its name or path.

    This method is part of the consumer API:
    do not use when producing Griffe trees!

    Members will be looked up in both declared members and inherited ones,
    triggering computation of the latter.

    Parameters:
        key: The name or path of the member.

    Examples:
        >>> del griffe_object["foo"]
        >>> del griffe_object["path.to.bar"]
        >>> del griffe_object[("path", "to", "qux")]
    """
    parts = _get_parts(key)
    if len(parts) == 1:
        name = parts[0]
        try:
            del self.members[name]  # type: ignore[attr-defined]
        except KeyError:
            del self.inherited_members[name]  # type: ignore[attr-defined]
    else:
        del self.all_members[parts[0]][parts[1:]]  # type: ignore[attr-defined]

__getitem__ ¤

__getitem__(key: str | Sequence[str]) -> Any

Get a member with its name or path.

This method is part of the consumer API: do not use when producing Griffe trees!

Members will be looked up in both declared members and inherited ones, triggering computation of the latter.

Parameters:

Examples:

>>> foo = griffe_object["foo"]
>>> bar = griffe_object["path.to.bar"]
>>> qux = griffe_object[("path", "to", "qux")]
Source code in src/griffe/_internal/mixins.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def __getitem__(self, key: str | Sequence[str]) -> Any:
    """Get a member with its name or path.

    This method is part of the consumer API:
    do not use when producing Griffe trees!

    Members will be looked up in both declared members and inherited ones,
    triggering computation of the latter.

    Parameters:
        key: The name or path of the member.

    Examples:
        >>> foo = griffe_object["foo"]
        >>> bar = griffe_object["path.to.bar"]
        >>> qux = griffe_object[("path", "to", "qux")]
    """
    parts = _get_parts(key)
    if len(parts) == 1:
        return self.all_members[parts[0]]  # type: ignore[attr-defined]
    return self.all_members[parts[0]][parts[1:]]  # type: ignore[attr-defined]

__setitem__ ¤

__setitem__(
    key: str | Sequence[str], value: Object | Alias
) -> None

Set a member with its name or path.

This method is part of the consumer API: do not use when producing Griffe trees!

Parameters:

Examples:

>>> griffe_object["foo"] = foo
>>> griffe_object["path.to.bar"] = bar
>>> griffe_object[("path", "to", "qux")] = qux
Source code in src/griffe/_internal/mixins.py
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
def __setitem__(self, key: str | Sequence[str], value: Object | Alias) -> None:
    """Set a member with its name or path.

    This method is part of the consumer API:
    do not use when producing Griffe trees!

    Parameters:
        key: The name or path of the member.
        value: The member.

    Examples:
        >>> griffe_object["foo"] = foo
        >>> griffe_object["path.to.bar"] = bar
        >>> griffe_object[("path", "to", "qux")] = qux
    """
    parts = _get_parts(key)
    if len(parts) == 1:
        name = parts[0]
        self.members[name] = value  # type: ignore[attr-defined]
        if self.is_collection:  # type: ignore[attr-defined]
            value._modules_collection = self  # type: ignore[union-attr]
        else:
            value.parent = self  # type: ignore[assignment]
    else:
        self.members[parts[0]][parts[1:]] = value  # type: ignore[attr-defined]

del_member ¤

del_member(key: str | Sequence[str]) -> None

Delete a member with its name or path.

This method is part of the producer API: you can use it safely while building Griffe trees (for example in Griffe extensions).

Members will be looked up in declared members only, not inherited ones.

Parameters:

Examples:

>>> griffe_object.del_member("foo")
>>> griffe_object.del_member("path.to.bar")
>>> griffe_object.del_member(("path", "to", "qux"))
Source code in src/griffe/_internal/mixins.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def del_member(self, key: str | Sequence[str]) -> None:
    """Delete a member with its name or path.

    This method is part of the producer API:
    you can use it safely while building Griffe trees
    (for example in Griffe extensions).

    Members will be looked up in declared members only, not inherited ones.

    Parameters:
        key: The name or path of the member.

    Examples:
        >>> griffe_object.del_member("foo")
        >>> griffe_object.del_member("path.to.bar")
        >>> griffe_object.del_member(("path", "to", "qux"))
    """
    parts = _get_parts(key)
    if len(parts) == 1:
        name = parts[0]
        del self.members[name]  # type: ignore[attr-defined]
    else:
        self.members[parts[0]].del_member(parts[1:])  # type: ignore[attr-defined]

get_member ¤

get_member(key: str | Sequence[str]) -> Any

Get a member with its name or path.

This method is part of the producer API: you can use it safely while building Griffe trees (for example in Griffe extensions).

Members will be looked up in declared members only, not inherited ones.

Parameters:

Examples:

>>> foo = griffe_object["foo"]
>>> bar = griffe_object["path.to.bar"]
>>> bar = griffe_object[("path", "to", "bar")]
Source code in src/griffe/_internal/mixins.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def get_member(self, key: str | Sequence[str]) -> Any:
    """Get a member with its name or path.

    This method is part of the producer API:
    you can use it safely while building Griffe trees
    (for example in Griffe extensions).

    Members will be looked up in declared members only, not inherited ones.

    Parameters:
        key: The name or path of the member.

    Examples:
        >>> foo = griffe_object["foo"]
        >>> bar = griffe_object["path.to.bar"]
        >>> bar = griffe_object[("path", "to", "bar")]
    """
    parts = _get_parts(key)
    if len(parts) == 1:
        return self.members[parts[0]]  # type: ignore[attr-defined]
    return self.members[parts[0]].get_member(parts[1:])  # type: ignore[attr-defined]

set_member ¤

set_member(
    key: str | Sequence[str], value: Object | Alias
) -> None

Set a member with its name or path.

This method is part of the producer API: you can use it safely while building Griffe trees (for example in Griffe extensions).

Parameters:

Examples:

>>> griffe_object.set_member("foo", foo)
>>> griffe_object.set_member("path.to.bar", bar)
>>> griffe_object.set_member(("path", "to", "qux"), qux)
Source code in src/griffe/_internal/mixins.py
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
def set_member(self, key: str | Sequence[str], value: Object | Alias) -> None:
    """Set a member with its name or path.

    This method is part of the producer API:
    you can use it safely while building Griffe trees
    (for example in Griffe extensions).

    Parameters:
        key: The name or path of the member.
        value: The member.

    Examples:
        >>> griffe_object.set_member("foo", foo)
        >>> griffe_object.set_member("path.to.bar", bar)
        >>> griffe_object.set_member(("path", "to", "qux"), qux)
    """
    parts = _get_parts(key)
    if len(parts) == 1:
        name = parts[0]
        if name in self.members:  # type: ignore[attr-defined]
            member = self.members[name]  # type: ignore[attr-defined]
            if not member.is_alias:
                # When reassigning a module to an existing one,
                # try to merge them as one regular and one stubs module
                # (implicit support for .pyi modules).
                if member.is_module and not (member.is_namespace_package or member.is_namespace_subpackage):
                    # Accessing attributes of the value or member can trigger alias errors.
                    # Accessing file paths can trigger a builtin module error.
                    with suppress(AliasResolutionError, CyclicAliasError, BuiltinModuleError):
                        if value.is_module and value.filepath != member.filepath:
                            with suppress(ValueError):
                                value = merge_stubs(member, value)  # type: ignore[arg-type]
                for alias in member.aliases.values():
                    with suppress(CyclicAliasError):
                        alias.target = value
        self.members[name] = value  # type: ignore[attr-defined]
        if self.is_collection:  # type: ignore[attr-defined]
            value._modules_collection = self  # type: ignore[union-attr]
        else:
            value.parent = self  # type: ignore[assignment]
    else:
        self.members[parts[0]].set_member(parts[1:], value)  # type: ignore[attr-defined]

LinesCollection ¤

LinesCollection()

A simple dictionary containing the modules source code lines.

Initialize the collection.

Methods:

  • __bool__

    A lines collection is always true-ish.

  • __contains__

    Check if a file path is in the collection.

  • __getitem__

    Get the lines of a file path.

  • __setitem__

    Set the lines of a file path.

  • items

    Return the collection items.

  • keys

    Return the collection keys.

  • values

    Return the collection values.

Source code in src/griffe/_internal/collections.py
20
21
22
def __init__(self) -> None:
    """Initialize the collection."""
    self._data: dict[Path, list[str]] = {}

__bool__ ¤

__bool__() -> bool

A lines collection is always true-ish.

Source code in src/griffe/_internal/collections.py
36
37
38
def __bool__(self) -> bool:
    """A lines collection is always true-ish."""
    return True

__contains__ ¤

__contains__(item: Path) -> bool

Check if a file path is in the collection.

Source code in src/griffe/_internal/collections.py
32
33
34
def __contains__(self, item: Path) -> bool:
    """Check if a file path is in the collection."""
    return item in self._data

__getitem__ ¤

__getitem__(key: Path) -> list[str]

Get the lines of a file path.

Source code in src/griffe/_internal/collections.py
24
25
26
def __getitem__(self, key: Path) -> list[str]:
    """Get the lines of a file path."""
    return self._data[key]

__setitem__ ¤

__setitem__(key: Path, value: list[str]) -> None

Set the lines of a file path.

Source code in src/griffe/_internal/collections.py
28
29
30
def __setitem__(self, key: Path, value: list[str]) -> None:
    """Set the lines of a file path."""
    self._data[key] = value

items ¤

items() -> ItemsView

Return the collection items.

Returns:

Source code in src/griffe/_internal/collections.py
56
57
58
59
60
61
62
def items(self) -> ItemsView:
    """Return the collection items.

    Returns:
        The collection items.
    """
    return self._data.items()

keys ¤

keys() -> KeysView

Return the collection keys.

Returns:

Source code in src/griffe/_internal/collections.py
40
41
42
43
44
45
46
def keys(self) -> KeysView:
    """Return the collection keys.

    Returns:
        The collection keys.
    """
    return self._data.keys()

values ¤

values() -> ValuesView

Return the collection values.

Returns:

Source code in src/griffe/_internal/collections.py
48
49
50
51
52
53
54
def values(self) -> ValuesView:
    """Return the collection values.

    Returns:
        The collection values.
    """
    return self._data.values()

Additional API¤

Stats ¤

Load statistics for a Griffe loader.

Parameters:

Methods:

  • as_text

    Format the statistics as text.

Attributes:

Source code in src/griffe/_internal/stats.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def __init__(self, loader: GriffeLoader) -> None:
    """Initialiwe the stats object.

    Parameters:
        loader: The loader to compute stats for.
    """
    self.loader = loader
    """The loader to compute stats for."""

    modules_by_extension = defaultdict(
        int,
        {
            "": 0,
            ".py": 0,
            ".pyi": 0,
            ".pyc": 0,
            ".pyo": 0,
            ".pyd": 0,
            ".so": 0,
        },
    )

    top_modules = loader.modules_collection.members.values()

    self.by_kind = {
        Kind.MODULE: 0,
        Kind.CLASS: 0,
        Kind.FUNCTION: 0,
        Kind.ATTRIBUTE: 0,
        Kind.TYPE_ALIAS: 0,
    }
    """Number of objects by kind."""

    self.packages = len(top_modules)
    """Number of packages."""

    self.modules_by_extension = modules_by_extension
    """Number of modules by extension."""

    self.lines = sum(len(lines) for lines in loader.lines_collection.values())
    """Total number of lines."""

    self.time_spent_visiting = 0
    """Time spent visiting modules."""

    self.time_spent_inspecting = 0
    """Time spent inspecting modules."""

    self.time_spent_serializing = 0
    """Time spent serializing objects."""

    for module in top_modules:
        self._itercount(module)

by_kind instance-attribute ¤

by_kind = {
    MODULE: 0,
    CLASS: 0,
    FUNCTION: 0,
    ATTRIBUTE: 0,
    TYPE_ALIAS: 0,
}

Number of objects by kind.

lines instance-attribute ¤

lines = sum((len(lines)) for lines in (values()))

Total number of lines.

loader instance-attribute ¤

loader = loader

The loader to compute stats for.

modules_by_extension instance-attribute ¤

modules_by_extension = modules_by_extension

Number of modules by extension.

packages instance-attribute ¤

packages = len(top_modules)

Number of packages.

time_spent_inspecting instance-attribute ¤

time_spent_inspecting = 0

Time spent inspecting modules.

time_spent_serializing instance-attribute ¤

time_spent_serializing = 0

Time spent serializing objects.

time_spent_visiting instance-attribute ¤

time_spent_visiting = 0

Time spent visiting modules.

as_text ¤

as_text() -> str

Format the statistics as text.

Returns:

  • str

    Text stats.

Source code in src/griffe/_internal/stats.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
def as_text(self) -> str:
    """Format the statistics as text.

    Returns:
        Text stats.
    """
    lines = []
    packages = self.packages
    modules = self.by_kind[Kind.MODULE]
    classes = self.by_kind[Kind.CLASS]
    functions = self.by_kind[Kind.FUNCTION]
    attributes = self.by_kind[Kind.ATTRIBUTE]
    type_aliases = self.by_kind[Kind.TYPE_ALIAS]
    objects = sum((modules, classes, functions, attributes, type_aliases))
    lines.append("Statistics")
    lines.append("---------------------")
    lines.append("Number of loaded objects")
    lines.append(f"  Modules: {modules}")
    lines.append(f"  Classes: {classes}")
    lines.append(f"  Functions: {functions}")
    lines.append(f"  Attributes: {attributes}")
    lines.append(f"  Type aliases: {type_aliases}")
    lines.append(f"  Total: {objects} across {packages} packages")
    per_ext = self.modules_by_extension
    builtin = per_ext[""]
    regular = per_ext[".py"]
    stubs = per_ext[".pyi"]
    compiled = modules - builtin - regular - stubs
    lines.append("")
    lines.append(f"Total number of lines: {self.lines}")
    lines.append("")
    lines.append("Modules")
    lines.append(f"  Builtin: {builtin}")
    lines.append(f"  Compiled: {compiled}")
    lines.append(f"  Regular: {regular}")
    lines.append(f"  Stubs: {stubs}")
    lines.append("  Per extension:")
    for ext, number in sorted(per_ext.items()):
        if ext:
            lines.append(f"    {ext}: {number}")

    visit_time = self.time_spent_visiting / 1000
    inspect_time = self.time_spent_inspecting / 1000
    total_time = visit_time + inspect_time
    visit_percent = visit_time / total_time * 100
    inspect_percent = inspect_time / total_time * 100

    force_inspection = self.loader.force_inspection
    visited_modules = 0 if force_inspection else regular
    try:
        visit_time_per_module = visit_time / visited_modules
    except ZeroDivisionError:
        visit_time_per_module = 0

    inspected_modules = builtin + compiled + (regular if force_inspection else 0)
    try:
        inspect_time_per_module = inspect_time / inspected_modules
    except ZeroDivisionError:
        inspect_time_per_module = 0

    lines.append("")
    lines.append(
        f"Time spent visiting modules ({visited_modules}): "
        f"{visit_time}ms, {visit_time_per_module:.02f}ms/module ({visit_percent:.02f}%)",
    )
    lines.append(
        f"Time spent inspecting modules ({inspected_modules}): "
        f"{inspect_time}ms, {inspect_time_per_module:.02f}ms/module ({inspect_percent:.02f}%)",
    )

    serialize_time = self.time_spent_serializing / 1000
    serialize_time_per_module = serialize_time / modules
    lines.append(f"Time spent serializing: {serialize_time}ms, {serialize_time_per_module:.02f}ms/module")

    return "\n".join(lines)

merge_stubs ¤

merge_stubs(mod1: Module, mod2: Module) -> Module

Merge stubs into a module.

Parameters:

  • mod1 ¤

    (Module) –

    A regular module or stubs module.

  • mod2 ¤

    (Module) –

    A regular module or stubs module.

Raises:

  • ValueError

    When both modules are regular modules (no stubs is passed).

Returns:

  • Module

    The regular module.

Source code in src/griffe/_internal/merger.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def merge_stubs(mod1: Module, mod2: Module) -> Module:
    """Merge stubs into a module.

    Parameters:
        mod1: A regular module or stubs module.
        mod2: A regular module or stubs module.

    Raises:
        ValueError: When both modules are regular modules (no stubs is passed).

    Returns:
        The regular module.
    """
    logger.debug("Trying to merge %s and %s", mod1.filepath, mod2.filepath)
    if mod1.filepath.suffix == ".pyi":  # type: ignore[union-attr]
        stubs = mod1
        module = mod2
    elif mod2.filepath.suffix == ".pyi":  # type: ignore[union-attr]
        stubs = mod2
        module = mod1
    else:
        raise ValueError("cannot merge regular (non-stubs) modules together")
    _merge_module_stubs(module, stubs)
    return module