@@ -660,24 +660,20 @@ def validate(obj_name, validator_cls=None, **validator_kwargs):
660
660
# cls = Validator._load_obj(f"{doc.name[:-9]}.{cls_name}") ## Alternative
661
661
cls_doc = Validator (get_doc_object (cls ))
662
662
elif isinstance (doc , AstValidator ): # Supports class traversal for ASTs.
663
- names = doc ._name .split ("." )
664
-
665
- if len (names ) > 2 : # i.e. module.class.__init__
666
- nested_cls_names = names [1 :- 1 ] # class1,class2 etc.
667
- cls_name = names [- 2 ]
668
- filename = doc .source_file_name # from the AstValidator object
669
-
670
- # Use AST to find the class node...
671
- with open (filename ) as file :
672
- module_node = ast .parse (file .read (), filename )
673
-
674
- # Recursively find each subclass from the module node.
675
- next_node = module_node
676
- for name in nested_cls_names :
677
- next_node = _find_class_node (next_node , name )
678
- # Get the documentation.
663
+ ancestry = doc .ancestry
664
+ if len (ancestry ) > 2 : # e.g. module.class.__init__
665
+ parent = doc .ancestry [- 1 ] # Get the parent
666
+ cls_name = "." .join (
667
+ [
668
+ getattr (node , "name" , node .__module__ )
669
+ for node in doc .ancestry
670
+ ]
671
+ )
679
672
cls_doc = AstValidator (
680
- ast_node = next_node , filename = filename , obj_name = cls_name
673
+ ast_node = parent ,
674
+ filename = doc .source_file_name ,
675
+ obj_name = cls_name ,
676
+ ancestry = doc .ancestry [:- 1 ],
681
677
)
682
678
else :
683
679
# Ignore edge case: __init__ functions that don't belong to a class.
0 commit comments