-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathmock.pyi
More file actions
577 lines (537 loc) · 19.9 KB
/
mock.pyi
File metadata and controls
577 lines (537 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
import sys
from _typeshed import MaybeNone
from collections.abc import Awaitable, Callable, Coroutine, Iterable, Mapping, Sequence
from contextlib import _GeneratorContextManager
from types import TracebackType
from typing import Any, ClassVar, Final, Generic, Literal, TypeVar, overload, type_check_only
from typing_extensions import ParamSpec, Self, TypeAlias, disjoint_base
_T = TypeVar("_T")
_TT = TypeVar("_TT", bound=type[Any])
_R = TypeVar("_R")
_F = TypeVar("_F", bound=Callable[..., Any])
_AF = TypeVar("_AF", bound=Callable[..., Coroutine[Any, Any, Any]])
_P = ParamSpec("_P")
if sys.version_info >= (3, 13):
# ThreadingMock added in 3.13
__all__ = (
"Mock",
"MagicMock",
"patch",
"sentinel",
"DEFAULT",
"ANY",
"call",
"create_autospec",
"ThreadingMock",
"AsyncMock",
"FILTER_DIR",
"NonCallableMock",
"NonCallableMagicMock",
"mock_open",
"PropertyMock",
"seal",
)
else:
__all__ = (
"Mock",
"MagicMock",
"patch",
"sentinel",
"DEFAULT",
"ANY",
"call",
"create_autospec",
"AsyncMock",
"FILTER_DIR",
"NonCallableMock",
"NonCallableMagicMock",
"mock_open",
"PropertyMock",
"seal",
)
FILTER_DIR: bool # controls the way mock objects respond to `dir` function
class _SentinelObject:
name: Any
def __init__(self, name: Any) -> None: ...
class _Sentinel:
def __getattr__(self, name: str) -> Any: ...
sentinel: _Sentinel
DEFAULT: Any
_ArgsKwargs: TypeAlias = tuple[tuple[Any, ...], Mapping[str, Any]]
_NameArgsKwargs: TypeAlias = tuple[str, tuple[Any, ...], Mapping[str, Any]]
_CallValue: TypeAlias = str | tuple[Any, ...] | Mapping[str, Any] | _ArgsKwargs | _NameArgsKwargs
if sys.version_info >= (3, 12):
class _Call(tuple[Any, ...]):
def __new__(
cls,
value: _CallValue = (),
name: str | None = "",
parent: _Call | None = None,
two: bool = False,
from_kall: bool = True,
) -> Self: ...
def __init__(
self,
value: _CallValue = (),
name: str | None = None,
parent: _Call | None = None,
two: bool = False,
from_kall: bool = True,
) -> None: ...
__hash__: ClassVar[None] # type: ignore[assignment]
def __eq__(self, other: object) -> bool: ...
def __ne__(self, value: object, /) -> bool: ...
def __call__(self, *args: Any, **kwargs: Any) -> _Call: ...
def __getattr__(self, attr: str) -> Any: ...
def __getattribute__(self, attr: str) -> Any: ...
@property
def args(self) -> tuple[Any, ...]: ...
@property
def kwargs(self) -> Mapping[str, Any]: ...
def call_list(self) -> Any: ...
else:
@disjoint_base
class _Call(tuple[Any, ...]):
def __new__(
cls,
value: _CallValue = (),
name: str | None = "",
parent: _Call | None = None,
two: bool = False,
from_kall: bool = True,
) -> Self: ...
def __init__(
self,
value: _CallValue = (),
name: str | None = None,
parent: _Call | None = None,
two: bool = False,
from_kall: bool = True,
) -> None: ...
__hash__: ClassVar[None] # type: ignore[assignment]
def __eq__(self, other: object) -> bool: ...
def __ne__(self, value: object, /) -> bool: ...
def __call__(self, *args: Any, **kwargs: Any) -> _Call: ...
def __getattr__(self, attr: str) -> Any: ...
def __getattribute__(self, attr: str) -> Any: ...
@property
def args(self) -> tuple[Any, ...]: ...
@property
def kwargs(self) -> Mapping[str, Any]: ...
def call_list(self) -> Any: ...
call: _Call
class _CallList(list[_Call]):
def __contains__(self, value: Any) -> bool: ...
class Base:
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
# We subclass with "Any" because mocks are explicitly designed to stand in for other types,
# something that can't be expressed with our static type system.
class NonCallableMock(Base, Any):
if sys.version_info >= (3, 12):
def __new__(
cls,
spec: list[str] | object | type[object] | None = None,
wraps: Any | None = None,
name: str | None = None,
spec_set: list[str] | object | type[object] | None = None,
parent: NonCallableMock | None = None,
_spec_state: Any | None = None,
_new_name: str = "",
_new_parent: NonCallableMock | None = None,
_spec_as_instance: bool = False,
_eat_self: bool | None = None,
unsafe: bool = False,
**kwargs: Any,
) -> Self: ...
else:
def __new__(cls, /, *args: Any, **kw: Any) -> Self: ...
def __init__(
self,
spec: list[str] | object | type[object] | None = None,
wraps: Any | None = None,
name: str | None = None,
spec_set: list[str] | object | type[object] | None = None,
parent: NonCallableMock | None = None,
_spec_state: Any | None = None,
_new_name: str = "",
_new_parent: NonCallableMock | None = None,
_spec_as_instance: bool = False,
_eat_self: bool | None = None,
unsafe: bool = False,
**kwargs: Any,
) -> None: ...
def __getattr__(self, name: str) -> Any: ...
def __delattr__(self, name: str) -> None: ...
def __setattr__(self, name: str, value: Any) -> None: ...
def __dir__(self) -> list[str]: ...
def assert_called_with(self, *args: Any, **kwargs: Any) -> None: ...
def assert_not_called(self) -> None: ...
def assert_called_once_with(self, *args: Any, **kwargs: Any) -> None: ...
def _format_mock_failure_message(self, args: Any, kwargs: Any, action: str = "call") -> str: ...
def assert_called(self) -> None: ...
def assert_called_once(self) -> None: ...
def reset_mock(self, visited: Any = None, *, return_value: bool = False, side_effect: bool = False) -> None: ...
def _extract_mock_name(self) -> str: ...
def _get_call_signature_from_name(self, name: str) -> Any: ...
def assert_any_call(self, *args: Any, **kwargs: Any) -> None: ...
def assert_has_calls(self, calls: Sequence[_Call], any_order: bool = False) -> None: ...
def mock_add_spec(self, spec: Any, spec_set: bool = False) -> None: ...
def _mock_add_spec(self, spec: Any, spec_set: bool, _spec_as_instance: bool = False, _eat_self: bool = False) -> None: ...
def attach_mock(self, mock: NonCallableMock, attribute: str) -> None: ...
def configure_mock(self, **kwargs: Any) -> None: ...
return_value: Any
side_effect: Any
called: bool
call_count: int
call_args: _Call | MaybeNone
call_args_list: _CallList
method_calls: _CallList
mock_calls: _CallList
def _format_mock_call_signature(self, args: Any, kwargs: Any) -> str: ...
def _call_matcher(self, _call: tuple[_Call, ...]) -> _Call: ...
def _get_child_mock(self, **kw: Any) -> NonCallableMock: ...
if sys.version_info >= (3, 13):
def _calls_repr(self) -> str: ...
else:
def _calls_repr(self, prefix: str = "Calls") -> str: ...
class CallableMixin(Base):
side_effect: Any
def __init__(
self,
spec: Any | None = None,
side_effect: Any | None = None,
return_value: Any = ...,
wraps: Any | None = None,
name: Any | None = None,
spec_set: Any | None = None,
parent: Any | None = None,
_spec_state: Any | None = None,
_new_name: Any = "",
_new_parent: Any | None = None,
**kwargs: Any,
) -> None: ...
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
class Mock(CallableMixin, NonCallableMock): ...
class _patch(Generic[_T]):
attribute_name: Any
getter: Callable[[], Any]
attribute: str
new: _T
new_callable: Any
spec: Any
create: bool
has_local: Any
spec_set: Any
autospec: Any
kwargs: Mapping[str, Any]
additional_patchers: Any
# If new==DEFAULT, self is _patch[Any]. Ideally we'd be able to add an overload for it so that self is _patch[MagicMock],
# but that's impossible with the current type system.
if sys.version_info >= (3, 10):
def __init__(
self: _patch[_T], # pyright: ignore[reportInvalidTypeVarUse] #11780
getter: Callable[[], Any],
attribute: str,
new: _T,
spec: Any | None,
create: bool,
spec_set: Any | None,
autospec: Any | None,
new_callable: Any | None,
kwargs: Mapping[str, Any],
*,
unsafe: bool = False,
) -> None: ...
else:
def __init__(
self: _patch[_T], # pyright: ignore[reportInvalidTypeVarUse] #11780
getter: Callable[[], Any],
attribute: str,
new: _T,
spec: Any | None,
create: bool,
spec_set: Any | None,
autospec: Any | None,
new_callable: Any | None,
kwargs: Mapping[str, Any],
) -> None: ...
def copy(self) -> _patch[_T]: ...
@overload
def __call__(self, func: _TT) -> _TT: ...
# If new==DEFAULT, this should add a MagicMock parameter to the function
# arguments. See the _patch_default_new class below for this functionality.
@overload
def __call__(self, func: Callable[_P, _R]) -> Callable[_P, _R]: ...
def decoration_helper(
self, patched: _patch[Any], args: Sequence[Any], keywargs: Any
) -> _GeneratorContextManager[tuple[Sequence[Any], Any]]: ...
def decorate_class(self, klass: _TT) -> _TT: ...
def decorate_callable(self, func: Callable[..., _R]) -> Callable[..., _R]: ...
def decorate_async_callable(self, func: Callable[..., Awaitable[_R]]) -> Callable[..., Awaitable[_R]]: ...
def get_original(self) -> tuple[Any, bool]: ...
target: Any
temp_original: Any
is_local: bool
def __enter__(self) -> _T: ...
def __exit__(
self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None, /
) -> None: ...
def start(self) -> _T: ...
def stop(self) -> None: ...
# This class does not exist at runtime, it's a hack to make this work:
# @patch("foo")
# def bar(..., mock: MagicMock) -> None: ...
@type_check_only
class _patch_pass_arg(_patch[_T]):
@overload
def __call__(self, func: _TT) -> _TT: ...
# Can't use the following as ParamSpec is only allowed as last parameter:
# def __call__(self, func: Callable[_P, _R]) -> Callable[Concatenate[_P, MagicMock], _R]: ...
@overload
def __call__(self, func: Callable[..., _R]) -> Callable[..., _R]: ...
class _patch_dict:
in_dict: Any
values: Any
clear: Any
def __init__(self, in_dict: Any, values: Any = (), clear: Any = False, **kwargs: Any) -> None: ...
def __call__(self, f: Any) -> Any: ...
if sys.version_info >= (3, 10):
def decorate_callable(self, f: _F) -> _F: ...
def decorate_async_callable(self, f: _AF) -> _AF: ...
def decorate_class(self, klass: Any) -> Any: ...
def __enter__(self) -> Any: ...
def __exit__(self, *args: object) -> Any: ...
start: Any
stop: Any
# This class does not exist at runtime, it's a hack to add methods to the
# patch() function.
@type_check_only
class _patcher:
TEST_PREFIX: str
dict: type[_patch_dict]
# This overload also covers the case, where new==DEFAULT. In this case, the return type is _patch[Any].
# Ideally we'd be able to add an overload for it so that the return type is _patch[MagicMock],
# but that's impossible with the current type system.
@overload
def __call__( # type: ignore[overload-overlap]
self,
target: str,
new: _T,
spec: Literal[False] | None = None,
create: bool = False,
spec_set: Literal[False] | None = None,
autospec: Literal[False] | None = None,
new_callable: None = None,
*,
unsafe: bool = False,
) -> _patch[_T]: ...
@overload
def __call__(
self,
target: str,
*,
# If not False or None, this is passed to new_callable
spec: Any | Literal[False] | None = None,
create: bool = False,
# If not False or None, this is passed to new_callable
spec_set: Any | Literal[False] | None = None,
autospec: Literal[False] | None = None,
new_callable: Callable[..., _T],
unsafe: bool = False,
# kwargs are passed to new_callable
**kwargs: Any,
) -> _patch_pass_arg[_T]: ...
@overload
def __call__(
self,
target: str,
*,
spec: Any | bool | None = None,
create: bool = False,
spec_set: Any | bool | None = None,
autospec: Any | bool | None = None,
new_callable: None = None,
unsafe: bool = False,
# kwargs are passed to the MagicMock/AsyncMock constructor
**kwargs: Any,
) -> _patch_pass_arg[MagicMock | AsyncMock]: ...
# This overload also covers the case, where new==DEFAULT. In this case, the return type is _patch[Any].
# Ideally we'd be able to add an overload for it so that the return type is _patch[MagicMock],
# but that's impossible with the current type system.
@overload
@staticmethod
def object(
target: Any,
attribute: str,
new: _T,
spec: Literal[False] | None = None,
create: bool = False,
spec_set: Literal[False] | None = None,
autospec: Literal[False] | None = None,
new_callable: None = None,
*,
unsafe: bool = False,
) -> _patch[_T]: ...
@overload
@staticmethod
def object(
target: Any,
attribute: str,
*,
# If not False or None, this is passed to new_callable
spec: Any | Literal[False] | None = None,
create: bool = False,
# If not False or None, this is passed to new_callable
spec_set: Any | Literal[False] | None = None,
autospec: Literal[False] | None = None,
new_callable: Callable[..., _T],
unsafe: bool = False,
# kwargs are passed to new_callable
**kwargs: Any,
) -> _patch_pass_arg[_T]: ...
@overload
@staticmethod
def object(
target: Any,
attribute: str,
*,
spec: Any | bool | None = None,
create: bool = False,
spec_set: Any | bool | None = None,
autospec: Any | bool | None = None,
new_callable: None = None,
unsafe: bool = False,
# kwargs are passed to the MagicMock/AsyncMock constructor
**kwargs: Any,
) -> _patch_pass_arg[MagicMock | AsyncMock]: ...
@overload
@staticmethod
def multiple(
target: Any | str,
# If not False or None, this is passed to new_callable
spec: Any | Literal[False] | None = None,
create: bool = False,
# If not False or None, this is passed to new_callable
spec_set: Any | Literal[False] | None = None,
autospec: Literal[False] | None = None,
*,
new_callable: Callable[..., _T],
# The kwargs must be DEFAULT
**kwargs: Any,
) -> _patch_pass_arg[_T]: ...
@overload
@staticmethod
def multiple(
target: Any | str,
# If not False or None, this is passed to new_callable
spec: Any | Literal[False] | None,
create: bool,
# If not False or None, this is passed to new_callable
spec_set: Any | Literal[False] | None,
autospec: Literal[False] | None,
new_callable: Callable[..., _T],
# The kwargs must be DEFAULT
**kwargs: Any,
) -> _patch_pass_arg[_T]: ...
@overload
@staticmethod
def multiple(
target: Any | str,
spec: Any | bool | None = None,
create: bool = False,
spec_set: Any | bool | None = None,
autospec: Any | bool | None = None,
new_callable: None = None,
# The kwargs are the mock objects or DEFAULT
**kwargs: Any,
) -> _patch[Any]: ...
@staticmethod
def stopall() -> None: ...
patch: _patcher
class MagicMixin(Base):
def __init__(self, *args: Any, **kw: Any) -> None: ...
class NonCallableMagicMock(MagicMixin, NonCallableMock): ...
class MagicMock(MagicMixin, Mock): ...
class AsyncMockMixin(Base):
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
async def _execute_mock_call(self, *args: Any, **kwargs: Any) -> Any: ...
def assert_awaited(self) -> None: ...
def assert_awaited_once(self) -> None: ...
def assert_awaited_with(self, *args: Any, **kwargs: Any) -> None: ...
def assert_awaited_once_with(self, *args: Any, **kwargs: Any) -> None: ...
def assert_any_await(self, *args: Any, **kwargs: Any) -> None: ...
def assert_has_awaits(self, calls: Iterable[_Call], any_order: bool = False) -> None: ...
def assert_not_awaited(self) -> None: ...
def reset_mock(self, *args: Any, **kwargs: Any) -> None: ...
await_count: int
await_args: _Call | None
await_args_list: _CallList
class AsyncMagicMixin(MagicMixin):
def __init__(self, *args: Any, **kw: Any) -> None: ...
class AsyncMock(AsyncMockMixin, AsyncMagicMixin, Mock):
# Improving the `reset_mock` signature.
# It is defined on `AsyncMockMixin` with `*args, **kwargs`, which is not ideal.
# But, `NonCallableMock` super-class has the better version.
def reset_mock(self, visited: Any = None, *, return_value: bool = False, side_effect: bool = False) -> None: ...
class MagicProxy(Base):
name: str
parent: Any
def __init__(self, name: str, parent: Any) -> None: ...
def create_mock(self) -> Any: ...
def __get__(self, obj: Any, _type: Any | None = None) -> Any: ...
# See https://github.com/python/typeshed/issues/14701
class _ANY(Any):
def __eq__(self, other: object) -> Literal[True]: ...
def __ne__(self, other: object) -> Literal[False]: ...
__hash__: ClassVar[None] # type: ignore[assignment]
ANY: _ANY
if sys.version_info >= (3, 10):
def create_autospec(
spec: Any,
spec_set: Any = False,
instance: Any = False,
_parent: Any | None = None,
_name: Any | None = None,
*,
unsafe: bool = False,
**kwargs: Any,
) -> Any: ...
else:
def create_autospec(
spec: Any,
spec_set: Any = False,
instance: Any = False,
_parent: Any | None = None,
_name: Any | None = None,
**kwargs: Any,
) -> Any: ...
class _SpecState:
spec: Any
ids: Any
spec_set: Any
parent: Any
instance: Any
name: Any
def __init__(
self,
spec: Any,
spec_set: Any = False,
parent: Any | None = None,
name: Any | None = None,
ids: Any | None = None,
instance: Any = False,
) -> None: ...
def mock_open(mock: Any | None = None, read_data: Any = "") -> Any: ...
class PropertyMock(Mock):
def __get__(self, obj: _T, obj_type: type[_T] | None = None) -> Self: ...
def __set__(self, obj: Any, val: Any) -> None: ...
if sys.version_info >= (3, 13):
class ThreadingMixin(Base):
DEFAULT_TIMEOUT: Final[float | None] = None
def __init__(self, /, *args: Any, timeout: float | None | _SentinelObject = ..., **kwargs: Any) -> None: ...
# Same as `NonCallableMock.reset_mock.`
def reset_mock(self, visited: Any = None, *, return_value: bool = False, side_effect: bool = False) -> None: ...
def wait_until_called(self, *, timeout: float | None | _SentinelObject = ...) -> None: ...
def wait_until_any_call_with(self, *args: Any, **kwargs: Any) -> None: ...
class ThreadingMock(ThreadingMixin, MagicMixin, Mock): ...
def seal(mock: Any) -> None: ...