mock: fix overloads (#5534)

* mock: fix overloads

Fixes #5533. This changes the order and in some cases removes default
values from new.

* ignore incompatible overlaps

Co-authored-by: hauntsaninja <>
This commit is contained in:
Shantanu
2021-05-26 13:16:51 -07:00
committed by GitHub
parent af33b09bf6
commit 74a8900166

View File

@@ -204,6 +204,21 @@ class _patcher:
TEST_PREFIX: str
dict: Type[_patch_dict]
if sys.version_info >= (3, 8):
# 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
self,
target: Any,
new: _T,
spec: Optional[Any] = ...,
create: bool = ...,
spec_set: Optional[Any] = ...,
autospec: Optional[Any] = ...,
new_callable: Optional[Any] = ...,
**kwargs: Any,
) -> _patch[_T]: ...
@overload
def __call__( # type: ignore
self,
@@ -216,11 +231,9 @@ class _patcher:
new_callable: Optional[Any] = ...,
**kwargs: Any,
) -> _patch[Union[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.
else:
@overload
def __call__(
def __call__( # type: ignore
self,
target: Any,
new: _T,
@@ -231,7 +244,6 @@ class _patcher:
new_callable: Optional[Any] = ...,
**kwargs: Any,
) -> _patch[_T]: ...
else:
@overload
def __call__( # type: ignore
self,
@@ -244,19 +256,20 @@ class _patcher:
new_callable: Optional[Any] = ...,
**kwargs: Any,
) -> _patch[MagicMock]: ...
@overload
def __call__(
self,
target: Any,
new: _T,
spec: Optional[Any] = ...,
create: bool = ...,
spec_set: Optional[Any] = ...,
autospec: Optional[Any] = ...,
new_callable: Optional[Any] = ...,
**kwargs: Any,
) -> _patch[_T]: ...
if sys.version_info >= (3, 8):
@overload
def object( # type: ignore
self,
target: Any,
attribute: str,
new: _T,
spec: Optional[Any] = ...,
create: bool = ...,
spec_set: Optional[Any] = ...,
autospec: Optional[Any] = ...,
new_callable: Optional[Any] = ...,
**kwargs: Any,
) -> _patch[_T]: ...
@overload
def object( # type: ignore
self,
@@ -270,12 +283,13 @@ class _patcher:
new_callable: Optional[Any] = ...,
**kwargs: Any,
) -> _patch[Union[MagicMock, AsyncMock]]: ...
else:
@overload
def object(
def object( # type: ignore
self,
target: Any,
attribute: str,
new: _T = ...,
new: _T,
spec: Optional[Any] = ...,
create: bool = ...,
spec_set: Optional[Any] = ...,
@@ -283,7 +297,6 @@ class _patcher:
new_callable: Optional[Any] = ...,
**kwargs: Any,
) -> _patch[_T]: ...
else:
@overload
def object( # type: ignore
self,
@@ -297,19 +310,6 @@ class _patcher:
new_callable: Optional[Any] = ...,
**kwargs: Any,
) -> _patch[MagicMock]: ...
@overload
def object(
self,
target: Any,
attribute: str,
new: _T = ...,
spec: Optional[Any] = ...,
create: bool = ...,
spec_set: Optional[Any] = ...,
autospec: Optional[Any] = ...,
new_callable: Optional[Any] = ...,
**kwargs: Any,
) -> _patch[_T]: ...
def multiple(
self,
target: Any,