@@ -409,7 +409,7 @@ def __new__(cls, /, *args, **kw):
409
409
if spec_arg and _is_async_obj (spec_arg ):
410
410
bases = (AsyncMockMixin , cls )
411
411
new = type (cls .__name__ , bases , {'__doc__' : cls .__doc__ })
412
- instance = object .__new__ (new )
412
+ instance = _safe_super ( NonCallableMock , cls ) .__new__ (new )
413
413
return instance
414
414
415
415
@@ -990,17 +990,18 @@ def _get_child_mock(self, /, **kw):
990
990
991
991
_type = type (self )
992
992
if issubclass (_type , MagicMock ) and _new_name in _async_method_magics :
993
+ # Any asynchronous magic becomes an AsyncMock
993
994
klass = AsyncMock
994
- elif _new_name in _sync_async_magics :
995
- # Special case these ones b/c users will assume they are async,
996
- # but they are actually sync (ie. __aiter__)
997
- klass = MagicMock
998
995
elif issubclass (_type , AsyncMockMixin ):
999
- klass = AsyncMock
996
+ if _new_name in _all_sync_magics :
997
+ # Any synchronous magic becomes a MagicMock
998
+ klass = MagicMock
999
+ else :
1000
+ klass = AsyncMock
1000
1001
elif not issubclass (_type , CallableMixin ):
1001
1002
if issubclass (_type , NonCallableMagicMock ):
1002
1003
klass = MagicMock
1003
- elif issubclass (_type , NonCallableMock ) :
1004
+ elif issubclass (_type , NonCallableMock ):
1004
1005
klass = Mock
1005
1006
else :
1006
1007
klass = _type .__mro__ [1 ]
@@ -1886,6 +1887,7 @@ def _patch_stopall():
1886
1887
"round trunc floor ceil "
1887
1888
"bool next "
1888
1889
"fspath "
1890
+ "aiter "
1889
1891
)
1890
1892
1891
1893
numerics = (
@@ -2024,21 +2026,22 @@ def _set_return_value(mock, method, name):
2024
2026
2025
2027
2026
2028
2027
- class MagicMixin (object ):
2029
+ class MagicMixin (Base ):
2028
2030
def __init__ (self , / , * args , ** kw ):
2029
2031
self ._mock_set_magics () # make magic work for kwargs in init
2030
2032
_safe_super (MagicMixin , self ).__init__ (* args , ** kw )
2031
2033
self ._mock_set_magics () # fix magic broken by upper level init
2032
2034
2033
2035
2034
2036
def _mock_set_magics (self ):
2035
- these_magics = _magics
2037
+ orig_magics = _magics | _async_method_magics
2038
+ these_magics = orig_magics
2036
2039
2037
2040
if getattr (self , "_mock_methods" , None ) is not None :
2038
- these_magics = _magics .intersection (self ._mock_methods )
2041
+ these_magics = orig_magics .intersection (self ._mock_methods )
2039
2042
2040
2043
remove_magics = set ()
2041
- remove_magics = _magics - these_magics
2044
+ remove_magics = orig_magics - these_magics
2042
2045
2043
2046
for entry in remove_magics :
2044
2047
if entry in type (self ).__dict__ :
@@ -2066,33 +2069,13 @@ def mock_add_spec(self, spec, spec_set=False):
2066
2069
self ._mock_set_magics ()
2067
2070
2068
2071
2069
- class AsyncMagicMixin :
2072
+ class AsyncMagicMixin ( MagicMixin ) :
2070
2073
def __init__ (self , / , * args , ** kw ):
2071
- self ._mock_set_async_magics () # make magic work for kwargs in init
2074
+ self ._mock_set_magics () # make magic work for kwargs in init
2072
2075
_safe_super (AsyncMagicMixin , self ).__init__ (* args , ** kw )
2073
- self ._mock_set_async_magics () # fix magic broken by upper level init
2074
-
2075
- def _mock_set_async_magics (self ):
2076
- these_magics = _async_magics
2077
-
2078
- if getattr (self , "_mock_methods" , None ) is not None :
2079
- these_magics = _async_magics .intersection (self ._mock_methods )
2080
- remove_magics = _async_magics - these_magics
2081
-
2082
- for entry in remove_magics :
2083
- if entry in type (self ).__dict__ :
2084
- # remove unneeded magic methods
2085
- delattr (self , entry )
2086
-
2087
- # don't overwrite existing attributes if called a second time
2088
- these_magics = these_magics - set (type (self ).__dict__ )
2089
-
2090
- _type = type (self )
2091
- for entry in these_magics :
2092
- setattr (_type , entry , MagicProxy (entry , self ))
2093
-
2076
+ self ._mock_set_magics () # fix magic broken by upper level init
2094
2077
2095
- class MagicMock (MagicMixin , AsyncMagicMixin , Mock ):
2078
+ class MagicMock (MagicMixin , Mock ):
2096
2079
"""
2097
2080
MagicMock is a subclass of Mock with default implementations
2098
2081
of most of the magic methods. You can use MagicMock without having to
@@ -2114,7 +2097,7 @@ def mock_add_spec(self, spec, spec_set=False):
2114
2097
2115
2098
2116
2099
2117
- class MagicProxy (object ):
2100
+ class MagicProxy (Base ):
2118
2101
def __init__ (self , name , parent ):
2119
2102
self .name = name
2120
2103
self .parent = parent
0 commit comments