Autodoc: Allow mocked module decorators to pass-through functions unchanged #3071
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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