Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

python - autodoc: base classes are shown with full name instead of respecting the import

I hope I find a solution here for this quite intricate problem.

I use sphinx and intersphinx to document my project.

I have a class that inherits from mongoengine.Document.

When I build sphinx docs using sphinx-apidoc and the sphinx-build (via the autogenerated Makefile by sphinx-quickstart), references to mongoengine.Document classes are shown as mongoengine.document.Document, which is actually the correct fully qualified name but this is a problem, because on mongoengine project that class is labelled as mongoengine.Document so intersphinx doesn't link at all.

Is there a way to tell to sphinx to produce information on base classes as they are imported (in my code i have from mongoengine import Document) instead of its full module path?

The following code:

from mongoengine import Document, EmbeddedDocumentListField

class MyDocument(Document):
""" my docstring """

it produces some html like:

class myproj.models.MyDocument(*args, **values) Bases:
mongoengine.document.Document  <-- intersphinx does not find the link to external doc!

instead of

class myproj.models.MyDocument(*args, **values)
Bases: mongoengine.Document <-- here intersphinx will properly link
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The __module__ attribute holds the name of the module in which the class was defined. The value of Document.__module__ is "mongoengine.document".

The attribute is writable, so a workaround is to add the following line to the code:

Document.__module__ = "mongoengine"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...