Skip to content

Replace hand-written Mock-class with unittest.mock.MagickMock in the FAQ trick for 'autodoc` #865

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

Closed
ankostis opened this issue Aug 1, 2014 · 6 comments

Comments

@ankostis
Copy link
Contributor

ankostis commented Aug 1, 2014

I'm suggesting to improve the exemplary code of the trick needed to make the autodoc sphinx-extension work with dependencies needing C-libraries:
http://read-the-docs.readthedocs.org/en/latest/faq.html#i-get-import-errors-on-libraries-that-depend-on-c-modules

Instead of including a half-baked mock-class why not use the readily unittest.mock.MagickMock?
That way we get 2 gains:

  1. A much smaller code, and
  2. the mocked modules work OK more often.

Here is the code i'm suggesting:

MOCK_MODULES = ['semantic_version', 'numpy']
from unittest import mock
sys.modules.update((mod_name, mock.MagicMock()) for mod_name in MOCK_MODULES)

The drawback is that the fix applies only for python 3.3+.1

I can request a merge if instructed on how to explain to the developers the python 3.3+ issue.

@ericholscher
Copy link
Member

Sounds like a good idea. Is this backported to 2.x do you know?

@jodal
Copy link
Member

jodal commented Aug 1, 2014

unittest.mock in Python 3.3 is the same as the mock package from PyPI, which works for all relevant Python versions.

@ericholscher
Copy link
Member

Cool. Would happily merge this if a PR was sent. Working on some other stuff today, but it sounds like a win.

ankostis added a commit to ankostis/readthedocs.org that referenced this issue Aug 1, 2014
ericholscher added a commit that referenced this issue Aug 1, 2014
#865: Use MagickMock class in the FAQ's exemplary code for mocking modul...
@ankostis
Copy link
Contributor Author

ankostis commented Aug 1, 2014

Please do not merge my request, it has a minor error in the python code (MagicKMock instead of MagicMock :-(

ericholscher added a commit that referenced this issue Aug 1, 2014
ankostis added a commit to ankostis/readthedocs.org that referenced this issue Aug 1, 2014
ericholscher added a commit that referenced this issue Aug 1, 2014
#865: Fix typo in suggested code regarding MagicKMock.
@ankostis
Copy link
Contributor Author

ankostis commented Aug 2, 2014

Well, although the MagicMock does a lost things better, it does not "behave" as package, as the class method of the previous code did.

So now I believe that i have nailed it:

from unittest.mock import MagicMock

class Mock(MagicMock):
    @classmethod
    def __getattr__(cls, name):
            return Mock()

I' preparing the patch.

ankostis added a commit to ankostis/readthedocs.org that referenced this issue Aug 2, 2014
ericholscher added a commit that referenced this issue Aug 2, 2014
#865: Restore part of the previous behavior on the suggested mocking-out...
@ericholscher
Copy link
Member

Thanks for this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants