readthedocs / sphinx-autoapi

A new approach to API documentation in Sphinx.
https://sphinx-autoapi.readthedocs.io/
MIT License
415 stars 126 forks source link

Fix getting 'orig_path' for 'viewcode_follow_imported' #367

Closed Z-Fran closed 1 year ago

Z-Fran commented 1 year ago

Motivation

When I use autoapi to build aAPI docs and I want to use viewcode to add source code link in docs, I meet this problem. The built doc is https://mmediting.readthedocs.io/en/dev-1.x/ . I find that when an object's (class, function, or method) directory level is lower more than 2 levels than the directory level of the current API doc, the source link of this object will lost. For example:

The path of class AOTBlockNeck is mmedit.models.editors.aotgan.aot_neck.AOTBlockNeck and the path of the current doc is mmedit.models.editors. The source link will be lost. image But when the path of the current doc is mmedit.models.editors.aotgan, the source link will be built. image

And another example of the method object: the path of 'method add_datasample' is mmedit.visualization.concat_visualizer.ConcatImageVisualizer.add_datasample, the path of class ConcatImageVisualizer is mmedit.visualization.concat_visualizer.ConcatImageVisualizer, and the path of the current doc is mmedit.visualization. 2 levels gap of path gets the source link, but 3 levels gap doesn't. image

Modification

I think the reason is that some objects don't get their correct path of source code file. Related codes are in autoapi/extension.py/viewcode_follow_imported. After modification, We will iteratively find the original_path of original_path, until the final original_path is a real source code file.

This is test results: image image

AWhetter commented 1 year ago

This looks great. Thanks for the fix and sorry for taking so long to review it!