New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autodoc unable to mock submodules #2174

Closed
Psirus opened this Issue Dec 15, 2015 · 9 comments

Comments

Projects
None yet
4 participants
@Psirus

Psirus commented Dec 15, 2015

conf.py:

autodoc_mock_imports = ['PySide.QtGui']

foo.py:

import PySide.QtGui as QtGui

class Gui(QtGui.QMainWindow):
    """ Example Docstring """

foo.rst:

.. automodule:: foo
   :members:

Gives this error:

AttributeError: type object 'QtGui' has no attribute 'QMainWindow'

@tk0miya tk0miya added the autodoc label Dec 16, 2015

@danbrees

This comment has been minimized.

Show comment
Hide comment
@danbrees

danbrees Mar 15, 2016

I am running into the same type of issue. I am trying to mock the import of a third party submodule of a submodule of a module (mod.Submod.subsubmod). When Submod has a capitalized first letter, I get:
AttributeError: type object 'Submod' has no attribute 'subsubmod'

I don't get the error if Submod is submod. I think Sphinx is interpreting Submod as a class, not a submodule. Unfortunately, I cannot just lowercase the submodule name.

danbrees commented Mar 15, 2016

I am running into the same type of issue. I am trying to mock the import of a third party submodule of a submodule of a module (mod.Submod.subsubmod). When Submod has a capitalized first letter, I get:
AttributeError: type object 'Submod' has no attribute 'subsubmod'

I don't get the error if Submod is submod. I think Sphinx is interpreting Submod as a class, not a submodule. Unfortunately, I cannot just lowercase the submodule name.

@AlexBudesteanu

This comment has been minimized.

Show comment
Hide comment
@AlexBudesteanu

AlexBudesteanu Nov 1, 2017

Is there a fix for this issue or a way to get around it?

AlexBudesteanu commented Nov 1, 2017

Is there a fix for this issue or a way to get around it?

@tk0miya

This comment has been minimized.

Show comment
Hide comment
@tk0miya

tk0miya Nov 1, 2017

Member

Could you share me a reproducible example? I'd like to investigate that.

Member

tk0miya commented Nov 1, 2017

Could you share me a reproducible example? I'd like to investigate that.

@tk0miya tk0miya added the bug label Nov 1, 2017

@AlexBudesteanu

This comment has been minimized.

Show comment
Hide comment
@AlexBudesteanu

AlexBudesteanu Nov 1, 2017

Before giving an example, I would like to enforce what @danbrees said:

I think Sphinx is interpreting Submod as a class, not a submodule.

by referencing _MockModule class in /ext/autodoc.py on my machine. (I have installed sphinx via debian so i have version '1.3.6-2ubuntu1.1')

class _MockModule(object):
    """Used by autodoc_mock_imports."""
    def __init__(self, *args, **kwargs):
        pass

    def __call__(self, *args, **kwargs):
        return _MockModule()

    @classmethod
    def __getattr__(cls, name):
        if name in ('__file__', '__path__'):
            return '/dev/null'
        elif name[0] == name[0].upper():
            # Not very good, we assume Uppercase names are classes...
            mocktype = type(name, (), {})
            mocktype.__module__ = __name__
            return mocktype
        else:
            return _MockModule()

AlexBudesteanu commented Nov 1, 2017

Before giving an example, I would like to enforce what @danbrees said:

I think Sphinx is interpreting Submod as a class, not a submodule.

by referencing _MockModule class in /ext/autodoc.py on my machine. (I have installed sphinx via debian so i have version '1.3.6-2ubuntu1.1')

class _MockModule(object):
    """Used by autodoc_mock_imports."""
    def __init__(self, *args, **kwargs):
        pass

    def __call__(self, *args, **kwargs):
        return _MockModule()

    @classmethod
    def __getattr__(cls, name):
        if name in ('__file__', '__path__'):
            return '/dev/null'
        elif name[0] == name[0].upper():
            # Not very good, we assume Uppercase names are classes...
            mocktype = type(name, (), {})
            mocktype.__module__ = __name__
            return mocktype
        else:
            return _MockModule()
@tk0miya

This comment has been minimized.

Show comment
Hide comment
@tk0miya

tk0miya Nov 1, 2017

Member

(I have installed sphinx via debian so i have version '1.3.6-2ubuntu1.1')

Now newest stable version is 1.6.5.
1.3 series is too old. Could you try newest version? It is much improved.

Member

tk0miya commented Nov 1, 2017

(I have installed sphinx via debian so i have version '1.3.6-2ubuntu1.1')

Now newest stable version is 1.6.5.
1.3 series is too old. Could you try newest version? It is much improved.

@AlexBudesteanu

This comment has been minimized.

Show comment
Hide comment
@AlexBudesteanu

AlexBudesteanu Nov 1, 2017

I can confirm that it works with v1.6.5.
Thank you for the support!

AlexBudesteanu commented Nov 1, 2017

I can confirm that it works with v1.6.5.
Thank you for the support!

@tk0miya

This comment has been minimized.

Show comment
Hide comment
@tk0miya

tk0miya Jan 8, 2018

Member

@Psirus Have you still been in trouble with newest Sphinx?
If not, I will close this. Please let me know your status.

Member

tk0miya commented Jan 8, 2018

@Psirus Have you still been in trouble with newest Sphinx?
If not, I will close this. Please let me know your status.

@Psirus

This comment has been minimized.

Show comment
Hide comment
@Psirus

Psirus Jan 8, 2018

No trouble; it works under 1.6.5. Thank you.

Psirus commented Jan 8, 2018

No trouble; it works under 1.6.5. Thank you.

@tk0miya

This comment has been minimized.

Show comment
Hide comment
@tk0miya

tk0miya Jan 8, 2018

Member

Thank you for confirmation.
This issue was already fixed. so I close this now :-)

Member

tk0miya commented Jan 8, 2018

Thank you for confirmation.
This issue was already fixed. so I close this now :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment