Skip to content

Commit

Permalink
BUG: Fix documenting classes that contain unittest.mock.Mock (#352)
Browse files Browse the repository at this point in the history
* BUG: Make `unittest.mock.Mock` not appear callable (fix #350)

* Add class with mocks to unit test example package
  • Loading branch information
Terrance authored Jun 21, 2024
1 parent cf5bffd commit 56bbc9c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pdoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
cast, Any, Callable, Dict, Generator, Iterable, List, Mapping, NewType,
Optional, Set, Tuple, Type, TypeVar, Union,
)
from unittest.mock import Mock
from warnings import warn

from mako.lookup import TemplateLookup
Expand Down Expand Up @@ -410,7 +411,7 @@ def _is_public(ident_name):


def _is_function(obj):
return inspect.isroutine(obj) and callable(obj)
return inspect.isroutine(obj) and callable(obj) and not isinstance(obj, Mock) # Mock: GH-350


def _is_descriptor(obj):
Expand Down
10 changes: 9 additions & 1 deletion pdoc/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
from tempfile import TemporaryDirectory
from time import sleep
from types import ModuleType
from unittest.mock import patch
from unittest import expectedFailure
from unittest.mock import Mock, patch
from urllib.error import HTTPError
from urllib.request import Request, urlopen

Expand Down Expand Up @@ -355,6 +356,8 @@ def test_text(self):
'B.p docstring',
'C',
'B.overridden docstring',
'function_mock',
'coroutine_mock',
]
exclude_patterns = [
'_private',
Expand Down Expand Up @@ -1174,6 +1177,11 @@ def __init__(self):
self.assertEqual(mod.doc['C'].doc['class_var'].docstring, 'class var')
self.assertEqual(mod.doc['C'].doc['instance_var'].docstring, 'instance var')

@expectedFailure
def test_mock_signature_error(self):
# GH-350 -- throws `TypeError: 'Mock' object is not subscriptable`:
self.assertIsInstance(inspect.signature(Mock(spec=lambda x: x)), inspect.Signature)


class HtmlHelpersTest(unittest.TestCase):
"""
Expand Down
17 changes: 17 additions & 0 deletions pdoc/test/example_pkg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from collections import namedtuple
import subprocess
import os
from unittest.mock import AsyncMock, Mock

CONST = 'const'
"""CONST docstring"""
Expand Down Expand Up @@ -363,3 +364,19 @@ def latex_math():

class Location(namedtuple('Location', 'lat lon')):
"""Geo-location, GPS position."""


def _func_spec(value: int) -> bool:
...

async def _coro_spec(value: int) -> bool:
...


class HasMockAttributes:
"""
Test class containing instances of `unittest.mock.Mock`.
"""

function_mock = Mock(spec=_func_spec)
coroutine_mock = AsyncMock(spec=_coro_spec)

0 comments on commit 56bbc9c

Please sign in to comment.