Skip to content

Commit e5d3a47

Browse files
authored
Add stubs for AsyncMock (#4752)
1 parent e035760 commit e5d3a47

File tree

2 files changed

+362
-166
lines changed

2 files changed

+362
-166
lines changed

stdlib/3/unittest/mock.pyi

Lines changed: 181 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -164,35 +164,63 @@ class _patch(Generic[_T]):
164164
autospec: Any
165165
kwargs: Mapping[str, Any]
166166
additional_patchers: Any
167-
@overload
168-
def __init__(
169-
self: _patch[MagicMock],
170-
getter: Callable[[], Any],
171-
attribute: str,
172-
*,
173-
spec: Optional[Any],
174-
create: bool,
175-
spec_set: Optional[Any],
176-
autospec: Optional[Any],
177-
new_callable: Optional[Any],
178-
kwargs: Mapping[str, Any],
179-
) -> None: ...
180-
# This overload also covers the case, where new==DEFAULT. In this case, self is _patch[Any].
181-
# Ideally we'd be able to add an overload for it so that self is _patch[MagicMock],
182-
# but that's impossible with the current type system.
183-
@overload
184-
def __init__(
185-
self: _patch[_T],
186-
getter: Callable[[], Any],
187-
attribute: str,
188-
new: _T,
189-
spec: Optional[Any],
190-
create: bool,
191-
spec_set: Optional[Any],
192-
autospec: Optional[Any],
193-
new_callable: Optional[Any],
194-
kwargs: Mapping[str, Any],
195-
) -> None: ...
167+
if sys.version_info >= (3, 8):
168+
@overload
169+
def __init__(
170+
self: _patch[Union[MagicMock, AsyncMock]],
171+
getter: Callable[[], Any],
172+
attribute: str,
173+
*,
174+
spec: Optional[Any],
175+
create: bool,
176+
spec_set: Optional[Any],
177+
autospec: Optional[Any],
178+
new_callable: Optional[Any],
179+
kwargs: Mapping[str, Any],
180+
) -> None: ...
181+
# This overload also covers the case, where new==DEFAULT. In this case, self is _patch[Any].
182+
# Ideally we'd be able to add an overload for it so that self is _patch[MagicMock],
183+
# but that's impossible with the current type system.
184+
@overload
185+
def __init__(
186+
self: _patch[_T],
187+
getter: Callable[[], Any],
188+
attribute: str,
189+
new: _T,
190+
spec: Optional[Any],
191+
create: bool,
192+
spec_set: Optional[Any],
193+
autospec: Optional[Any],
194+
new_callable: Optional[Any],
195+
kwargs: Mapping[str, Any],
196+
) -> None: ...
197+
else:
198+
@overload
199+
def __init__(
200+
self: _patch[MagicMock],
201+
getter: Callable[[], Any],
202+
attribute: str,
203+
*,
204+
spec: Optional[Any],
205+
create: bool,
206+
spec_set: Optional[Any],
207+
autospec: Optional[Any],
208+
new_callable: Optional[Any],
209+
kwargs: Mapping[str, Any],
210+
) -> None: ...
211+
@overload
212+
def __init__(
213+
self: _patch[_T],
214+
getter: Callable[[], Any],
215+
attribute: str,
216+
new: _T,
217+
spec: Optional[Any],
218+
create: bool,
219+
spec_set: Optional[Any],
220+
autospec: Optional[Any],
221+
new_callable: Optional[Any],
222+
kwargs: Mapping[str, Any],
223+
) -> None: ...
196224
def copy(self) -> _patch[_T]: ...
197225
def __call__(self, func: Callable[..., _R]) -> Callable[..., _R]: ...
198226
def decorate_class(self, klass: _TT) -> _TT: ...
@@ -221,59 +249,113 @@ class _patch_dict:
221249
class _patcher:
222250
TEST_PREFIX: str
223251
dict: Type[_patch_dict]
224-
@overload
225-
def __call__( # type: ignore
226-
self,
227-
target: Any,
228-
*,
229-
spec: Optional[Any] = ...,
230-
create: bool = ...,
231-
spec_set: Optional[Any] = ...,
232-
autospec: Optional[Any] = ...,
233-
new_callable: Optional[Any] = ...,
234-
**kwargs: Any,
235-
) -> _patch[MagicMock]: ...
236-
# This overload also covers the case, where new==DEFAULT. In this case, the return type is _patch[Any].
237-
# Ideally we'd be able to add an overload for it so that the return type is _patch[MagicMock],
238-
# but that's impossible with the current type system.
239-
@overload
240-
def __call__(
241-
self,
242-
target: Any,
243-
new: _T,
244-
spec: Optional[Any] = ...,
245-
create: bool = ...,
246-
spec_set: Optional[Any] = ...,
247-
autospec: Optional[Any] = ...,
248-
new_callable: Optional[Any] = ...,
249-
**kwargs: Any,
250-
) -> _patch[_T]: ...
251-
@overload
252-
def object( # type: ignore
253-
self,
254-
target: Any,
255-
attribute: str,
256-
*,
257-
spec: Optional[Any] = ...,
258-
create: bool = ...,
259-
spec_set: Optional[Any] = ...,
260-
autospec: Optional[Any] = ...,
261-
new_callable: Optional[Any] = ...,
262-
**kwargs: Any,
263-
) -> _patch[MagicMock]: ...
264-
@overload
265-
def object(
266-
self,
267-
target: Any,
268-
attribute: str,
269-
new: _T = ...,
270-
spec: Optional[Any] = ...,
271-
create: bool = ...,
272-
spec_set: Optional[Any] = ...,
273-
autospec: Optional[Any] = ...,
274-
new_callable: Optional[Any] = ...,
275-
**kwargs: Any,
276-
) -> _patch[_T]: ...
252+
if sys.version_info >= (3, 8):
253+
@overload
254+
def __call__( # type: ignore
255+
self,
256+
target: Any,
257+
*,
258+
spec: Optional[Any] = ...,
259+
create: bool = ...,
260+
spec_set: Optional[Any] = ...,
261+
autospec: Optional[Any] = ...,
262+
new_callable: Optional[Any] = ...,
263+
**kwargs: Any,
264+
) -> _patch[Union[MagicMock, AsyncMock]]: ...
265+
# This overload also covers the case, where new==DEFAULT. In this case, the return type is _patch[Any].
266+
# Ideally we'd be able to add an overload for it so that the return type is _patch[MagicMock],
267+
# but that's impossible with the current type system.
268+
@overload
269+
def __call__(
270+
self,
271+
target: Any,
272+
new: _T,
273+
spec: Optional[Any] = ...,
274+
create: bool = ...,
275+
spec_set: Optional[Any] = ...,
276+
autospec: Optional[Any] = ...,
277+
new_callable: Optional[Any] = ...,
278+
**kwargs: Any,
279+
) -> _patch[_T]: ...
280+
else:
281+
@overload
282+
def __call__( # type: ignore
283+
self,
284+
target: Any,
285+
*,
286+
spec: Optional[Any] = ...,
287+
create: bool = ...,
288+
spec_set: Optional[Any] = ...,
289+
autospec: Optional[Any] = ...,
290+
new_callable: Optional[Any] = ...,
291+
**kwargs: Any,
292+
) -> _patch[MagicMock]: ...
293+
@overload
294+
def __call__(
295+
self,
296+
target: Any,
297+
new: _T,
298+
spec: Optional[Any] = ...,
299+
create: bool = ...,
300+
spec_set: Optional[Any] = ...,
301+
autospec: Optional[Any] = ...,
302+
new_callable: Optional[Any] = ...,
303+
**kwargs: Any,
304+
) -> _patch[_T]: ...
305+
if sys.version_info >= (3, 8):
306+
@overload
307+
def object( # type: ignore
308+
self,
309+
target: Any,
310+
attribute: str,
311+
*,
312+
spec: Optional[Any] = ...,
313+
create: bool = ...,
314+
spec_set: Optional[Any] = ...,
315+
autospec: Optional[Any] = ...,
316+
new_callable: Optional[Any] = ...,
317+
**kwargs: Any,
318+
) -> _patch[Union[MagicMock, AsyncMock]]: ...
319+
@overload
320+
def object(
321+
self,
322+
target: Any,
323+
attribute: str,
324+
new: _T = ...,
325+
spec: Optional[Any] = ...,
326+
create: bool = ...,
327+
spec_set: Optional[Any] = ...,
328+
autospec: Optional[Any] = ...,
329+
new_callable: Optional[Any] = ...,
330+
**kwargs: Any,
331+
) -> _patch[_T]: ...
332+
else:
333+
@overload
334+
def object( # type: ignore
335+
self,
336+
target: Any,
337+
attribute: str,
338+
*,
339+
spec: Optional[Any] = ...,
340+
create: bool = ...,
341+
spec_set: Optional[Any] = ...,
342+
autospec: Optional[Any] = ...,
343+
new_callable: Optional[Any] = ...,
344+
**kwargs: Any,
345+
) -> _patch[MagicMock]: ...
346+
@overload
347+
def object(
348+
self,
349+
target: Any,
350+
attribute: str,
351+
new: _T = ...,
352+
spec: Optional[Any] = ...,
353+
create: bool = ...,
354+
spec_set: Optional[Any] = ...,
355+
autospec: Optional[Any] = ...,
356+
new_callable: Optional[Any] = ...,
357+
**kwargs: Any,
358+
) -> _patch[_T]: ...
277359
def multiple(
278360
self,
279361
target: Any,
@@ -298,7 +380,23 @@ class MagicMock(MagicMixin, Mock):
298380
def mock_add_spec(self, spec: Any, spec_set: bool = ...) -> None: ...
299381

300382
if sys.version_info >= (3, 8):
301-
AsyncMock = Any
383+
class AsyncMockMixin(Base):
384+
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
385+
async def _execute_mock_call(self, *args: Any, **kwargs: Any) -> Any: ...
386+
def assert_awaited(self) -> None: ...
387+
def assert_awaited_once(self) -> None: ...
388+
def assert_awaited_with(self, *args: Any, **kwargs: Any) -> None: ...
389+
def assert_awaited_once_with(self, *args: Any, **kwargs: Any) -> None: ...
390+
def assert_any_await(self, *args: Any, **kwargs: Any) -> None: ...
391+
def assert_has_awaits(self, calls: _CallList, any_order: bool = ...) -> None: ...
392+
def assert_not_awaited(self) -> None: ...
393+
def reset_mock(self, *args, **kwargs) -> None: ...
394+
await_count: int
395+
await_args: Optional[_Call]
396+
await_args_list: _CallList
397+
class AsyncMagicMixin(MagicMixin):
398+
def __init__(self, *args: Any, **kw: Any) -> None: ...
399+
class AsyncMock(AsyncMockMixin, AsyncMagicMixin, Mock): ...
302400

303401
class MagicProxy:
304402
name: Any

0 commit comments

Comments
 (0)