@@ -2757,8 +2757,8 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
2757
2757
if _parent is not None and not instance :
2758
2758
_parent ._mock_children [_name ] = mock
2759
2759
2760
- wrapped = kwargs . get ( 'wraps' )
2761
-
2760
+ # Pop wraps from kwargs because it must not be passed to configure_mock.
2761
+ wrapped = kwargs . pop ( 'wraps' , None )
2762
2762
if is_type and not instance and 'return_value' not in kwargs :
2763
2763
mock .return_value = create_autospec (spec , spec_set , instance = True ,
2764
2764
_name = '()' , _parent = mock ,
@@ -2783,12 +2783,12 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
2783
2783
except AttributeError :
2784
2784
continue
2785
2785
2786
- kwargs = {'spec' : original }
2786
+ child_kwargs = {'spec' : original }
2787
2787
# Wrap child attributes also.
2788
2788
if wrapped and hasattr (wrapped , entry ):
2789
- kwargs .update (wraps = original )
2789
+ child_kwargs .update (wraps = original )
2790
2790
if spec_set :
2791
- kwargs = {'spec_set' : original }
2791
+ child_kwargs = {'spec_set' : original }
2792
2792
2793
2793
if not isinstance (original , FunctionTypes ):
2794
2794
new = _SpecState (original , spec_set , mock , entry , instance )
@@ -2799,14 +2799,13 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
2799
2799
parent = mock .mock
2800
2800
2801
2801
skipfirst = _must_skip (spec , entry , is_type )
2802
- kwargs ['_eat_self' ] = skipfirst
2802
+ child_kwargs ['_eat_self' ] = skipfirst
2803
2803
if iscoroutinefunction (original ):
2804
2804
child_klass = AsyncMock
2805
2805
else :
2806
2806
child_klass = MagicMock
2807
2807
new = child_klass (parent = parent , name = entry , _new_name = entry ,
2808
- _new_parent = parent ,
2809
- ** kwargs )
2808
+ _new_parent = parent , ** child_kwargs )
2810
2809
mock ._mock_children [entry ] = new
2811
2810
new .return_value = child_klass ()
2812
2811
_check_signature (original , new , skipfirst = skipfirst )
@@ -2817,6 +2816,11 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
2817
2816
# setting as an instance attribute?
2818
2817
if isinstance (new , FunctionTypes ):
2819
2818
setattr (mock , entry , new )
2819
+ # kwargs are passed with respect to the parent mock so, they are not used
2820
+ # for creating return_value of the parent mock. So, this condition
2821
+ # should be true only for the parent mock if kwargs are given.
2822
+ if _is_instance_mock (mock ) and kwargs :
2823
+ mock .configure_mock (** kwargs )
2820
2824
2821
2825
return mock
2822
2826
0 commit comments