Improve unittest.mock stubs (#14421)

* Add `patch()` default arguments.
* Forbid certain argument combinations for `patch()`.
* Reorder `patch()` overloads.
* Add `unsafe` argument.
* Add overloads for `path.multiple()`.
This commit is contained in:
Sebastian Rittau
2025-08-22 09:31:26 +02:00
committed by GitHub
parent 94fe8571a8
commit f8773eb27a
+83 -35
View File
@@ -297,27 +297,32 @@ class _patcher:
# 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__(
def __call__( # type: ignore[overload-overlap]
self,
target: str,
new: _T,
spec: Any | None = ...,
create: bool = ...,
spec_set: Any | None = ...,
autospec: Any | None = ...,
new_callable: Callable[..., Any] | None = ...,
**kwargs: Any,
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,
*,
spec: Any | None = ...,
create: bool = ...,
spec_set: Any | None = ...,
autospec: Any | None = ...,
# 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
@@ -325,25 +330,31 @@ class _patcher:
self,
target: str,
*,
spec: Any | None = ...,
create: bool = ...,
spec_set: Any | None = ...,
autospec: Any | None = ...,
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: Any | None = ...,
create: bool = ...,
spec_set: Any | None = ...,
autospec: Any | None = ...,
new_callable: Callable[..., Any] | None = ...,
**kwargs: Any,
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
@@ -351,11 +362,15 @@ class _patcher:
target: Any,
attribute: str,
*,
spec: Any | None = ...,
create: bool = ...,
spec_set: Any | None = ...,
autospec: Any | None = ...,
# 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
@@ -364,21 +379,54 @@ class _patcher:
target: Any,
attribute: str,
*,
spec: Any | None = ...,
create: bool = ...,
spec_set: Any | None = ...,
autospec: Any | None = ...,
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,
spec: Any | None = ...,
create: bool = ...,
spec_set: Any | None = ...,
autospec: Any | None = ...,
new_callable: Any | None = ...,
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