1818
1919import pytest
2020
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
4022
41- return _get_mock_module . _module
23+ _T = TypeVar ( "_T" )
4224
4325
4426class PytestMockWarning (UserWarning ):
@@ -54,7 +36,7 @@ class MockerFixture:
5436 def __init__ (self , config : Any ) -> None :
5537 self ._patches = [] # type: List[Any]
5638 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 )
5840 self .patch = self ._Patcher (
5941 self ._patches , self ._mocks , mock_module
6042 ) # type: MockerFixture._Patcher
@@ -99,20 +81,14 @@ def spy(self, obj: object, name: str) -> unittest.mock.MagicMock:
9981 :return: Spy object.
10082 """
10183 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 )
11692
11793 def wrapper (* args , ** kwargs ):
11894 spy_obj .spy_return = None
@@ -518,7 +494,7 @@ def wrap_assert_methods(config: Any) -> None:
518494 if _mock_module_originals :
519495 return
520496
521- mock_module = _get_mock_module (config )
497+ mock_module = get_mock_module (config )
522498
523499 wrappers = {
524500 "assert_called" : wrap_assert_called ,
@@ -594,16 +570,6 @@ def pytest_addoption(parser: Any) -> None:
594570 )
595571
596572
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-
607573def pytest_configure (config : Any ) -> None :
608574 tb = config .getoption ("--tb" , default = "auto" )
609575 if (
0 commit comments