Skip to content

Make unittest.mock.Mock not appear callable #352

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

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -350,6 +351,8 @@ def test_text(self):
'B.p docstring',
'C',
'B.overridden docstring',
'function_mock',
'coroutine_mock',
]
exclude_patterns = [
'_private',
Expand Down Expand Up @@ -1157,6 +1160,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)