18
18
19
19
import pytest
20
20
21
- _T = TypeVar ("_T" )
22
-
23
-
24
- def _get_mock_module (config ):
25
- """
26
- Import and return the actual "mock" module. By default this is
27
- "unittest.mock", but the user can force to always use "mock" using
28
- the mock_use_standalone_module ini option.
29
- """
30
- if not hasattr (_get_mock_module , "_module" ):
31
- use_standalone_module = parse_ini_boolean (
32
- config .getini ("mock_use_standalone_module" )
33
- )
34
- if use_standalone_module :
35
- import mock
36
-
37
- _get_mock_module ._module = mock
38
- else :
39
- _get_mock_module ._module = unittest .mock
21
+ from ._util import get_mock_module , parse_ini_boolean
40
22
41
- return _get_mock_module . _module
23
+ _T = TypeVar ( "_T" )
42
24
43
25
44
26
class PytestMockWarning (UserWarning ):
@@ -54,7 +36,7 @@ class MockerFixture:
54
36
def __init__ (self , config : Any ) -> None :
55
37
self ._patches = [] # type: List[Any]
56
38
self ._mocks = [] # type: List[Any]
57
- self .mock_module = mock_module = _get_mock_module (config )
39
+ self .mock_module = mock_module = get_mock_module (config )
58
40
self .patch = self ._Patcher (
59
41
self ._patches , self ._mocks , mock_module
60
42
) # type: MockerFixture._Patcher
@@ -99,20 +81,14 @@ def spy(self, obj: object, name: str) -> unittest.mock.MagicMock:
99
81
:return: Spy object.
100
82
"""
101
83
method = getattr (obj , name )
102
-
103
- autospec = inspect .ismethod (method ) or inspect .isfunction (method )
104
- # Can't use autospec classmethod or staticmethod objects
105
- # see: https://bugs.python.org/issue23078
106
- if inspect .isclass (obj ):
107
- # Bypass class descriptor:
108
- # http://stackoverflow.com/questions/14187973/python3-check-if-method-is-static
109
- try :
110
- value = obj .__getattribute__ (obj , name ) # type:ignore
111
- except AttributeError :
112
- pass
113
- else :
114
- if isinstance (value , (classmethod , staticmethod )):
115
- autospec = False
84
+ if inspect .isclass (obj ) and isinstance (
85
+ inspect .getattr_static (obj , name ), (classmethod , staticmethod )
86
+ ):
87
+ # Can't use autospec classmethod or staticmethod objects before 3.7
88
+ # see: https://bugs.python.org/issue23078
89
+ autospec = False
90
+ else :
91
+ autospec = inspect .ismethod (method ) or inspect .isfunction (method )
116
92
117
93
def wrapper (* args , ** kwargs ):
118
94
spy_obj .spy_return = None
@@ -518,7 +494,7 @@ def wrap_assert_methods(config: Any) -> None:
518
494
if _mock_module_originals :
519
495
return
520
496
521
- mock_module = _get_mock_module (config )
497
+ mock_module = get_mock_module (config )
522
498
523
499
wrappers = {
524
500
"assert_called" : wrap_assert_called ,
@@ -594,16 +570,6 @@ def pytest_addoption(parser: Any) -> None:
594
570
)
595
571
596
572
597
- def parse_ini_boolean (value : Union [bool , str ]) -> bool :
598
- if isinstance (value , bool ):
599
- return value
600
- if value .lower () == "true" :
601
- return True
602
- if value .lower () == "false" :
603
- return False
604
- raise ValueError ("unknown string for bool: %r" % value )
605
-
606
-
607
573
def pytest_configure (config : Any ) -> None :
608
574
tb = config .getoption ("--tb" , default = "auto" )
609
575
if (
0 commit comments