Skip to content

Commit 56bbc9c

Browse files
authoredJun 21, 2024··
BUG: Fix documenting classes that contain unittest.mock.Mock (#352)
* BUG: Make `unittest.mock.Mock` not appear callable (fix #350) * Add class with mocks to unit test example package
1 parent cf5bffd commit 56bbc9c

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed
 

‎pdoc/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
cast, Any, Callable, Dict, Generator, Iterable, List, Mapping, NewType,
2727
Optional, Set, Tuple, Type, TypeVar, Union,
2828
)
29+
from unittest.mock import Mock
2930
from warnings import warn
3031

3132
from mako.lookup import TemplateLookup
@@ -410,7 +411,7 @@ def _is_public(ident_name):
410411

411412

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

415416

416417
def _is_descriptor(obj):

‎pdoc/test/__init__.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
from tempfile import TemporaryDirectory
2323
from time import sleep
2424
from types import ModuleType
25-
from unittest.mock import patch
25+
from unittest import expectedFailure
26+
from unittest.mock import Mock, patch
2627
from urllib.error import HTTPError
2728
from urllib.request import Request, urlopen
2829

@@ -355,6 +356,8 @@ def test_text(self):
355356
'B.p docstring',
356357
'C',
357358
'B.overridden docstring',
359+
'function_mock',
360+
'coroutine_mock',
358361
]
359362
exclude_patterns = [
360363
'_private',
@@ -1174,6 +1177,11 @@ def __init__(self):
11741177
self.assertEqual(mod.doc['C'].doc['class_var'].docstring, 'class var')
11751178
self.assertEqual(mod.doc['C'].doc['instance_var'].docstring, 'instance var')
11761179

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

11781186
class HtmlHelpersTest(unittest.TestCase):
11791187
"""

‎pdoc/test/example_pkg/__init__.py

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from collections import namedtuple
33
import subprocess
44
import os
5+
from unittest.mock import AsyncMock, Mock
56

67
CONST = 'const'
78
"""CONST docstring"""
@@ -363,3 +364,19 @@ def latex_math():
363364

364365
class Location(namedtuple('Location', 'lat lon')):
365366
"""Geo-location, GPS position."""
367+
368+
369+
def _func_spec(value: int) -> bool:
370+
...
371+
372+
async def _coro_spec(value: int) -> bool:
373+
...
374+
375+
376+
class HasMockAttributes:
377+
"""
378+
Test class containing instances of `unittest.mock.Mock`.
379+
"""
380+
381+
function_mock = Mock(spec=_func_spec)
382+
coroutine_mock = AsyncMock(spec=_coro_spec)

0 commit comments

Comments
 (0)
Please sign in to comment.