7

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
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
BangTheBank
  • 809
  • 3
  • 11
  • 26
  • Tricky problem. I don't know how to solve it. The bases of a class are determined from the `__bases__` variable. `MyDocument.__bases__` returns `(,)`. – mzjn Dec 20 '17 at 15:22
  • 2
    Found a workaround. Add `Document.__module__ = "mongoengine"` to your code. – mzjn Dec 20 '17 at 15:37
  • @mzjn thanks it worked! I think you can make an answer from it. – BangTheBank Dec 21 '17 at 09:36

1 Answers1

1

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"
mzjn
  • 48,958
  • 13
  • 128
  • 248