Repository: sphinx-doc/sphinx

Issue:
Change mock to work with class decorators
**Is your feature request related to a problem? Please describe.**
I am using autodoc and mock an external library that provides class decorators. All classes that use the mocked decorators do not show up in the documentation.

**Describe the solution you'd like**
Checking `args[0]` for `[type, FunctionType, MethodType]` in https://github.com/sphinx-doc/sphinx/blob/master/sphinx/ext/autodoc/mock.py#L61 solves the problem. Currently only `[FunctionType, MethodType]` are checked.

**Describe alternatives you've considered**
I am currently using this workaround in my `conf.py`:
```python
def call_mock(self, *args, **kw):
    from types import FunctionType, MethodType
    if args and type(args[0]) in [type, FunctionType, MethodType]:
        # Appears to be a decorator, pass through unchanged
        return args[0]
    return self

from sphinx.ext.autodoc.mock import _MockObject
_MockObject.__call__ = call_mock
```

**Additional context**



Discussion:

Patch:
diff --git a/CHANGES b/CHANGES
index 0b80ad1c1..bc6479bea 100644
--- a/CHANGES
+++ b/CHANGES
@@ -30,6 +30,7 @@ Bugs fixed
 * #6704: linkcheck: Be defensive and handle newly defined HTTP error code
 * #6655: image URLs containing ``data:`` causes gettext builder crashed
 * #6584: i18n: Error when compiling message catalogs on Hindi
+* #6709: autodoc: mock object does not work as a class decorator
 
 Testing
 --------
diff --git a/sphinx/ext/autodoc/mock.py b/sphinx/ext/autodoc/mock.py
index 4f534a452..7fa4627a6 100644
--- a/sphinx/ext/autodoc/mock.py
+++ b/sphinx/ext/autodoc/mock.py
@@ -60,7 +60,7 @@ def __getattr__(self, key: str) -> "_MockObject":
         return _make_subclass(key, self.__display_name__, self.__class__)()
 
     def __call__(self, *args, **kw) -> Any:
-        if args and type(args[0]) in [FunctionType, MethodType]:
+        if args and type(args[0]) in [type, FunctionType, MethodType]:
             # Appears to be a decorator, pass through unchanged
             return args[0]
         return self


Test patch:
diff --git a/tests/test_ext_autodoc_mock.py b/tests/test_ext_autodoc_mock.py
index 5562f47de..04bff11ae 100644
--- a/tests/test_ext_autodoc_mock.py
+++ b/tests/test_ext_autodoc_mock.py
@@ -96,3 +96,24 @@ class Derived(Base, mock.SubClass):
     assert isinstance(obj, Base)
     assert isinstance(obj, _MockObject)
     assert isinstance(obj.some_method(), Derived)
+
+
+def test_mock_decorator():
+    mock = _MockObject()
+
+    @mock.function_deco
+    def func():
+        """docstring"""
+
+    class Foo:
+        @mock.method_deco
+        def meth(self):
+            """docstring"""
+
+    @mock.class_deco
+    class Bar:
+        """docstring"""
+
+    assert func.__doc__ == "docstring"
+    assert Foo.meth.__doc__ == "docstring"
+    assert Bar.__doc__ == "docstring"