Agents¤
Griffe is able to analyze code both statically and dynamically.
Main API¤
visit ¤
visit(
module_name: str,
filepath: Path,
code: str,
*,
extensions: Extensions | None = None,
parent: Module | None = None,
docstring_parser: DocstringStyle | Parser | None = None,
docstring_options: DocstringOptions | None = None,
lines_collection: LinesCollection | None = None,
modules_collection: ModulesCollection | None = None,
) -> Module
Parse and visit a module file.
We provide this function for static analysis. It uses a NodeVisitor-like class, the Visitor, to compile and parse code (using compile) then visit the resulting AST (Abstract Syntax Tree).
Important
This function is generally not used directly. In most cases, users can rely on the GriffeLoader and its accompanying load shortcut and their respective options to load modules using static analysis.
Parameters:
-
(module_name¤str) –The module name (as when importing [from] it).
-
(filepath¤Path) –The module file path.
-
(code¤str) –The module contents.
-
(extensions¤Extensions | None, default:None) –The extensions to use when visiting the AST.
-
(parent¤Module | None, default:None) –The optional parent of this module.
-
(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.
Returns:
-
Module–The module, with its members populated.
Source code in src/griffe/_internal/agents/visitor.py
80 81 82 83 84 85 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 | |
inspect ¤
inspect(
module_name: str,
*,
filepath: Path | None = None,
import_paths: Sequence[str | Path] | None = None,
extensions: Extensions | None = None,
parent: Module | None = None,
docstring_parser: DocstringStyle | Parser | None = None,
docstring_options: DocstringOptions | None = None,
lines_collection: LinesCollection | None = None,
modules_collection: ModulesCollection | None = None,
) -> Module
Inspect a module.
Sometimes we cannot get the source code of a module or an object, typically built-in modules like itertools. The only way to know what they are made of is to actually import them and inspect their contents.
Sometimes, even if the source code is available, loading the object is desired because it was created or modified dynamically, and our static agent is not powerful enough to infer all these dynamic modifications. In this case, we load the module using introspection.
Griffe therefore provides this function for dynamic analysis. It uses a NodeVisitor-like class, the Inspector, to inspect the module with inspect.getmembers().
The inspection agent works similarly to the regular Visitor agent, in that it maintains a state with the current object being handled, and recursively handle its members.
Important
This function is generally not used directly. In most cases, users can rely on the GriffeLoader and its accompanying load shortcut and their respective options to load modules using dynamic analysis.
Parameters:
-
(module_name¤str) –The module name (as when importing [from] it).
-
(filepath¤Path | None, default:None) –The module file path.
-
(import_paths¤Sequence[str | Path] | None, default:None) –Paths to import the module from.
-
(extensions¤Extensions | None, default:None) –The extensions to use when inspecting the module.
-
(parent¤Module | None, default:None) –The optional parent of this module.
-
(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.
Returns:
-
Module–The module, with its members populated.
Source code in src/griffe/_internal/agents/inspector.py
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
Advanced API¤
Visitor ¤
Visitor(
module_name: str,
filepath: Path,
code: str,
extensions: Extensions,
parent: Module | None = None,
docstring_parser: DocstringStyle | Parser | None = None,
docstring_options: DocstringOptions | None = None,
lines_collection: LinesCollection | None = None,
modules_collection: ModulesCollection | None = None,
)
This class is used to instantiate a visitor.
Visitors iterate on AST nodes to extract data from them.
Parameters:
-
(module_name¤str) –The module name.
-
(filepath¤Path) –The module filepath.
-
(code¤str) –The module source code.
-
(extensions¤Extensions) –The extensions to use when visiting.
-
(parent¤Module | None, default:None) –An optional parent for the final module object.
-
(docstring_parser¤DocstringStyle | Parser | None, default:None) –The docstring parser to use.
-
(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.
- Guide User guide Manipulating APIs Extending APIs Writing extensions How it works
- Reference Python API Agents
Methods:
-
decorators_to_labels–Build and return a set of labels based on decorators.
-
generic_visit–Extend the base generic visit with extensions.
-
get_base_property–Check decorators to return the base property in case of setters and deleters.
-
get_module–Build and return the object representing the module attached to this visitor.
-
handle_attribute–Handle an attribute (assignment) node.
-
handle_function–Handle a function definition node.
-
visit–Extend the base visit with extensions.
-
visit_annassign–Visit an annotated assignment node.
-
visit_assign–Visit an assignment node.
-
visit_asyncfunctiondef–Visit an async function definition node.
-
visit_augassign–Visit an augmented assignment node.
-
visit_classdef–Visit a class definition node.
-
visit_functiondef–Visit a function definition node.
-
visit_if–Visit an "if" node.
-
visit_import–Visit an import node.
-
visit_importfrom–Visit an "import from" node.
-
visit_module–Visit a module node.
-
visit_typealias–Visit a type alias node.
Attributes:
-
code(str) –The module source code.
-
current(Module | Class) –The current object being visited.
-
docstring_options(DocstringOptions) –The docstring parsing options.
-
docstring_parser(DocstringStyle | Parser | None) –The docstring parser to use.
-
extensions(Extensions) –The extensions to use when visiting the AST.
-
filepath(Path) –The module filepath.
-
lines_collection(LinesCollection) –A collection of source code lines.
-
module_name(str) –The module name.
-
modules_collection(ModulesCollection) –A collection of modules.
-
parent(Module | None) –An optional parent for the final module object.
-
type_guarded(bool) –Whether the current code branch is type-guarded.
Source code in src/griffe/_internal/agents/visitor.py
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 | |
docstring_options instance-attribute ¤
docstring_options: DocstringOptions = (
docstring_options or {}
)
The docstring parsing options.
docstring_parser instance-attribute ¤
docstring_parser: DocstringStyle | Parser | None = (
docstring_parser
)
The docstring parser to use.
extensions instance-attribute ¤
extensions: Extensions = extensions
The extensions to use when visiting the AST.
lines_collection instance-attribute ¤
lines_collection: LinesCollection = (
lines_collection or LinesCollection()
)
A collection of source code lines.
modules_collection instance-attribute ¤
modules_collection: ModulesCollection = (
modules_collection or ModulesCollection()
)
A collection of modules.
parent instance-attribute ¤
An optional parent for the final module object.
type_guarded instance-attribute ¤
type_guarded: bool = False
Whether the current code branch is type-guarded.
decorators_to_labels ¤
decorators_to_labels(
decorators: list[Decorator],
) -> set[str]
Build and return a set of labels based on decorators.
Parameters:
Returns:
Source code in src/griffe/_internal/agents/visitor.py
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | |
generic_visit ¤
Extend the base generic visit with extensions.
Parameters:
Source code in src/griffe/_internal/agents/visitor.py
268 269 270 271 272 273 274 275 | |
get_base_property ¤
get_base_property(
decorators: list[Decorator], function: Function
) -> str | None
Check decorators to return the base property in case of setters and deleters.
Parameters:
Returns:
-
base_property(str | None) –The property for which the setter/deleted is set.
-
property_function(str | None) –Either
"setter"or"deleter".
Source code in src/griffe/_internal/agents/visitor.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | |
get_module ¤
get_module() -> Module
Build and return the object representing the module attached to this visitor.
This method triggers a complete visit of the module nodes.
Returns:
-
Module–A module instance.
Source code in src/griffe/_internal/agents/visitor.py
246 247 248 249 250 251 252 253 254 255 256 257 258 | |
handle_attribute ¤
Handle an attribute (assignment) node.
Parameters:
-
(node¤Assign | AnnAssign) –The node to visit.
-
(annotation¤str | Expr | None, default:None) –A potential annotation.
Source code in src/griffe/_internal/agents/visitor.py
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 | |
handle_function ¤
handle_function(
node: AsyncFunctionDef | FunctionDef,
labels: set | None = None,
) -> None
Handle a function definition node.
Parameters:
-
(node¤AsyncFunctionDef | FunctionDef) –The node to visit.
-
(labels¤set | None, default:None) –Labels to add to the data object.
Source code in src/griffe/_internal/agents/visitor.py
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 451 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 | |
visit ¤
Extend the base visit with extensions.
Parameters:
Source code in src/griffe/_internal/agents/visitor.py
260 261 262 263 264 265 266 | |
visit_annassign ¤
Visit an annotated assignment node.
Parameters:
Source code in src/griffe/_internal/agents/visitor.py
728 729 730 731 732 733 734 | |
visit_assign ¤
Visit an assignment node.
Parameters:
Source code in src/griffe/_internal/agents/visitor.py
720 721 722 723 724 725 726 | |
visit_asyncfunctiondef ¤
visit_asyncfunctiondef(node: AsyncFunctionDef) -> None
Visit an async function definition node.
Parameters:
-
(node¤AsyncFunctionDef) –The node to visit.
Source code in src/griffe/_internal/agents/visitor.py
509 510 511 512 513 514 515 | |
visit_augassign ¤
Visit an augmented assignment node.
Parameters:
Source code in src/griffe/_internal/agents/visitor.py
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 | |
visit_classdef ¤
Visit a class definition node.
Parameters:
Source code in src/griffe/_internal/agents/visitor.py
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 343 344 345 346 347 348 349 350 351 | |
visit_functiondef ¤
visit_functiondef(node: FunctionDef) -> None
Visit a function definition node.
Parameters:
-
(node¤FunctionDef) –The node to visit.
Source code in src/griffe/_internal/agents/visitor.py
501 502 503 504 505 506 507 | |
visit_if ¤
Visit an "if" node.
Parameters:
Source code in src/griffe/_internal/agents/visitor.py
757 758 759 760 761 762 763 764 765 766 767 768 | |
visit_import ¤
Visit an import node.
Parameters:
Source code in src/griffe/_internal/agents/visitor.py
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 | |
visit_importfrom ¤
visit_importfrom(node: ImportFrom) -> None
Visit an "import from" node.
Parameters:
-
(node¤ImportFrom) –The node to visit.
Source code in src/griffe/_internal/agents/visitor.py
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 | |
visit_module ¤
Visit a module node.
Parameters:
Source code in src/griffe/_internal/agents/visitor.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
visit_typealias ¤
Visit a type alias node.
Parameters:
Source code in src/griffe/_internal/agents/visitor.py
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | |
Inspector ¤
Inspector(
module_name: str,
filepath: Path | None,
extensions: Extensions,
parent: Module | None = None,
docstring_parser: DocstringStyle | Parser | None = None,
docstring_options: DocstringOptions | None = None,
lines_collection: LinesCollection | None = None,
modules_collection: ModulesCollection | None = None,
)
This class is used to instantiate an inspector.
Inspectors iterate on objects members to extract data from them.
Parameters:
-
(module_name¤str) –The module name.
-
(filepath¤Path | None) –The optional filepath.
-
(extensions¤Extensions) –Extensions to use when inspecting.
-
(parent¤Module | None, default:None) –The module parent.
-
(docstring_parser¤DocstringStyle | Parser | None, default:None) –The docstring parser to use.
-
(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.
Methods:
-
generic_inspect–Extend the base generic inspection with extensions.
-
get_module–Build and return the object representing the module attached to this inspector.
-
handle_attribute–Handle an attribute.
-
handle_function–Handle a function.
-
inspect–Extend the base inspection with extensions.
-
inspect_attribute–Inspect an attribute.
-
inspect_builtin_function–Inspect a builtin function.
-
inspect_builtin_method–Inspect a builtin method.
-
inspect_cached_property–Inspect a cached property.
-
inspect_class–Inspect a class.
-
inspect_classmethod–Inspect a class method.
-
inspect_coroutine–Inspect a coroutine.
-
inspect_function–Inspect a function.
-
inspect_getset_descriptor–Inspect a get/set descriptor.
-
inspect_method–Inspect a method.
-
inspect_method_descriptor–Inspect a method descriptor.
-
inspect_module–Inspect a module.
-
inspect_property–Inspect a property.
-
inspect_staticmethod–Inspect a static method.
-
inspect_type_alias–Inspect a type alias.
Attributes:
-
current(Module | Class) –The current object being inspected.
-
docstring_options(DocstringOptions) –The docstring parsing options.
-
docstring_parser(DocstringStyle | Parser | None) –The docstring parser to use.
-
extensions(Extensions) –The extensions to use when inspecting.
-
filepath(Path | None) –The module file path.
-
lines_collection(LinesCollection) –A collection of source code lines.
-
module_name(str) –The module name.
-
modules_collection(ModulesCollection) –A collection of modules.
-
parent(Module | None) –An optional parent for the final module object.
Source code in src/griffe/_internal/agents/inspector.py
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 | |
docstring_options instance-attribute ¤
docstring_options: DocstringOptions = (
docstring_options or {}
)
The docstring parsing options.
docstring_parser instance-attribute ¤
docstring_parser: DocstringStyle | Parser | None = (
docstring_parser
)
The docstring parser to use.
extensions instance-attribute ¤
extensions: Extensions = extensions
The extensions to use when inspecting.
lines_collection instance-attribute ¤
lines_collection: LinesCollection = (
lines_collection or LinesCollection()
)
A collection of source code lines.
modules_collection instance-attribute ¤
modules_collection: ModulesCollection = (
modules_collection or ModulesCollection()
)
A collection of modules.
parent instance-attribute ¤
An optional parent for the final module object.
generic_inspect ¤
generic_inspect(node: ObjectNode) -> None
Extend the base generic inspection with extensions.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
Source code in src/griffe/_internal/agents/inspector.py
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 292 293 294 295 296 297 298 | |
get_module ¤
get_module(
import_paths: Sequence[str | Path] | None = None,
) -> Module
Build and return the object representing the module attached to this inspector.
This method triggers a complete inspection of the module members.
Parameters:
-
(import_paths¤Sequence[str | Path] | None, default:None) –Paths replacing
sys.pathto import the module.
Returns:
-
Module–A module instance.
Source code in src/griffe/_internal/agents/inspector.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
handle_attribute ¤
handle_attribute(
node: ObjectNode, annotation: str | Expr | None = None
) -> None
Handle an attribute.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
-
(annotation¤str | Expr | None, default:None) –A potential annotation.
Source code in src/griffe/_internal/agents/inspector.py
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 | |
handle_function ¤
handle_function(
node: ObjectNode, labels: set | None = None
) -> None
Handle a function.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
-
(labels¤set | None, default:None) –Labels to add to the data object.
Source code in src/griffe/_internal/agents/inspector.py
445 446 447 448 449 450 451 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 | |
inspect ¤
inspect(node: ObjectNode) -> None
Extend the base inspection with extensions.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
Source code in src/griffe/_internal/agents/inspector.py
255 256 257 258 259 260 261 | |
inspect_attribute ¤
inspect_attribute(node: ObjectNode) -> None
Inspect an attribute.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
Source code in src/griffe/_internal/agents/inspector.py
536 537 538 539 540 541 542 | |
inspect_builtin_function ¤
inspect_builtin_function(node: ObjectNode) -> None
Inspect a builtin function.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
Source code in src/griffe/_internal/agents/inspector.py
405 406 407 408 409 410 411 | |
inspect_builtin_method ¤
inspect_builtin_method(node: ObjectNode) -> None
Inspect a builtin method.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
Source code in src/griffe/_internal/agents/inspector.py
381 382 383 384 385 386 387 | |
inspect_cached_property ¤
inspect_cached_property(node: ObjectNode) -> None
Inspect a cached property.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
Source code in src/griffe/_internal/agents/inspector.py
421 422 423 424 425 426 427 | |
inspect_class ¤
inspect_class(node: ObjectNode) -> None
Inspect a class.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
Source code in src/griffe/_internal/agents/inspector.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | |
inspect_classmethod ¤
inspect_classmethod(node: ObjectNode) -> None
Inspect a class method.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
Source code in src/griffe/_internal/agents/inspector.py
365 366 367 368 369 370 371 | |
inspect_coroutine ¤
inspect_coroutine(node: ObjectNode) -> None
Inspect a coroutine.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
Source code in src/griffe/_internal/agents/inspector.py
397 398 399 400 401 402 403 | |
inspect_function ¤
inspect_function(node: ObjectNode) -> None
Inspect a function.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
Source code in src/griffe/_internal/agents/inspector.py
413 414 415 416 417 418 419 | |
inspect_getset_descriptor ¤
inspect_getset_descriptor(node: ObjectNode) -> None
Inspect a get/set descriptor.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
Source code in src/griffe/_internal/agents/inspector.py
437 438 439 440 441 442 443 | |
inspect_method ¤
inspect_method(node: ObjectNode) -> None
Inspect a method.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
Source code in src/griffe/_internal/agents/inspector.py
389 390 391 392 393 394 395 | |
inspect_method_descriptor ¤
inspect_method_descriptor(node: ObjectNode) -> None
Inspect a method descriptor.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
Source code in src/griffe/_internal/agents/inspector.py
373 374 375 376 377 378 379 | |
inspect_module ¤
inspect_module(node: ObjectNode) -> None
Inspect a module.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
Source code in src/griffe/_internal/agents/inspector.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
inspect_property ¤
inspect_property(node: ObjectNode) -> None
Inspect a property.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
Source code in src/griffe/_internal/agents/inspector.py
429 430 431 432 433 434 435 | |
inspect_staticmethod ¤
inspect_staticmethod(node: ObjectNode) -> None
Inspect a static method.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
Source code in src/griffe/_internal/agents/inspector.py
357 358 359 360 361 362 363 | |
inspect_type_alias ¤
inspect_type_alias(node: ObjectNode) -> None
Inspect a type alias.
Parameters:
-
(node¤ObjectNode) –The node to inspect.
Source code in src/griffe/_internal/agents/inspector.py
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 | |
Dynamic analysis helpers¤
sys_path ¤
Redefine sys.path temporarily.
Parameters:
-
(*paths¤str | Path, default:()) –The paths to use when importing modules. If no paths are given, keep
sys.pathuntouched.
Yields:
-
None–Nothing.
Source code in src/griffe/_internal/importer.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
dynamic_import ¤
dynamic_import(
import_path: str,
import_paths: Sequence[str | Path] | None = None,
) -> Any
Dynamically import the specified object.
It can be a module, class, method, function, attribute, type alias, nested arbitrarily.
It works like this:
- for a given object path
a.b.x.y - it tries to import
a.b.x.yas a module (withimportlib.import_module) - if it fails, it tries again with
a.b.x, storingy - then
a.b, storingx.y - then
a, storingb.x.y - if nothing worked, it raises an error
- if one of the iteration worked, it moves on, and...
- it tries to get the remaining (stored) parts with
getattr - for example it gets
bfroma, thenxfromb, etc. - if a single attribute access fails, it raises an error
- if everything worked, it returns the last obtained attribute
Since the function potentially tries multiple things before succeeding, all errors happening along the way are recorded, and re-emitted with an ImportError when it fails, to let users know what was tried.
Important
The paths given through the import_paths parameter are used to temporarily patch sys.path: this function is therefore not thread-safe.
Important
The paths given as import_paths must be correct. The contents of sys.path must be consistent to what a user of the imported code would expect. Given a set of paths, if the import fails for a user, it will fail here too, with potentially unintuitive errors. If we wanted to make this function more robust, we could add a loop to "roll the window" of given paths, shifting them to the left (for example: ("/a/a", "/a/b", "/a/c/"), then ("/a/b", "/a/c", "/a/a/"), then ("/a/c", "/a/a", "/a/b/")), to make sure each entry is given highest priority at least once, maintaining relative order, but we deem this unnecessary for now.
Parameters:
-
(import_path¤str) –The path of the object to import.
-
(import_paths¤Sequence[str | Path] | None, default:None) –The (sys) paths to import the object from.
Raises:
-
ModuleNotFoundError–When the object's module could not be found.
-
ImportError–When there was an import error or when couldn't get the attribute.
Returns:
-
Any–The imported object.
Source code in src/griffe/_internal/importer.py
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 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 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 | |
ObjectNode ¤
Helper class to represent an object tree.
It's not really a tree but more a backward-linked list: each node has a reference to its parent, but not to its child (for simplicity purposes and to avoid bugs).
Each node stores an object, its name, and a reference to its parent node.
Parameters:
-
(obj¤Any) –A Python object.
-
(name¤str) –The object's name.
-
(parent¤ObjectNode | None, default:None) –The object's parent node.
- Guide User guide Manipulating APIs Extending APIs Writing extensions Visiting trees
- Reference Python API
- Agents
-
Inspector-
generic_inspect -
handle_attribute -
handle_function -
inspect -
inspect_attribute -
inspect_builtin_function -
inspect_builtin_method -
inspect_cached_property -
inspect_class -
inspect_classmethod -
inspect_coroutine -
inspect_function -
inspect_getset_descriptor -
inspect_method -
inspect_method_descriptor -
inspect_module -
inspect_property -
inspect_staticmethod -
inspect_type_alias
-
-
ObjectNode
-
- Extensions
Extension
- Agents
Attributes:
-
alias_target_path(str | None) –Alias target path of this node, if the node should be an alias.
-
children(Sequence[ObjectNode]) –The children of this node.
-
exclude_specials(set[str]) –Low level attributes known to cause issues when resolving aliases.
-
is_attribute(bool) –Whether this node's object is an attribute.
-
is_builtin_function(bool) –Whether this node's object is a builtin function.
-
is_builtin_method(bool) –Whether this node's object is a builtin method.
-
is_cached_property(bool) –Whether this node's object is a cached property.
-
is_class(bool) –Whether this node's object is a class.
-
is_classmethod(bool) –Whether this node's object is a classmethod.
-
is_coroutine(bool) –Whether this node's object is a coroutine.
-
is_function(bool) –Whether this node's object is a function.
-
is_getset_descriptor(bool) –Whether this node's object is a get/set descriptor.
-
is_method(bool) –Whether this node's object is a method.
-
is_method_descriptor(bool) –Whether this node's object is a method descriptor.
-
is_module(bool) –Whether this node's object is a module.
-
is_property(bool) –Whether this node's object is a property.
-
is_staticmethod(bool) –Whether this node's object is a staticmethod.
-
is_type_alias(bool) –Whether this node's object is a type alias.
-
kind(ObjectKind) –The kind of this node.
-
module(ObjectNode) –The object's module, fetched from the node tree.
-
module_path(str | None) –The object's module path.
-
name(str) –The Python object's name.
-
obj(Any) –The actual Python object.
-
parent(ObjectNode | None) –The parent node.
-
parent_is_class(bool) –Whether the object of this node's parent is a class.
-
path(str) –The object's (Python) path.
Source code in src/griffe/_internal/agents/nodes/runtime.py
52 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 | |
alias_target_path cached property ¤
alias_target_path: str | None
Alias target path of this node, if the node should be an alias.
exclude_specials class-attribute ¤
Low level attributes known to cause issues when resolving aliases.
is_builtin_function cached property ¤
is_builtin_function: bool
Whether this node's object is a builtin function.
is_builtin_method cached property ¤
is_builtin_method: bool
Whether this node's object is a builtin method.
is_cached_property instance-attribute ¤
is_cached_property: bool = is_cached_property
Whether this node's object is a cached property.
is_getset_descriptor cached property ¤
is_getset_descriptor: bool
Whether this node's object is a get/set descriptor.
is_method_descriptor cached property ¤
is_method_descriptor: bool
Whether this node's object is a method descriptor.
Built-in methods (e.g. those implemented in C/Rust) are often method descriptors, rather than normal methods.
is_staticmethod cached property ¤
is_staticmethod: bool
Whether this node's object is a staticmethod.
ObjectKind ¤
flowchart TD
griffe.ObjectKind[ObjectKind]
click griffe.ObjectKind href "" "griffe.ObjectKind"
Enumeration of the different runtime object kinds.
- Guide User guide Manipulating APIs Extending APIs Writing extensions
Attributes:
-
ATTRIBUTE–Attributes.
-
BUILTIN_FUNCTION–Built-in functions.
-
BUILTIN_METHOD–Built-in methods.
-
CACHED_PROPERTY–Cached properties.
-
CLASS–Classes.
-
CLASSMETHOD–Class methods.
-
COROUTINE–Coroutines
-
FUNCTION–Functions.
-
GETSET_DESCRIPTOR–Get/set descriptors.
-
METHOD–Methods.
-
METHOD_DESCRIPTOR–Method descriptors.
-
MODULE–Modules.
-
PROPERTY–Properties.
-
STATICMETHOD–Static methods.
-
TYPE_ALIAS–Type aliases.
BUILTIN_FUNCTION class-attribute instance-attribute ¤
BUILTIN_FUNCTION = 'builtin_function'
Built-in functions.
BUILTIN_METHOD class-attribute instance-attribute ¤
BUILTIN_METHOD = 'builtin_method'
Built-in methods.
CACHED_PROPERTY class-attribute instance-attribute ¤
CACHED_PROPERTY = 'cached_property'
Cached properties.
GETSET_DESCRIPTOR class-attribute instance-attribute ¤
GETSET_DESCRIPTOR = 'getset_descriptor'
Get/set descriptors.
METHOD_DESCRIPTOR class-attribute instance-attribute ¤
METHOD_DESCRIPTOR = 'method_descriptor'
Method descriptors.
Static analysis helpers¤
builtin_decorators module-attribute ¤
builtin_decorators = {
"property": "property",
"staticmethod": "staticmethod",
"classmethod": "classmethod",
}
Mapping of builtin decorators to labels.
stdlib_decorators module-attribute ¤
stdlib_decorators = {
"abc.abstractmethod": {"abstractmethod"},
"functools.cache": {"cached"},
"functools.cached_property": {"cached", "property"},
"cached_property.cached_property": {
"cached",
"property",
},
"functools.lru_cache": {"cached"},
"dataclasses.dataclass": {"dataclass"},
}
Mapping of standard library decorators to labels.
typing_overload module-attribute ¤
typing_overload = {
"typing.overload",
"typing_extensions.overload",
}
Set of recognized typing overload decorators.
When such a decorator is found, the decorated function becomes an overload.
ast_kind ¤
Return the kind of an AST node.
Parameters:
Returns:
-
str–The node kind.
Source code in src/griffe/_internal/agents/nodes/ast.py
14 15 16 17 18 19 20 21 22 23 | |
ast_children ¤
Return the children of an AST node.
Parameters:
Yields:
-
AST–The node children.
Source code in src/griffe/_internal/agents/nodes/ast.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
ast_previous_siblings ¤
Return the previous siblings of this node, starting from the closest.
Parameters:
Yields:
-
AST–The previous siblings.
Source code in src/griffe/_internal/agents/nodes/ast.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
ast_next_siblings ¤
Return the next siblings of this node, starting from the closest.
Parameters:
Yields:
-
AST–The next siblings.
Source code in src/griffe/_internal/agents/nodes/ast.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
ast_siblings ¤
Return the siblings of this node.
Parameters:
Yields:
-
AST–The siblings.
Source code in src/griffe/_internal/agents/nodes/ast.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
ast_previous ¤
Return the previous sibling of this node.
Parameters:
Raises:
-
LastNodeError–When the node does not have previous siblings.
Returns:
-
AST–The sibling.
Source code in src/griffe/_internal/agents/nodes/ast.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
ast_next ¤
Return the next sibling of this node.
Parameters:
Raises:
-
LastNodeError–When the node does not have next siblings.
Returns:
-
AST–The sibling.
Source code in src/griffe/_internal/agents/nodes/ast.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
ast_first_child ¤
Return the first child of this node.
Parameters:
Raises:
-
LastNodeError–When the node does not have children.
Returns:
-
AST–The child.
Source code in src/griffe/_internal/agents/nodes/ast.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
ast_last_child ¤
Return the lasts child of this node.
Parameters:
Raises:
-
LastNodeError–When the node does not have children.
Returns:
-
AST–The child.
Source code in src/griffe/_internal/agents/nodes/ast.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
get_docstring ¤
Extract a docstring.
Parameters:
-
(node¤AST) –The node to extract the docstring from.
-
(strict¤bool, default:False) –Whether to skip searching the body (functions).
Returns:
-
tuple[str | None, int | None, int | None]–A tuple with the value and line numbers of the docstring.
Source code in src/griffe/_internal/agents/nodes/docstrings.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
get_name ¤
Extract name from an assignment node.
Parameters:
Returns:
-
str–A list of names.
Source code in src/griffe/_internal/agents/nodes/assignments.py
23 24 25 26 27 28 29 30 31 32 | |
get_names ¤
Extract names from an assignment node.
Parameters:
Returns:
Source code in src/griffe/_internal/agents/nodes/assignments.py
51 52 53 54 55 56 57 58 59 60 | |
get_instance_names ¤
Extract names from an assignment node, only for instance attributes.
Parameters:
Returns:
Source code in src/griffe/_internal/agents/nodes/assignments.py
63 64 65 66 67 68 69 70 71 72 | |
get__all__ ¤
Get the values declared in __all__.
Parameters:
-
(node¤Assign | AnnAssign | AugAssign) –The assignment node.
-
(parent¤Module) –The parent module.
Returns:
Source code in src/griffe/_internal/agents/nodes/exports.py
78 79 80 81 82 83 84 85 86 87 88 89 90 | |
safe_get__all__ ¤
safe_get__all__(
node: Assign | AnnAssign | AugAssign,
parent: Module,
log_level: LogLevel = debug,
) -> list[str | ExprName]
Safely (no exception) extract values in __all__.
Parameters:
-
(node¤Assign | AnnAssign | AugAssign) –The
__all__assignment node. -
(parent¤Module) –The parent used to resolve the names.
-
(log_level¤LogLevel, default:debug) –Log level to use to log a message.
Returns:
Source code in src/griffe/_internal/agents/nodes/exports.py
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 | |
relative_to_absolute ¤
relative_to_absolute(
node: ImportFrom, name: alias, current_module: Module
) -> str
Convert a relative import path to an absolute one.
Parameters:
-
(node¤ImportFrom) –The "from ... import ..." AST node.
-
(name¤alias) –The imported name.
-
(current_module¤Module) –The module in which the import happens.
Returns:
-
str–The absolute import path.
Source code in src/griffe/_internal/agents/nodes/imports.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
get_parameters ¤
get_parameters(node: arguments) -> ParametersType
Source code in src/griffe/_internal/agents/nodes/parameters.py
18 19 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 73 74 75 76 77 78 79 80 81 82 83 | |
get_value ¤
Get the string representation of a node.
Parameters:
Returns:
-
str | None–The representing code for the node.
Source code in src/griffe/_internal/agents/nodes/values.py
15 16 17 18 19 20 21 22 23 24 25 26 | |
safe_get_value ¤
Safely (no exception) get the string representation of a node.
Parameters:
-
(node¤AST | None) –The node to represent.
-
(filepath¤str | Path | None, default:None) –An optional filepath from where the node comes.
Returns:
-
str | None–The representing code for the node.
Source code in src/griffe/_internal/agents/nodes/values.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
Deprecated API¤
ExportedName dataclass ¤
Deprecated. An intermediate class to store names.
The get__all__ function now returns instances of ExprName instead.
Attributes: