@@ -2788,8 +2788,8 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
2788
2788
if _parent is not None and not instance :
2789
2789
_parent ._mock_children [_name ] = mock
2790
2790
2791
- wrapped = kwargs . get ( 'wraps' )
2792
-
2791
+ # Pop wraps from kwargs because it must not be passed to configure_mock.
2792
+ wrapped = kwargs . pop ( 'wraps' , None )
2793
2793
if is_type and not instance and 'return_value' not in kwargs :
2794
2794
mock .return_value = create_autospec (spec , spec_set , instance = True ,
2795
2795
_name = '()' , _parent = mock ,
@@ -2814,12 +2814,12 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
2814
2814
except AttributeError :
2815
2815
continue
2816
2816
2817
- kwargs = {'spec' : original }
2817
+ child_kwargs = {'spec' : original }
2818
2818
# Wrap child attributes also.
2819
2819
if wrapped and hasattr (wrapped , entry ):
2820
- kwargs .update (wraps = original )
2820
+ child_kwargs .update (wraps = original )
2821
2821
if spec_set :
2822
- kwargs = {'spec_set' : original }
2822
+ child_kwargs = {'spec_set' : original }
2823
2823
2824
2824
if not isinstance (original , FunctionTypes ):
2825
2825
new = _SpecState (original , spec_set , mock , entry , instance )
@@ -2830,14 +2830,13 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
2830
2830
parent = mock .mock
2831
2831
2832
2832
skipfirst = _must_skip (spec , entry , is_type )
2833
- kwargs ['_eat_self' ] = skipfirst
2833
+ child_kwargs ['_eat_self' ] = skipfirst
2834
2834
if iscoroutinefunction (original ):
2835
2835
child_klass = AsyncMock
2836
2836
else :
2837
2837
child_klass = MagicMock
2838
2838
new = child_klass (parent = parent , name = entry , _new_name = entry ,
2839
- _new_parent = parent ,
2840
- ** kwargs )
2839
+ _new_parent = parent , ** child_kwargs )
2841
2840
mock ._mock_children [entry ] = new
2842
2841
new .return_value = child_klass ()
2843
2842
_check_signature (original , new , skipfirst = skipfirst )
@@ -2848,6 +2847,11 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
2848
2847
# setting as an instance attribute?
2849
2848
if isinstance (new , FunctionTypes ):
2850
2849
setattr (mock , entry , new )
2850
+ # kwargs are passed with respect to the parent mock so, they are not used
2851
+ # for creating return_value of the parent mock. So, this condition
2852
+ # should be true only for the parent mock if kwargs are given.
2853
+ if _is_instance_mock (mock ) and kwargs :
2854
+ mock .configure_mock (** kwargs )
2851
2855
2852
2856
return mock
2853
2857
0 commit comments