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 | class 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.
"""
def __init__(
self,
filters: Optional[list[str]] = None,
docstring_style: str = "google",
docstring_options: Optional[dict] = None,
inherited_members: bool = False, # noqa: FBT001, FBT002
new_path_syntax: bool = False, # noqa: FBT001, FBT002
) -> None:
"""Initialize the object.
Arguments:
filters: A list of regular expressions to fine-grain select members. It is applied recursively.
docstring_style: The style to use when parsing docstrings.
docstring_options: The options to pass to the docstrings parser.
inherited_members: Whether to select inherited members for classes.
new_path_syntax: Whether to use the "colon" syntax for the path.
"""
if not filters:
filters = []
self.filters = [(filtr, re.compile(filtr.lstrip("!"))) for filtr in filters]
self.docstring_parser = PARSERS[docstring_style](**(docstring_options or {}))
self.errors: list[str] = []
self.select_inherited_members = inherited_members
self.new_path_syntax = new_path_syntax
def get_object_documentation(self, dotted_path: str, members: Optional[Union[set[str], bool]] = None) -> Object:
"""Get the documentation for an object and its children.
Arguments:
dotted_path: The Python dotted path to the desired object.
members: `True` to select members and filter them, `False` to select no members,
or a list of names to explicitly select the members with these names.
It is applied only on the root object.
Returns:
The documented object.
"""
if members is True:
members = set()
root_object: Object
leaf = get_object_tree(dotted_path, self.new_path_syntax)
if leaf.is_module():
root_object = self.get_module_documentation(leaf, members)
elif leaf.is_class():
root_object = self.get_class_documentation(leaf, members)
elif leaf.is_staticmethod():
root_object = self.get_staticmethod_documentation(leaf)
elif leaf.is_classmethod():
root_object = self.get_classmethod_documentation(leaf)
elif leaf.is_method_descriptor() or leaf.is_method():
root_object = self.get_regular_method_documentation(leaf)
elif leaf.is_function():
root_object = self.get_function_documentation(leaf)
elif leaf.is_property():
root_object = self.get_property_documentation(leaf)
else:
root_object = self.get_attribute_documentation(leaf)
root_object.parse_all_docstrings(self.docstring_parser)
return root_object
def get_module_documentation(
self,
node: ObjectNode,
select_members: Optional[Union[set[str], bool]] = None,
) -> Module:
"""Get the documentation for a module and its children.
Arguments:
node: The node representing the module and its parents.
select_members: Explicit members to select.
Returns:
The documented module object.
"""
module = node.obj
path = node.dotted_path
name = path.split(".")[-1]
source: Optional[Source]
try:
source = Source(inspect.getsource(module), 1)
except OSError:
try:
code = Path(node.file_path).read_text()
except (OSError, UnicodeDecodeError):
source = None
else:
source = Source(code, 1) if code else None
root_object = Module(
name=name,
path=path,
file_path=node.file_path,
docstring=inspect.getdoc(module),
source=source,
)
if select_members is False:
return root_object
select_members = select_members or set()
attributes_data = get_module_attributes(module)
root_object.parse_docstring(self.docstring_parser, attributes=attributes_data)
for member_name, member in inspect.getmembers(module):
if self.select(member_name, select_members): # type: ignore[arg-type]
child_node = ObjectNode(member, member_name, parent=node)
if child_node.is_class() and node.root.obj is inspect.getmodule(child_node.obj):
root_object.add_child(self.get_class_documentation(child_node))
elif child_node.is_function() and node.root.obj is inspect.getmodule(child_node.obj):
root_object.add_child(self.get_function_documentation(child_node))
elif member_name in attributes_data:
root_object.add_child(self.get_attribute_documentation(child_node, attributes_data[member_name]))
if hasattr(module, "__path__"):
for _, modname, _ in pkgutil.iter_modules(module.__path__):
if self.select(modname, select_members): # type: ignore[arg-type]
leaf = get_object_tree(f"{path}.{modname}")
root_object.add_child(self.get_module_documentation(leaf))
return root_object
@staticmethod
def _class_path(cls: type) -> str: # noqa: PLW0211
mod = cls.__module__
qname = cls.__qualname__
if mod == "builtins":
return qname
return f"{mod}.{qname}"
def get_class_documentation(
self,
node: ObjectNode,
select_members: Optional[Union[set[str], bool]] = None,
) -> Class:
"""Get the documentation for a class and its children.
Arguments:
node: The node representing the class and its parents.
select_members: Explicit members to select.
Returns:
The documented class object.
"""
class_ = node.obj
docstring = inspect.cleandoc(class_.__doc__ or "")
bases = [self._class_path(b) for b in class_.__bases__]
source: Optional[Source]
try:
source = Source(*inspect.getsourcelines(node.obj))
except (OSError, TypeError):
source = None
root_object = Class(
name=node.name,
path=node.dotted_path,
file_path=node.file_path,
docstring=docstring,
bases=bases,
source=source,
)
# Even if we don't select members, we want to correctly parse the docstring
attributes_data: dict[str, dict[str, Any]] = {}
for parent_class in reversed(class_.__mro__[:-1]):
merge(attributes_data, get_class_attributes(parent_class))
context: dict[str, Any] = {"attributes": attributes_data}
if "__init__" in class_.__dict__:
try:
attributes_data.update(get_instance_attributes(class_.__init__))
context["signature"] = inspect.signature(class_.__init__)
except (TypeError, ValueError):
pass
root_object.parse_docstring(self.docstring_parser, **context)
if select_members is False:
return root_object
select_members = select_members or set()
# Build the list of members
members = {}
inherited = set()
direct_members = class_.__dict__
all_members = dict(inspect.getmembers(class_))
for member_name, member in all_members.items():
if member is class_:
continue
if not (member is type or member is object) and self.select(member_name, select_members): # type: ignore[arg-type]
if member_name not in direct_members:
if self.select_inherited_members:
members[member_name] = member
inherited.add(member_name)
else:
members[member_name] = member
# Iterate on the selected members
child: Object
for member_name, member in members.items():
child_node = ObjectNode(member, member_name, parent=node)
if child_node.is_class():
child = self.get_class_documentation(child_node)
elif child_node.is_classmethod():
child = self.get_classmethod_documentation(child_node)
elif child_node.is_staticmethod():
child = self.get_staticmethod_documentation(child_node)
elif child_node.is_method():
child = self.get_regular_method_documentation(child_node)
elif child_node.is_property():
child = self.get_property_documentation(child_node)
elif member_name in attributes_data:
child = self.get_attribute_documentation(child_node, attributes_data[member_name])
else:
continue
if member_name in inherited:
child.properties.append("inherited")
root_object.add_child(child)
for attr_name, properties, add_method in (
("__fields__", ["pydantic-model"], self.get_pydantic_field_documentation),
("_declared_fields", ["marshmallow-model"], self.get_marshmallow_field_documentation),
("_meta.get_fields", ["django-model"], self.get_django_field_documentation),
("__dataclass_fields__", ["dataclass"], self.get_annotated_dataclass_field),
):
if self.detect_field_model(attr_name, direct_members, all_members):
root_object.properties.extend(properties)
self.add_fields(
node,
root_object,
attr_name,
all_members,
select_members,
class_,
add_method,
)
break
return root_object
def detect_field_model(self, attr_name: str, direct_members: Sequence[str], all_members: dict) -> bool:
"""Detect if an attribute is present in members.
Arguments:
attr_name: The name of the attribute to detect, can contain dots.
direct_members: The direct members of the class.
all_members: All members of the class.
Returns:
Whether the attribute is present.
"""
first_order_attr_name, remainder = split_attr_name(attr_name)
if not (
first_order_attr_name in direct_members
or (self.select_inherited_members and first_order_attr_name in all_members)
):
return False
if remainder:
with suppress(AttributeError):
return bool(attrgetter(remainder)(all_members[first_order_attr_name]))
return True
def add_fields(
self,
node: ObjectNode,
root_object: Object,
attr_name: str,
members: Mapping[str, Any],
select_members: Optional[Union[set[str], bool]],
base_class: type,
add_method: Callable,
) -> None:
"""Add detected fields to the current object.
Arguments:
node: The current object node.
root_object: The current object.
attr_name: The fields attribute name.
members: The members to pick the fields attribute in.
select_members: The members to select.
base_class: The class declaring the fields.
add_method: The method to add the children object.
"""
fields = get_fields(attr_name, members=members)
for field_name, field in fields.items():
select_field = self.select(field_name, select_members) # type: ignore[arg-type]
is_inherited = field_is_inherited(field_name, attr_name, base_class)
if select_field and (self.select_inherited_members or not is_inherited):
child_node = ObjectNode(obj=field, name=field_name, parent=node)
root_object.add_child(add_method(child_node))
def get_function_documentation(self, node: ObjectNode) -> Function:
"""Get the documentation for a function.
Arguments:
node: The node representing the function and its parents.
Returns:
The documented function object.
"""
function = node.obj
source: Optional[Source]
signature: Optional[inspect.Signature]
try:
signature = inspect.signature(function)
except TypeError:
signature = None
try:
source = Source(*inspect.getsourcelines(function))
except OSError:
source = None
properties: list[str] = []
if node.is_coroutine_function():
properties.append("async")
return Function(
name=node.name,
path=node.dotted_path,
file_path=node.file_path,
docstring=inspect.getdoc(function),
signature=signature,
source=source,
properties=properties,
)
def get_property_documentation(self, node: ObjectNode) -> Attribute:
"""Get the documentation for a property.
Arguments:
node: The node representing the property and its parents.
Returns:
The documented attribute object (properties are considered attributes for now).
"""
prop = node.obj
path = node.dotted_path
properties = ["property"]
if node.is_cached_property():
# cached_property is always writable, see the docs
properties.extend(["writable", "cached"])
sig_source_func = prop.func
else:
properties.append("readonly" if prop.fset is None else "writable")
sig_source_func = prop.fget
source: Optional[Source]
try:
signature = inspect.signature(sig_source_func)
except (TypeError, ValueError):
attr_type = None
else:
attr_type = signature.return_annotation
try:
source = Source(*inspect.getsourcelines(sig_source_func))
except (OSError, TypeError):
source = None
return Attribute(
name=node.name,
path=path,
file_path=node.file_path,
docstring=inspect.getdoc(prop),
attr_type=attr_type,
properties=properties,
source=source,
)
@staticmethod
def get_pydantic_field_documentation(node: ObjectNode) -> Attribute:
"""Get the documentation for a Pydantic Field.
Arguments:
node: The node representing the Field and its parents.
Returns:
The documented attribute object.
"""
prop = node.obj
path = node.dotted_path
properties = ["pydantic-field"]
if prop.required:
properties.append("required")
return Attribute(
name=node.name,
path=path
|