@@ -32,21 +32,21 @@ def __init__(
32
32
spy_creator : "SpyCreator" ,
33
33
) -> None :
34
34
"""Initialize a BaseSpy from a call handler and an optional spec object."""
35
- super ().__setattr__ ("_core " , core )
36
- super ().__setattr__ ("_call_handler " , call_handler )
37
- super ().__setattr__ ("_spy_creator " , spy_creator )
38
- super ().__setattr__ ("_spy_children " , {})
39
- super ().__setattr__ ("_spy_property_values " , {})
40
- super ().__setattr__ ("__signature__" , self ._core .signature )
35
+ super ().__setattr__ ("_decoy_spy_core " , core )
36
+ super ().__setattr__ ("_decoy_spy_call_handler " , call_handler )
37
+ super ().__setattr__ ("_decoy_spy_creator " , spy_creator )
38
+ super ().__setattr__ ("_decoy_spy_children " , {})
39
+ super ().__setattr__ ("_decoy_spy_property_values " , {})
40
+ super ().__setattr__ ("__signature__" , self ._decoy_spy_core .signature )
41
41
42
42
@property # type: ignore[misc]
43
43
def __class__ (self ) -> Any :
44
44
"""Ensure Spy can pass `instanceof` checks."""
45
- return self ._core .class_type or type (self )
45
+ return self ._decoy_spy_core .class_type or type (self )
46
46
47
47
def __enter__ (self ) -> Any :
48
48
"""Allow a spy to be used as a context manager."""
49
- enter_spy = self ._get_or_create_child_spy ("__enter__" )
49
+ enter_spy = self ._decoy_spy_get_or_create_child_spy ("__enter__" )
50
50
return enter_spy ()
51
51
52
52
def __exit__ (
@@ -56,12 +56,14 @@ def __exit__(
56
56
traceback : Optional [TracebackType ],
57
57
) -> Optional [bool ]:
58
58
"""Allow a spy to be used as a context manager."""
59
- exit_spy = self ._get_or_create_child_spy ("__exit__" )
59
+ exit_spy = self ._decoy_spy_get_or_create_child_spy ("__exit__" )
60
60
return cast (Optional [bool ], exit_spy (exc_type , exc_value , traceback ))
61
61
62
62
async def __aenter__ (self ) -> Any :
63
63
"""Allow a spy to be used as an async context manager."""
64
- enter_spy = self ._get_or_create_child_spy ("__aenter__" , child_is_async = True )
64
+ enter_spy = self ._decoy_spy_get_or_create_child_spy (
65
+ "__aenter__" , child_is_async = True
66
+ )
65
67
return await enter_spy ()
66
68
67
69
async def __aexit__ (
@@ -71,49 +73,53 @@ async def __aexit__(
71
73
traceback : Optional [TracebackType ],
72
74
) -> Optional [bool ]:
73
75
"""Allow a spy to be used as a context manager."""
74
- exit_spy = self ._get_or_create_child_spy ("__aexit__" , child_is_async = True )
76
+ exit_spy = self ._decoy_spy_get_or_create_child_spy (
77
+ "__aexit__" , child_is_async = True
78
+ )
75
79
return cast (Optional [bool ], await exit_spy (exc_type , exc_value , traceback ))
76
80
77
81
def __repr__ (self ) -> str :
78
82
"""Get a helpful string representation of the spy."""
79
- return f"<Decoy mock `{ self ._core .full_name } `>"
83
+ return f"<Decoy mock `{ self ._decoy_spy_core .full_name } `>"
80
84
81
85
def __getattr__ (self , name : str ) -> Any :
82
86
"""Get a property of the spy, always returning a child spy."""
83
87
# do not attempt to mock magic methods
84
88
if name .startswith ("__" ) and name .endswith ("__" ):
85
89
return super ().__getattribute__ (name )
86
90
87
- return self ._get_or_create_child_spy (name )
91
+ return self ._decoy_spy_get_or_create_child_spy (name )
88
92
89
93
def __setattr__ (self , name : str , value : Any ) -> None :
90
94
"""Set a property on the spy, recording the call."""
91
95
event = SpyEvent (
92
- spy = self ._core .info ,
96
+ spy = self ._decoy_spy_core .info ,
93
97
payload = SpyPropAccess (
94
98
prop_name = name ,
95
99
access_type = PropAccessType .SET ,
96
100
value = value ,
97
101
),
98
102
)
99
- self ._call_handler .handle (event )
100
- self ._spy_property_values [name ] = value
103
+ self ._decoy_spy_call_handler .handle (event )
104
+ self ._decoy_spy_property_values [name ] = value
101
105
102
106
def __delattr__ (self , name : str ) -> None :
103
107
"""Delete a property on the spy, recording the call."""
104
108
event = SpyEvent (
105
- spy = self ._core .info ,
109
+ spy = self ._decoy_spy_core .info ,
106
110
payload = SpyPropAccess (prop_name = name , access_type = PropAccessType .DELETE ),
107
111
)
108
- self ._call_handler .handle (event )
109
- self ._spy_property_values .pop (name , None )
112
+ self ._decoy_spy_call_handler .handle (event )
113
+ self ._decoy_spy_property_values .pop (name , None )
110
114
111
- def _get_or_create_child_spy (self , name : str , child_is_async : bool = False ) -> Any :
115
+ def _decoy_spy_get_or_create_child_spy (
116
+ self , name : str , child_is_async : bool = False
117
+ ) -> Any :
112
118
"""Lazily construct a child spy, basing it on type hints if available."""
113
119
# check for any stubbed behaviors for property getter
114
- get_result = self ._call_handler .handle (
120
+ get_result = self ._decoy_spy_call_handler .handle (
115
121
SpyEvent (
116
- spy = self ._core .info ,
122
+ spy = self ._decoy_spy_core .info ,
117
123
payload = SpyPropAccess (
118
124
prop_name = name ,
119
125
access_type = PropAccessType .GET ,
@@ -124,30 +130,32 @@ def _get_or_create_child_spy(self, name: str, child_is_async: bool = False) -> A
124
130
if get_result :
125
131
return get_result .value
126
132
127
- if name in self ._spy_property_values :
128
- return self ._spy_property_values [name ]
133
+ if name in self ._decoy_spy_property_values :
134
+ return self ._decoy_spy_property_values [name ]
129
135
130
136
# return previously constructed (and cached) child spies
131
- if name in self ._spy_children :
132
- return self ._spy_children [name ]
137
+ if name in self ._decoy_spy_children :
138
+ return self ._decoy_spy_children [name ]
133
139
134
- child_core = self ._core .create_child_core (name = name , is_async = child_is_async )
135
- child_spy = self ._spy_creator .create (core = child_core )
136
- self ._spy_children [name ] = child_spy
140
+ child_core = self ._decoy_spy_core .create_child_core (
141
+ name = name , is_async = child_is_async
142
+ )
143
+ child_spy = self ._decoy_spy_creator .create (core = child_core )
144
+ self ._decoy_spy_children [name ] = child_spy
137
145
138
146
return child_spy
139
147
140
- def _call (self , * args : Any , ** kwargs : Any ) -> Any :
141
- bound_args , bound_kwargs = self ._core .bind_args (* args , ** kwargs )
148
+ def _decoy_spy_call (self , * args : Any , ** kwargs : Any ) -> Any :
149
+ bound_args , bound_kwargs = self ._decoy_spy_core .bind_args (* args , ** kwargs )
142
150
call = SpyEvent (
143
- spy = self ._core .info ,
151
+ spy = self ._decoy_spy_core .info ,
144
152
payload = SpyCall (
145
153
args = bound_args ,
146
154
kwargs = bound_kwargs ,
147
155
),
148
156
)
149
157
150
- result = self ._call_handler .handle (call )
158
+ result = self ._decoy_spy_call_handler .handle (call )
151
159
return result .value if result else None
152
160
153
161
@@ -156,7 +164,7 @@ class AsyncSpy(BaseSpy):
156
164
157
165
async def __call__ (self , * args : Any , ** kwargs : Any ) -> Any :
158
166
"""Handle a call to the spy asynchronously."""
159
- result = self ._call (* args , ** kwargs )
167
+ result = self ._decoy_spy_call (* args , ** kwargs )
160
168
return (await result ) if inspect .iscoroutine (result ) else result
161
169
162
170
@@ -165,7 +173,7 @@ class Spy(BaseSpy):
165
173
166
174
def __call__ (self , * args : Any , ** kwargs : Any ) -> Any :
167
175
"""Handle a call to the spy."""
168
- return self ._call (* args , ** kwargs )
176
+ return self ._decoy_spy_call (* args , ** kwargs )
169
177
170
178
171
179
AnySpy = Union [AsyncSpy , Spy ]
@@ -175,7 +183,7 @@ class SpyCreator:
175
183
"""Spy factory."""
176
184
177
185
def __init__ (self , call_handler : CallHandler ) -> None :
178
- self ._call_handler = call_handler
186
+ self ._decoy_spy_call_handler = call_handler
179
187
180
188
@overload
181
189
def create (self , * , core : SpyCore ) -> AnySpy :
@@ -208,5 +216,5 @@ def create(
208
216
return spy_cls (
209
217
core = core ,
210
218
spy_creator = self ,
211
- call_handler = self ._call_handler ,
219
+ call_handler = self ._decoy_spy_call_handler ,
212
220
)
0 commit comments