Finder¤
Advanced API¤
ModuleFinder ¤
ModuleFinder(
search_paths: Sequence[str | Path] | None = None,
)
The Griffe finder, allowing to find modules on the file system.
The module finder is generally not used directly. Each GriffeLoader instance creates its own module finder instance. The finder can be configured when instantiating the loader thanks to the loader's search_paths parameter.
Parameters:
Methods:
-
append_search_path–Append a search path.
-
find_package–Find a package or namespace package.
-
find_spec–Find the top-level parent module of a module.
-
insert_search_path–Insert a search path at the given position.
-
iter_submodules–Iterate on a module's submodules, if any.
-
submodules–Return the list of a module's submodules.
Attributes:
-
accepted_py_module_extensions(list[str]) –List of extensions supported by the finder.
-
extensions_set(set[str]) –Set of extensions supported by the finder.
-
search_paths(list[Path]) –The finder search paths.
Source code in src/griffe/_internal/finder.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
accepted_py_module_extensions class-attribute ¤
List of extensions supported by the finder.
extensions_set class-attribute ¤
extensions_set: set[str] = set(
accepted_py_module_extensions
)
Set of extensions supported by the finder.
append_search_path ¤
Append a search path.
The path will be resolved (absolute, normalized). The path won't be appended if it is already in the search paths list.
Parameters:
Source code in src/griffe/_internal/finder.py
119 120 121 122 123 124 125 126 127 128 | |
find_package ¤
find_package(
module_name: str,
) -> Package | NamespacePackage
Find a package or namespace package.
Parameters:
Raises:
-
ModuleNotFoundError–When the module cannot be found.
Returns:
-
Package | NamespacePackage–A package or namespace package wrapper.
Source code in src/griffe/_internal/finder.py
231 232 233 234 235 236 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 | |
find_spec ¤
find_spec(
module: str | Path,
*,
try_relative_path: bool = True,
find_stubs_package: bool = False,
) -> tuple[str, Package | NamespacePackage]
Find the top-level parent module of a module.
If a Path is passed, only try to find the module as a file path. If a string is passed, first try to find the module as a file path, then look into the search paths.
Parameters:
-
(module¤str | Path) –The module name or path.
-
(try_relative_path¤bool, default:True) –Whether to try finding the module as a relative path, when the given module is not already a 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:
-
FileNotFoundError–When a Path was passed and the module could not be found:
- the directory has no
__init__.pyfile in it - the path does not exist
- the directory has no
-
ModuleNotFoundError–When a string was passed and the module could not be found:
- no
module/__init__.py - no
module.py - no
module.pth - no
moduledirectory (namespace packages) - or unsupported .pth file
- no
Returns:
-
tuple[str, Package | NamespacePackage]–The name of the module, and an instance representing its (namespace) package.
Source code in src/griffe/_internal/finder.py
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
insert_search_path ¤
Insert a search path at the given position.
The path will be resolved (absolute, normalized). The path won't be inserted if it is already in the search paths list.
Parameters:
Source code in src/griffe/_internal/finder.py
134 135 136 137 138 139 140 141 142 143 144 145 146 | |
iter_submodules ¤
iter_submodules(
path: Path | list[Path], seen: set | None = None
) -> Iterator[NamePartsAndPathType]
Iterate on a module's submodules, if any.
Parameters:
-
(path¤Path | list[Path]) –The module path.
-
(seen¤set | None, default:None) –If not none, this set is used to skip some files. The goal is to replicate the behavior of Python by only using the first packages (with
__init__modules) of the same name found in different namespace packages. As soon as we find an__init__module, we add its parent path to theseenset, which will be reused when scanning the next namespace packages.
Yields:
-
name_parts(tuple[str, ...]) –The parts of a submodule name.
-
filepath(Path) –A submodule filepath.
Source code in src/griffe/_internal/finder.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 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 333 334 335 336 337 338 339 340 341 342 | |
submodules ¤
submodules(module: Module) -> list[NamePartsAndPathType]
Return the list of a module's submodules.
Parameters:
Returns:
-
list[NamePartsAndPathType]–A list of tuples containing the parts of the submodule name and its path.
Source code in src/griffe/_internal/finder.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
Package dataclass ¤
NamespacePackage dataclass ¤
This class is a simple placeholder used during the process of finding packages.
Parameters:
Attributes:
Types¤
NamePartsType module-attribute ¤
Type alias for the parts of a module name.
NamePartsAndPathType module-attribute ¤
NamePartsAndPathType = tuple[NamePartsType, Path]
Type alias for the parts of a module name and its path.