@@ -415,7 +415,7 @@ def __new__(cls, /, *args, **kw):
415
415
if _is_async_obj (bound_args [spec_arg [0 ]]):
416
416
bases = (AsyncMockMixin , cls ,)
417
417
new = type (cls .__name__ , bases , {'__doc__' : cls .__doc__ })
418
- instance = object .__new__ (new )
418
+ instance = _safe_super ( NonCallableMock , cls ) .__new__ (new )
419
419
return instance
420
420
421
421
@@ -995,17 +995,18 @@ def _get_child_mock(self, /, **kw):
995
995
996
996
_type = type (self )
997
997
if issubclass (_type , MagicMock ) and _new_name in _async_method_magics :
998
+ # Any asynchronous magic becomes an AsyncMock
998
999
klass = AsyncMock
999
- elif _new_name in _sync_async_magics :
1000
- # Special case these ones b/c users will assume they are async,
1001
- # but they are actually sync (ie. __aiter__)
1002
- klass = MagicMock
1003
1000
elif issubclass (_type , AsyncMockMixin ):
1004
- klass = AsyncMock
1001
+ if _new_name in _all_sync_magics :
1002
+ # Any synchronous magic becomes a MagicMock
1003
+ klass = MagicMock
1004
+ else :
1005
+ klass = AsyncMock
1005
1006
elif not issubclass (_type , CallableMixin ):
1006
1007
if issubclass (_type , NonCallableMagicMock ):
1007
1008
klass = MagicMock
1008
- elif issubclass (_type , NonCallableMock ) :
1009
+ elif issubclass (_type , NonCallableMock ):
1009
1010
klass = Mock
1010
1011
else :
1011
1012
klass = _type .__mro__ [1 ]
@@ -1872,6 +1873,7 @@ def _patch_stopall():
1872
1873
"round trunc floor ceil "
1873
1874
"bool next "
1874
1875
"fspath "
1876
+ "aiter "
1875
1877
)
1876
1878
1877
1879
numerics = (
@@ -2010,21 +2012,22 @@ def _set_return_value(mock, method, name):
2010
2012
2011
2013
2012
2014
2013
- class MagicMixin (object ):
2015
+ class MagicMixin (Base ):
2014
2016
def __init__ (self , / , * args , ** kw ):
2015
2017
self ._mock_set_magics () # make magic work for kwargs in init
2016
2018
_safe_super (MagicMixin , self ).__init__ (* args , ** kw )
2017
2019
self ._mock_set_magics () # fix magic broken by upper level init
2018
2020
2019
2021
2020
2022
def _mock_set_magics (self ):
2021
- these_magics = _magics
2023
+ orig_magics = _magics | _async_method_magics
2024
+ these_magics = orig_magics
2022
2025
2023
2026
if getattr (self , "_mock_methods" , None ) is not None :
2024
- these_magics = _magics .intersection (self ._mock_methods )
2027
+ these_magics = orig_magics .intersection (self ._mock_methods )
2025
2028
2026
2029
remove_magics = set ()
2027
- remove_magics = _magics - these_magics
2030
+ remove_magics = orig_magics - these_magics
2028
2031
2029
2032
for entry in remove_magics :
2030
2033
if entry in type (self ).__dict__ :
@@ -2052,33 +2055,13 @@ def mock_add_spec(self, spec, spec_set=False):
2052
2055
self ._mock_set_magics ()
2053
2056
2054
2057
2055
- class AsyncMagicMixin :
2058
+ class AsyncMagicMixin ( MagicMixin ) :
2056
2059
def __init__ (self , / , * args , ** kw ):
2057
- self ._mock_set_async_magics () # make magic work for kwargs in init
2060
+ self ._mock_set_magics () # make magic work for kwargs in init
2058
2061
_safe_super (AsyncMagicMixin , self ).__init__ (* args , ** kw )
2059
- self ._mock_set_async_magics () # fix magic broken by upper level init
2060
-
2061
- def _mock_set_async_magics (self ):
2062
- these_magics = _async_magics
2063
-
2064
- if getattr (self , "_mock_methods" , None ) is not None :
2065
- these_magics = _async_magics .intersection (self ._mock_methods )
2066
- remove_magics = _async_magics - these_magics
2067
-
2068
- for entry in remove_magics :
2069
- if entry in type (self ).__dict__ :
2070
- # remove unneeded magic methods
2071
- delattr (self , entry )
2072
-
2073
- # don't overwrite existing attributes if called a second time
2074
- these_magics = these_magics - set (type (self ).__dict__ )
2075
-
2076
- _type = type (self )
2077
- for entry in these_magics :
2078
- setattr (_type , entry , MagicProxy (entry , self ))
2079
-
2062
+ self ._mock_set_magics () # fix magic broken by upper level init
2080
2063
2081
- class MagicMock (MagicMixin , AsyncMagicMixin , Mock ):
2064
+ class MagicMock (MagicMixin , Mock ):
2082
2065
"""
2083
2066
MagicMock is a subclass of Mock with default implementations
2084
2067
of most of the magic methods. You can use MagicMock without having to
@@ -2100,7 +2083,7 @@ def mock_add_spec(self, spec, spec_set=False):
2100
2083
2101
2084
2102
2085
2103
- class MagicProxy (object ):
2086
+ class MagicProxy (Base ):
2104
2087
def __init__ (self , name , parent ):
2105
2088
self .name = name
2106
2089
self .parent = parent
0 commit comments