Skip to content
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: Allow mocked module decorators to pass-through functions unchanged #3071

Merged
merged 2 commits into from Nov 2, 2016

Conversation

mleinart
Copy link
Contributor

Summary

Autodoc's autodoc_mock_imports feature is useful for both ignoring imports and any module-level statements or class definitions that use those imports.

One category of imported objects that are most commonly used at the module level are decorators. I propose support for mocking imports that are used as decorators in autodoc'd modules.

Current Behavior

When a mocked import in an autodoc'd class is a decorator used on functions or methods in that module, the function, class, or method it decorates will be replaced with a _MockModule instance and the original definition discarded. This makes it impossible to autodoc anything decorated in projects where the decorator is from a mocked import.

Proposed Behavior

_MockModule should do a simple inspection on call()s to guess whether the mock instance is being called as a decorator and if so, pass through the original object unchanged.

Example Use Case

The Fabric project defines execution tasks in Python by decorating module-level functions with a decorator. If 'fabric' (and its submodules) is added to the list of mocked modules in conf.py, it becomes impossible to use autodoc to generate documentation on this config.

Note: This would also make Fabric's Sphinx helper unwrap_tasks() unnecessary, simplifying conf.py for Fabric users

@mleinart mleinart force-pushed the autodoc/pass_through_decorators branch from 8fc490f to b33dc29 Compare October 19, 2016 22:12
@tk0miya
Copy link
Member

tk0miya commented Oct 20, 2016

+1
Could you add any testcases for this?

@mleinart
Copy link
Contributor Author

@tk0miya added a couple tests and verified failure without this change, success with it.

Let me know if this doesn't turn out to be the best place to put these tests. I used test_generate since that's what calls import_object() where the mocking takes place and it's got pretty appropriate assertion helpers for these tests

@tk0miya tk0miya merged commit b32d9a9 into sphinx-doc:master Nov 2, 2016
@tk0miya
Copy link
Member

tk0miya commented Nov 2, 2016

Merged.
Thank you for contribution!

@tk0miya tk0miya added this to the 1.5 milestone Nov 2, 2016
@mleinart mleinart deleted the autodoc/pass_through_decorators branch December 13, 2016 20:31
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants