loader
This module is responsible for loading the documentation from Python objects.
It uses inspect for introspecting objects,
iterating over their members, etc.
Loader
¤
This class contains the object documentation loading mechanisms.
Any error that occurred during collection of the objects and their documentation is stored in the errors list.
Source code in src/pytkdocs/loader.py
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 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 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 510 511 512 513 514 515 516 517 518 519 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 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 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 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 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 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 852 853 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 937 938 939 940 941 942 943 | |
__init__(filters=None, docstring_style='google', docstring_options=None, inherited_members=False, new_path_syntax=False)
¤
Initialize the object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filters
|
Optional[list[str]]
|
A list of regular expressions to fine-grain select members. It is applied recursively. |
None
|
docstring_style
|
str
|
The style to use when parsing docstrings. |
'google'
|
docstring_options
|
Optional[dict]
|
The options to pass to the docstrings parser. |
None
|
inherited_members
|
bool
|
Whether to select inherited members for classes. |
False
|
new_path_syntax
|
bool
|
Whether to use the "colon" syntax for the path. |
False
|
Source code in src/pytkdocs/loader.py
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 | |
add_fields(node, root_object, attr_name, members, select_members, base_class, add_method)
¤
Add detected fields to the current object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
ObjectNode
|
The current object node. |
required |
root_object
|
Object
|
The current object. |
required |
attr_name
|
str
|
The fields attribute name. |
required |
members
|
Mapping[str, Any]
|
The members to pick the fields attribute in. |
required |
select_members
|
Optional[Union[set[str], bool]]
|
The members to select. |
required |
base_class
|
type
|
The class declaring the fields. |
required |
add_method
|
Callable
|
The method to add the children object. |
required |
Source code in src/pytkdocs/loader.py
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 591 592 593 | |
detect_field_model(attr_name, direct_members, all_members)
¤
Detect if an attribute is present in members.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attr_name
|
str
|
The name of the attribute to detect, can contain dots. |
required |
direct_members
|
Sequence[str]
|
The direct members of the class. |
required |
all_members
|
dict
|
All members of the class. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether the attribute is present. |
Source code in src/pytkdocs/loader.py
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 | |
filter_name_out(name)
cached
¤
Filter a name based on the loader's filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name to filter. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the name was filtered out, False otherwise. |
Source code in src/pytkdocs/loader.py
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 | |
get_annotated_dataclass_field(node, attribute_data=None)
staticmethod
¤
Get the documentation for a dataclass field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
ObjectNode
|
The node representing the annotation and its parents. |
required |
attribute_data
|
Optional[dict]
|
Docstring and annotation for this attribute. |
None
|
Returns:
| Type | Description |
|---|---|
Attribute
|
The documented attribute object. |
Source code in src/pytkdocs/loader.py
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 | |
get_attribute_documentation(node, attribute_data=None)
staticmethod
¤
Get the documentation for an attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
ObjectNode
|
The node representing the method and its parents. |
required |
attribute_data
|
Optional[dict]
|
Docstring and annotation for this attribute. |
None
|
Returns:
| Type | Description |
|---|---|
Attribute
|
The documented attribute object. |
Source code in src/pytkdocs/loader.py
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 | |
get_class_documentation(node, select_members=None)
¤
Get the documentation for a class and its children.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
ObjectNode
|
The node representing the class and its parents. |
required |
select_members
|
Optional[Union[set[str], bool]]
|
Explicit members to select. |
None
|
Returns:
| Type | Description |
|---|---|
Class
|
The documented class object. |
Source code in src/pytkdocs/loader.py
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 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 529 530 531 532 533 534 535 536 537 538 539 | |
get_classmethod_documentation(node)
¤
Get the documentation for a class-method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
ObjectNode
|
The node representing the class-method and its parents. |
required |
Returns:
| Type | Description |
|---|---|
Method
|
The documented method object. |
Source code in src/pytkdocs/loader.py
785 786 787 788 789 790 791 792 793 794 | |
get_django_field_documentation(node)
staticmethod
¤
Get the documentation for a Django Field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
ObjectNode
|
The node representing the Field and its parents. |
required |
Returns:
| Type | Description |
|---|---|
Attribute
|
The documented attribute object. |
Source code in src/pytkdocs/loader.py
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 | |
get_function_documentation(node)
¤
Get the documentation for a function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
ObjectNode
|
The node representing the function and its parents. |
required |
Returns:
| Type | Description |
|---|---|
Function
|
The documented function object. |
Source code in src/pytkdocs/loader.py
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 | |
get_marshmallow_field_documentation(node)
staticmethod
¤
Get the documentation for a Marshmallow Field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
ObjectNode
|
The node representing the Field and its parents. |
required |
Returns:
| Type | Description |
|---|---|
Attribute
|
The documented attribute object. |
Source code in src/pytkdocs/loader.py
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 | |
get_method_documentation(node, properties=None)
¤
Get the documentation for a method or method descriptor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
ObjectNode
|
The node representing the method and its parents. |
required |
properties
|
Optional[list[str]]
|
A list of properties to apply to the method. |
None
|
Returns:
| Type | Description |
|---|---|
Method
|
The documented method object. |
Source code in src/pytkdocs/loader.py
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 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 | |
get_module_documentation(node, select_members=None)
¤
Get the documentation for a module and its children.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
ObjectNode
|
The node representing the module and its parents. |
required |
select_members
|
Optional[Union[set[str], bool]]
|
Explicit members to select. |
None
|
Returns:
| Type | Description |
|---|---|
Module
|
The documented module object. |
Source code in src/pytkdocs/loader.py
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 | |
get_object_documentation(dotted_path, members=None)
¤
Get the documentation for an object and its children.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dotted_path
|
str
|
The Python dotted path to the desired object. |
required |
members
|
Optional[Union[set[str], bool]]
|
|
None
|
Returns:
| Type | Description |
|---|---|
Object
|
The documented object. |
Source code in src/pytkdocs/loader.py
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 352 353 354 355 356 357 | |
get_property_documentation(node)
¤
Get the documentation for a property.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
ObjectNode
|
The node representing the property and its parents. |
required |
Returns:
| Type | Description |
|---|---|
Attribute
|
The documented attribute object (properties are considered attributes for now). |
Source code in src/pytkdocs/loader.py
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 | |
get_pydantic_field_documentation(node)
staticmethod
¤
Get the documentation for a Pydantic Field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
ObjectNode
|
The node representing the Field and its parents. |
required |
Returns:
| Type | Description |
|---|---|
Attribute
|
The documented attribute object. |
Source code in src/pytkdocs/loader.py
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 | |
get_regular_method_documentation(node)
¤
Get the documentation for a regular method (not class- nor static-method).
We do extra processing in this method to discard docstrings of __init__ methods
that were inherited from parent classes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
ObjectNode
|
The node representing the method and its parents. |
required |
Returns:
| Type | Description |
|---|---|
Method
|
The documented method object. |
Source code in src/pytkdocs/loader.py
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 | |
get_staticmethod_documentation(node)
¤
Get the documentation for a static-method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
ObjectNode
|
The node representing the static-method and its parents. |
required |
Returns:
| Type | Description |
|---|---|
Method
|
The documented method object. |
Source code in src/pytkdocs/loader.py
796 797 798 799 800 801 802 803 804 805 | |
select(name, names)
¤
Tells whether we should select an object or not, given its name.
If the set of names is not empty, we check against it, otherwise we check against filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the object to select or not. |
required |
names
|
set[str]
|
An explicit list of names to select. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Yes or no. |
Source code in src/pytkdocs/loader.py
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 | |
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.
Source code in src/pytkdocs/loader.py
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 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 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 | |
dotted_path
property
¤
Return the Python dotted path to the object.
Returns:
| Type | Description |
|---|---|
str
|
The Python dotted path to the object. |
file_path
property
¤
Return the object's module file path.
Returns:
| Type | Description |
|---|---|
str
|
The object's module file path. |
name = name
instance-attribute
¤
The Python object's name.
obj = obj
instance-attribute
¤
The actual Python object.
parent = parent
instance-attribute
¤
The parent node.
root
property
¤
__init__(obj, name, parent=None)
¤
Initialize the object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
A Python object. |
required |
name
|
str
|
The object's name. |
required |
parent
|
Optional[ObjectNode]
|
The object's parent node. |
None
|
Source code in src/pytkdocs/loader.py
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 | |
is_cached_property()
¤
Tell if this node's object is a cached property.
Returns:
| Type | Description |
|---|---|
bool
|
If this node's object is a cached property. |
Source code in src/pytkdocs/loader.py
139 140 141 142 143 144 145 | |
is_class()
¤
Tell if this node's object is a class.
Returns:
| Type | Description |
|---|---|
bool
|
If this node's object is a class. |
Source code in src/pytkdocs/loader.py
107 108 109 110 111 112 113 | |
is_classmethod()
¤
Tell if this node's object is a classmethod.
Returns:
| Type | Description |
|---|---|
bool
|
If this node's object is a classmethod. |
Source code in src/pytkdocs/loader.py
186 187 188 189 190 191 192 193 194 195 | |
is_coroutine_function()
¤
Tell if this node's object is a coroutine.
Returns:
| Type | Description |
|---|---|
bool
|
If this node's object is a coroutine. |
Source code in src/pytkdocs/loader.py
123 124 125 126 127 128 129 | |
is_function()
¤
Tell if this node's object is a function.
Returns:
| Type | Description |
|---|---|
bool
|
If this node's object is a function. |
Source code in src/pytkdocs/loader.py
115 116 117 118 119 120 121 | |
is_method()
¤
Tell if this node's object is a method.
Returns:
| Type | Description |
|---|---|
bool
|
If this node's object is a method. |
Source code in src/pytkdocs/loader.py
155 156 157 158 159 160 161 162 | |
is_method_descriptor()
¤
Tell if 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.
Returns:
| Type | Description |
|---|---|
bool
|
If this node's object is a method descriptor. |
Source code in src/pytkdocs/loader.py
164 165 166 167 168 169 170 171 172 173 | |
is_module()
¤
Tell if this node's object is a module.
Returns:
| Type | Description |
|---|---|
bool
|
The root of the tree. |
Source code in src/pytkdocs/loader.py
99 100 101 102 103 104 105 | |
is_property()
¤
Tell if this node's object is a property.
Returns:
| Type | Description |
|---|---|
bool
|
If this node's object is a property. |
Source code in src/pytkdocs/loader.py
131 132 133 134 135 136 137 | |
is_staticmethod()
¤
Tell if this node's object is a staticmethod.
Returns:
| Type | Description |
|---|---|
bool
|
If this node's object is a staticmethod. |
Source code in src/pytkdocs/loader.py
175 176 177 178 179 180 181 182 183 184 | |
parent_is_class()
¤
Tell if the object of this node's parent is a class.
Returns:
| Type | Description |
|---|---|
bool
|
If the object of this node's parent is a class. |
Source code in src/pytkdocs/loader.py
147 148 149 150 151 152 153 | |
field_is_inherited(field_name, fields_name, base_class)
¤
Check if a field with a certain name was inherited from parent classes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_name
|
str
|
The name of the field to check. |
required |
fields_name
|
str
|
The name of the attribute in which the fields are stored. |
required |
base_class
|
type
|
The base class in which the field appears. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether the field was inherited. |
Source code in src/pytkdocs/loader.py
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 | |
get_object_tree(path, new_path_syntax=False)
¤
Transform a path into an actual Python object.
The path can be arbitrary long. You can pass the path to a package,
a module, a class, a function or a global variable, as deep as you
want, as long as the deepest module is importable through
importlib.import_module and each object is obtainable through
the getattr method. It is not possible to load local objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The dot/colon-separated path of the object. |
required |
new_path_syntax
|
bool
|
Whether to use the "colon" syntax for the path. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When the path is not valid (evaluates to |
ImportError
|
When the object or its parent module could not be imported. |
Returns:
| Type | Description |
|---|---|
ObjectNode
|
The leaf node representing the object and its parents. |
Source code in src/pytkdocs/loader.py
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 230 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 277 278 279 280 281 282 283 284 285 | |
split_attr_name(attr_name)
¤
Split an attribute name into a first-order attribute name and remainder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attr_name
|
str
|
Attribute name (a) |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, Optional[str]]
|
Tuple containing: first_order_attr_name: Name of the first order attribute (a) remainder: The remainder (b.c) |
Source code in src/pytkdocs/loader.py
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 | |